AliOss storage for Laravel based on iidestiny/flysystem-oss.
- PHP >= 7.0
$ composer require "iidestiny/laravel-filesystem-oss" -vvv
- After installing the library, register the
Iidestiny\LaravelFilesystemOss\OssStorageServiceProvider::class
in yourconfig/app.php
file:
'providers' => [
// Other service providers...
Iidestiny\LaravelFilesystemOss\OssStorageServiceProvider::class,
],
Laravel 5.5+ skip
- Add a new disk to your
config/filesystems.php
config:
<?php
return [
'disks' => [
//...
'oss' => [
'driver' => 'oss',
'root' => '',
'access_key' => env('OSS_ACCESS_KEY'),
'secret_key' => env('OSS_SECRET_KEY'),
'endpoint' => env('OSS_ENDPOINT'), // 使用 ssl 这里设置如: https://oss-cn-beijing.aliyuncs.com
'bucket' => env('OSS_BUCKET'),
'isCName' => env('OSS_IS_CNAME', false), // 如果 isCname 为 false,endpoint 应配置 oss 提供的域名如:`oss-cn-beijing.aliyuncs.com`,否则为自定义域名,,cname 或 cdn 请自行到阿里 oss 后台配置并绑定 bucket
],
//...
]
];
<?php
$disk = Storage::disk('oss');
// create a file
$disk->put('avatars/filename.jpg', $fileContents);
// check if a file exists
$exists = $disk->has('file.jpg');
// get timestamp
$time = $disk->lastModified('file1.jpg');
$time = $disk->getTimestamp('file1.jpg');
// copy a file
$disk->copy('old/file1.jpg', 'new/file1.jpg');
// move a file
$disk->move('old/file1.jpg', 'new/file1.jpg');
// get file contents
$contents = $disk->read('folder/my_file.txt');
// get file url
$url = $disk->getUrl('folder/my_file.txt');
// file access period & file handle
$url = $disk->signUrl('cat.png', $timeout, ['x-oss-process' => 'image/circle,r_100']);
See more methods laravel-filesystem-doc
oss 直传有三种方式,当前扩展包使用的是最完整的 服务端签名直传并设置上传回调 方式,扩展包只生成前端页面上传所需的签名参数,前端上传实现可参考 官方文档中的实例 或自行搜索
$config = $disk->signatureConfig($prefix, $callBackUrl, $expire);
MIT