PHP code example of jasonmann / laravel-filesystem-ali

1. Go to this page and download the library: Download jasonmann/laravel-filesystem-ali library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jasonmann / laravel-filesystem-ali example snippets


'providers' => [
    // Other service providers...
    Jasonmann\LaravelFilesystem\Ali\AliStorageServiceProvider::class,
],


return [
   'disks' => [
        //...
        'ali' => [
            'driver' => 'ali',
            'root' => env('OSS_ROOT',''),
            'access_key' => env('OSS_ACCESS_KEY'),
            'secret_key' => env('OSS_SECRET_KEY'),
            'endpoint'   => env('OSS_ENDPOINT'),
            'bucket'     => env('OSS_BUCKET'),
            'isCName'    => env('OSS_IS_CNAME', false),
        ],
        //...
    ]
];

$app->register(\Jasonmann\LaravelFilesystem\Ali\AliStorageServiceProvider::class);



return [
   'disks' => [
        //...
        'ali' => [
            'driver' => 'ali',
            'root' => env('OSS_ROOT',''),
            'access_key' => env('OSS_ACCESS_KEY'),
            'secret_key' => env('OSS_SECRET_KEY'),
            'endpoint'   => env('OSS_ENDPOINT'),
            'bucket'     => env('OSS_BUCKET'),
            'isCName'    => env('OSS_IS_CNAME', false),
        ],
        //...
    ]
];


$disk = Storage::disk('ali');
// 上传
$disk->put('avatars/filename.jpg', $fileContents);
// 检查文件是否存在
$exists = $disk->has('file.jpg');
// 获取文件修改时间
$time = $disk->lastModified('file1.jpg');
$time = $disk->getTimestamp('file1.jpg');
// 拷贝文件
$disk->copy('old/file1.jpg', 'new/file1.jpg');
// 移动文件也可改名
$disk->move('old/file1.jpg', 'new/file1.jpg');
// 获取文件内容
$contents = $disk->read('folder/my_file.txt');

// 获取文件访问地址「公共读的 bucket 才生效」
$url = $disk->getUrl('folder/my_file.txt');
// 设置文件访问有效期「$timeout 为多少秒过期」「私有 bucket 才可看见效果」
$url = $disk->signUrl('cat.png', $timeout, ['x-oss-process' => 'image/circle,r_100']);
// 和 signurl 功能一样,区别在于 $expiration 是未来过期时间如:2022-05-05 17:50:32 时链接失效
$url = $disk->getTemporaryUrl('file.md', $expiration);

// 获取完整处理能力
$kernel = $disk->kernel();
// 例如:防盗链功能
$refererConfig = new RefererConfig();
// 设置允许空Referer。
$refererConfig->setAllowEmptyReferer(true);
// 添加Referer白名单。Referer参数支持通配符星号(*)和问号(?)。
$refererConfig->addReferer("www.aliiyun.com");
$refererConfig->addReferer("www.aliiyuncs.com");
$kernel->putBucketReferer($bucket, $refererConfig);

/**
 * 1. 前缀如:'images/'
 * 2. 回调服务器 url
 * 3. 回调自定义参数,oss 回传应用服务器时会带上
 * 4. 当前直传配置链接有效期
 */
$config = $disk->signatureConfig($prefix = '/', $callBackUrl = '', $customData = [], $expire = 30);

// 验签,就是如此简单
// $verify 验签结果,$data 回调数据
list($verify, $data) = $disk->verify();
// [$verify, $data] = $disk->verify(); // php 7.1 +
if (!$verify) {
    // 验证失败处理,此时 $data 为验签失败提示信息
}
// 注意一定要返回 json 格式的字符串,因为 oss 服务器只接收 json 格式,否则给前端报 CallbackFailed
return response()->json($data);