Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Implement Image Upload with Laravel 5.6 and Alibaba Cloud OSS

Implement Image Upload with Laravel 5.6 and Alibaba Cloud OSS

This guide walks through setting up a Laravel 5.6 project, installing the Alibaba Cloud OSS storage package, configuring access credentials, creating a custom upload controller, and testing the image upload API with Postman, providing complete code snippets and step‑by‑step instructions.

This tutorial demonstrates how to build an image‑upload feature using Laravel 5.6 together with Alibaba Cloud OSS.

1. Project Setup and Environment Requirements

First, create a new Laravel 5.6 project. The server must meet the following PHP requirements:

PHP >= 7.1.3

OpenSSL PHP extension

PDO extension

Mbstring extension

Tokenizer extension

XML extension

Ctype extension

JSON extension

Ensure Composer is installed, then run: composer -v Install Laravel 5.6 with:

composer create-project --prefer-dist laravel/laravel blog

Generate the application key: php artisan key:generate Visit http://localhost/blog/public/index.php to verify the installation.

2. Add Alibaba Cloud OSS Package

In composer.json add the OSS storage package: "jacobcyl/ali-oss-storage": "^2.1" Then update dependencies:

composer update<br/>composer dumpautoload

3. Configure OSS Credentials

Add the service provider to config/app.php under providers: Jacobcyl\AliOSS\AliOssServiceProvider::class, In config/filesystems.php add a new disk configuration:

'oss' => [
    'driver' => 'oss',
    'access_id' => env('OSS_ACCESS_ID', 'your_id'),
    'access_key' => env('OSS_ACCESS_KEY', 'your_key'),
    'bucket' => env('OSS_BUCKET', 'your_bucket'),
    'endpoint' => env('OSS_ENDPOINT', 'oss-cn-hangzhou.aliyuncs.com'),
    'isCName' => false,
    'debug' => true,
],

Create config/alioss.php to hold the same environment variables:

<?php
return [
    'OSS_ACCESS_ID' => env('OSS_ACCESS_ID', 'your_id'),
    'OSS_ACCESS_KEY' => env('OSS_ACCESS_KEY', 'your_key'),
    'OSS_ENDPOINT' => env('OSS_ENDPOINT', 'oss-cn-hangzhou.aliyuncs.com'),
    'OSS_BUCKET' => env('OSS_BUCKET', 'your_bucket'),
    'OSS_HOST' => 'https://your_domain.oss-cn-hangzhou.aliyuncs.com', // front‑end domain
    'OSS_URL' => 'https://your_domain.oss-cn-hangzhou.aliyuncs.com', // CDN domain (optional)
];

4. Build the Upload API

Create a controller UploadsController that extends Controller. Define a POST route:

Route::post('/index/image', '\App\Http\Controllers\Index\UploadsController@index');

The controller method processes three possible image sources (file upload, remote URL, or base64 string), validates the image, converts it to base64, checks size, builds a storage path, and uploads the file to OSS. On success it returns the image name and public URL.

public function index(Request $request)
{
    $disk = \Storage::disk('oss');
    // validation and source handling omitted for brevity
    $image_info = $this->base64_image_format($base64_str);
    // size check
    $image_path = 'uploads/image/' . date('Ym');
    $image_name = $image_path . '/' . md5($image_info['image_str']) . '.' . $image_info['image_suffix'];
    $temp = $disk->put($image_name, $image_info['image_str']);
    if (!$temp) {
        return $this->array_format('Upload failed', 414);
    }
    return $this->array_format('Upload successful', 200, [
        'image_name' => $image_name,
        'image_url' => $disk->url($image_name),
    ]);
}

5. Test with Postman

Use Postman to send a POST request to /index/image with the appropriate parameters (file, URL, or base64). The response will contain the OSS URL of the uploaded image.

After completing the steps, the image upload functionality is fully operational.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

backendPHPStorageImage UploadLaravelAlibaba Cloud OSS

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion