Installation
composer require oh-my-gold/aliyun-oss-storage
Publish Config
php artisan vendor:publish --provider="OhMyGold\AliyunOssStorage\AliyunOssStorageServiceProvider" --tag="config"
Edit config/aliyun-oss-storage.php with your AccessKeyId, AccessKeySecret, Endpoint, and Bucket.
First Use Case: Upload a File
use OhMyGold\AliyunOssStorage\Facades\AliyunOssStorage;
$path = AliyunOssStorage::putFile('folder/file.txt', storage_path('local/file.txt'));
Verify Connection
if (AliyunOssStorage::connected()) {
echo "Connected to OSS!";
}
File Operations
putFile(), putObject() (for streams)
AliyunOssStorage::putFile('path/to/file.jpg', $localPath, ['Content-Type' => 'image/jpeg']);
getFile(), getObject()
$tempPath = AliyunOssStorage::getFile('path/to/file.jpg', storage_path('temp/'));
deleteFile()
AliyunOssStorage::deleteFile('path/to/file.jpg');
Directory Handling
listObjects() (with pagination)
$objects = AliyunOssStorage::listObjects('folder/', ['prefix' => 'folder/']);
putObject() with a trailing / (OSS auto-creates).Metadata & Customization
AliyunOssStorage::putFile('file.txt', $localPath, [
'Content-Type' => 'text/plain',
'x-oss-meta-custom' => 'value'
]);
$metadata = AliyunOssStorage::getObjectMetadata('file.txt');
Signed URLs (Temporary Access)
$url = AliyunOssStorage::temporaryUrl('file.jpg', now()->addMinutes(15));
Filesystem Integration
Override Laravel’s default filesystem in config/filesystems.php:
'oss' => [
'driver' => 'oss',
'key' => env('OSS_KEY'),
'secret' => env('OSS_SECRET'),
'endpoint' => env('OSS_ENDPOINT'),
'bucket' => env('OSS_BUCKET'),
],
Then use Storage::disk('oss')->put() as usual.
Model Storage (e.g., Images)
use OhMyGold\AliyunOssStorage\Facades\AliyunOssStorage;
public function storeImage($file) {
$path = AliyunOssStorage::putFile(
"users/{$this->id}/{$file->hashName()}",
$file->path()
);
return $path;
}
Queue Jobs for Async Uploads
UploadToOss::dispatch($localPath, 'remote/path.jpg')->onQueue('oss-uploads');
Credentials Leaks
AccessKeyId/AccessKeySecret in code. Use Laravel’s .env and validate in config.oss:*).Endpoint Mismatches
Endpoint in config matches your OSS region (e.g., https://oss-cn-hangzhou.aliyuncs.com).AliyunOssStorage::connected() before production.Bucket Policy Issues
public-read or your RAM role).<CORSConfiguration>
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
</CORSRule>
</CORSConfiguration>
Large File Handling
$uploadId = AliyunOssStorage::initMultipartUpload('large-file.zip');
// Upload parts...
AliyunOssStorage::completeMultipartUpload($uploadId, ['part1', 'part2']);
Timeouts
'timeout' => 300, // seconds
Enable Logging
Add to config/aliyun-oss-storage.php:
'debug' => env('APP_DEBUG', false),
Check Laravel logs for OSS SDK errors.
SDK Verbose Mode Temporarily enable SDK logging:
\OSS\OssClient::setDebug(true);
Common Errors & Fixes
| Error | Cause | Solution |
|---|---|---|
AccessDenied |
Invalid credentials | Check .env and IAM permissions |
NoSuchBucket |
Bucket doesn’t exist | Create bucket in OSS console |
InvalidArgument (Endpoint) |
Wrong region endpoint | Use correct endpoint (e.g., cn-hangzhou) |
SlowDown |
Rate limiting | Retry with exponential backoff |
Custom Storage Adapter
Extend \OhMyGold\AliyunOssStorage\Storage\AliyunOssStorage to add:
Event Listeners Listen for upload/download events:
AliyunOssStorage::addListener('uploaded', function ($event) {
// Log or process uploaded file
});
Fallback to Local Storage Implement a hybrid filesystem:
$disk = Storage::disk('oss');
if (!$disk->exists($path)) {
$disk = Storage::disk('local');
}
Testing Use a mock OSS client in tests:
$mock = Mockery::mock('\OSS\OssClient');
$this->app->instance('\OSS\OssClient', $mock);
How can I help you explore Laravel packages today?