aliyuncs/oss-sdk-php
Alibaba Cloud OSS SDK for PHP (V1): connect to Object Storage Service to upload, download, and manage files. Composer install, works on PHP 5.3+ with cURL. Supports common OSS operations for websites and applications with secure, reliable storage.
Installation
composer require aliyuncs/oss-sdk-php
For V2 (recommended):
composer require alibabacloud/oss-php-sdk-v2
First Use Case: Upload a File
use OSS\OssClient;
use OSS\Core\Auth\Credentials\Credentials;
$accessKeyId = 'your-access-key-id';
$accessKeySecret = 'your-access-key-secret';
$endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'; // Replace with your endpoint
$bucket = 'your-bucket-name';
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$result = $ossClient->putObject($bucket, 'example.txt', __DIR__ . '/example.txt');
Where to Look First
examples/ directory in the repo for quick-start snippets.Authentication & Initialization
.env for secrets.
$credentials = new Credentials(env('OSS_ACCESS_KEY_ID'), env('OSS_ACCESS_KEY_SECRET'));
$ossClient = new OssClient($credentials, env('OSS_ENDPOINT'));
$app->singleton(OssClient::class, function ($app) {
return new OssClient(
$app['config']['oss.access_key_id'],
$app['config']['oss.access_key_secret'],
$app['config']['oss.endpoint']
);
});
File Operations
// Upload with progress callback (V2)
$ossClient->putObjectV2(
$bucket,
'large-file.zip',
__DIR__ . '/large-file.zip',
[
'progress_callback' => function ($progress) {
Log::debug("Upload progress: {$progress['percent']}%");
}
]
);
$ossClient->putObjectFromStream($bucket, 'stream.txt', fopen('php://temp', 'r+'));
Bucket Management
$ossClient->createBucket('my-new-bucket', 'oss-cn-hangzhou');
$ossClient->deleteBucket('my-new-bucket');
$ossClient->putBucketAcl('my-bucket', 'public-read');
Advanced Features
$objects = $ossClient->listObjects($bucket);
foreach ($objects->getContents() as $object) {
// Process each object
}
$uploadId = $ossClient->initMultipartUpload($bucket, 'large-file.zip');
// Upload parts...
$ossClient->completeMultipartUpload($bucket, 'large-file.zip', $uploadId, $parts);
Integration with Laravel
league/flysystem-aliyun-oss for seamless integration with Laravel's Filesystem:
$adapter = new \League\Flysystem\AwsS3\AwsS3Adapter(
$ossClient,
$bucket,
'oss-cn-hangzhou'
);
$filesystem = new \League\Flysystem\Filesystem($adapter);
class UploadOssJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;
public function handle()
{
$ossClient->putObject($this->bucket, $this->key, $this->filePath);
}
}
V1 vs. V2 Migration
OSS → Alibabacloud\OSS.putObject() → putObjectV2() for progress callbacks.\Alibabacloud\OSS\Exception\OssException).CORS and Pre-Signed URLs
$url = $ossClient->presignObjectUrl($bucket, 'file.txt', time() + 3600); // 1-hour expiry
GET, PUT).Retry Logic
$config = new \Alibabacloud\Client\Config([
'retry' => [
'max_attempts' => 5,
'delay' => 100, // ms
]
]);
$ossClient = new OssClient($credentials, $endpoint, [], $config);
Large File Handling
multipartUpload for files > 100MB. V2 simplifies this with initMultipartUpload().Permissions and Security
OSS:PutObject).public-read unless required. Use pre-signed URLs or ACLs sparingly.Logging and Debugging
$config = new \Alibabacloud\Client\Config([
'debug' => true,
'logger' => new \Psr\Log\NullLogger(), // Replace with Monolog/PSR-3 logger
]);
SignatureDoesNotMatch: Check accessKeySecret or request signing.NoSuchBucket: Verify bucket name and region.InvalidAccessKeyId: Confirm accessKeyId is correct.Configuration Management
.env:
OSS_ACCESS_KEY_ID=your-key
OSS_ACCESS_KEY_SECRET=your-secret
OSS_ENDPOINT=oss-cn-hangzhou.aliyuncs.com
OSS_BUCKET=your-bucket
config/oss.php):
return [
'default' => env('OSS_ENDPOINT'),
'credentials' => [
'key' => env('OSS_ACCESS_KEY_ID'),
'secret' => env('OSS_ACCESS_KEY_SECRET'),
],
'buckets' => [
'primary' => env('OSS_BUCKET'),
],
];
Performance Optimization
TransmissionManager (V2) for concurrent uploads:
$transmissionManager = new \Alibabacloud\OSS\Transmission\TransmissionManager($ossClient);
$uploadTask = $transmissionManager->uploadObject(
$bucket,
'file.txt',
__DIR__ . '/file.txt'
);
$uploadTask->wait(); // Block until complete
Testing
$ossClient->deleteObject($bucket, 'test-file.txt');
How can I help you explore Laravel packages today?