PHP code example of larva / laravel-flysystem-oss

1. Go to this page and download the library: Download larva/laravel-flysystem-oss 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/ */

    

larva / laravel-flysystem-oss example snippets


'oss' => [
    'driver'        => 'oss',
    'access_id'     => env('OSS_ACCESS_ID', 'your access id'),
    'access_key'    => env('OSS_ACCESS_KEY', 'your access key'),
    'bucket'        => env('OSS_BUCKET', 'your bucket'),
    'endpoint'      => env('OSS_ENDPOINT', 'your endpoint'),  // 不要使用 CNAME,请使用 OSS 的 endpoint 地址
    'url'           => env('OSS_URL', 'cdn url'),             // 自定义访问域名,可以是 CDN 或绑定的域名,如 https://www.bbb.com,末尾不要斜杠
    'root'          => env('OSS_ROOT', ''),                   // 文件路径前缀,若所有内容存放在子目录中则填写,否则留空
    'visibility'    => 'public',                               // 默认可见性,可选:public / private
    'security_token' => null,                                  // STS 临时安全令牌,用于临时授权场景
    'proxy'         => null,                                   // HTTP 代理地址
    'timeout'       => 3600,                                   // 请求超时时间(秒)
    'ssl'           => true,                                   // 是否使用 HTTPS
],

'default' => 'oss',

use Illuminate\Support\Facades\Storage;

$disk = Storage::disk('oss');

// 写入文件
$disk->put('path/to/file.txt', 'file contents');

// 以流的方式写入
$disk->writeStream('path/to/file.txt', fopen('local-file.txt', 'r'));

// 读取文件
$contents = $disk->get('path/to/file.txt');

// 以流的方式读取
$stream = $disk->readStream('path/to/file.txt');

// 判断文件是否存在
$exists = $disk->exists('path/to/file.txt');

// 删除文件
$disk->delete('path/to/file.txt');

// 复制 / 移动文件
$disk->copy('source/path.txt', 'destination/path.txt');
$disk->move('source/path.txt', 'destination/path.txt');

// 创建目录
$disk->makeDirectory('path/to/directory');

// 删除目录(递归删除目录下所有文件)
$disk->deleteDirectory('path/to/directory');

// 列出目录内容
foreach ($disk->listContents('path/to/directory') as $item) {
    $item->path();      // 路径
    $item->type();      // 类型:file / dir
    $item->lastModified();
    $item->fileSize();
}

$mimeType     = $disk->mimeType('path/to/file.txt');
$lastModified = $disk->lastModified('path/to/file.txt');
$fileSize     = $disk->fileSize('path/to/file.txt');

use Illuminate\Contracts\Filesystem\Visibility;

// 设置可见性
$disk->setVisibility('path/to/file.txt', Visibility::PRIVATE);

// 获取可见性
$visibility = $disk->getVisibility('path/to/file.txt');

$url = $disk->url('path/to/file.txt');
// 返回: https://cdn.example.com/path/to/file.txt

use Carbon\Carbon;

// 生成 10 分钟有效的临时 URL
$tempUrl = $disk->temporaryUrl('path/to/private-file.txt', Carbon::now()->addMinutes(10));

use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;

$disk = Storage::disk('oss');

// 生成 30 分钟有效的预签名上传 URL
$result = $disk->temporaryUploadUrl(
    'uploads/avatar.jpg',
    Carbon::now()->addMinutes(30)
);

// 返回结构:
// [
//     'url'     => 'https://bucket.oss-cn-hangzhou.aliyuncs.com/uploads/avatar.jpg?OSSAccessKeyId=...&Expires=...&Signature=...',
//     'headers' => [],
// ]

$result = $disk->temporaryUploadUrl('uploads/file.pdf', Carbon::now()->addMinutes(30), [
    'Content-Type'       => 'application/pdf',
    'x-oss-object-acl'   => 'public-read',
    'x-oss-storage-class' => 'IA',
]);

$client = $disk->getClient();

// 示例:分片上传大文件
$client->multiuploadFile('your-bucket', 'large-file.zip', '/path/to/local/large-file.zip');

use Illuminate\Contracts\Filesystem\Visibility;
use League\Flysystem\Visibility as FlysystemVisibility;

// 设置存储类型为低频访问
$disk->put('file.txt', 'contents', [
    'x-oss-storage-class' => 'IA',
]);

// 设置服务端加密
$disk->put('secret.txt', 'contents', [
    'x-oss-server-side-encryption' => 'AES256',
]);

// 设置对象标签
$disk->put('file.txt', 'contents', [
    'x-oss-tagging' => 'key1=value1&key2=value2',
]);