alphasnow / aliyun-oss-laravel
阿里云对象存储服务,用于Laravel
4.9.0
2024-06-14 01:45 UTC
Requires
- php: ^8.0.2
- alphasnow/aliyun-oss-flysystem: ^3.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.12
- mockery/mockery: ^1.5
- orchestra/testbench: ^7.13
- php-coveralls/php-coveralls: *
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
README
英语 | 简体中文
Aliyun OSS Laravel
此包是一个包装器,将 aliyun-oss-flysystem 与Laravel桥接,作为可用的存储磁盘。
如果需要客户端直接传输,请使用Web服务器签名直接传输OSS扩展包 aliyun-oss-appserver。
兼容性
安装
- 如果您使用composer管理项目依赖项,请在项目的根目录中运行以下命令
composer require alphasnow/aliyun-oss-laravel
然后运行composer install
以安装依赖项。 - 修改环境文件
.env
OSS_ACCESS_KEY_ID=<Your aliyun accessKeyId, Required> OSS_ACCESS_KEY_SECRET=<Your aliyun accessKeySecret, Required> OSS_BUCKET=<Your oss bucket name, Required> OSS_ENDPOINT=<Your oss endpoint domain, Required>
- (可选) 修改配置文件
config/filesystems.php
"default" => env("FILESYSTEM_DRIVER", "oss"), // ... "disks"=>[ // ... "oss" => [ "driver" => "oss", "access_key_id" => env("OSS_ACCESS_KEY_ID"), // Required, yourAccessKeyId "access_key_secret" => env("OSS_ACCESS_KEY_SECRET"), // Required, yourAccessKeySecret "bucket" => env("OSS_BUCKET"), // Required, for example: my-bucket "endpoint" => env("OSS_ENDPOINT"), // Required, for example: oss-cn-shanghai.aliyuncs.com "internal" => env("OSS_INTERNAL", null), // Optional, for example: oss-cn-shanghai-internal.aliyuncs.com "domain" => env("OSS_DOMAIN", null), // Optional, for example: oss.my-domain.com "is_cname" => env("OSS_CNAME", false), // Optional, if the Endpoint is a custom domain name, this must be true, see: https://github.com/aliyun/aliyun-oss-php-sdk/blob/572d0f8e099e8630ae7139ed3fdedb926c7a760f/src/OSS/OssClient.php#L113C1-L122C78 "prefix" => env("OSS_PREFIX", ""), // Optional, the prefix of the store path "use_ssl" => env("OSS_SSL", false), // Optional, whether to use HTTPS "throw" => env("OSS_THROW", false), // Optional, whether to throw an exception that causes an error "signatureVersion" => env("OSS_SIGNATURE_VERSION", "v1"), // Optional, select v1 or v4 as the signature version "region" => env("OSS_REGION", ""), // Optional, for example: cn-shanghai, used only when v4 signature version is selected "options" => [], // Optional, add global configuration parameters, For example: [\OSS\OssClient::OSS_CHECK_MD5 => false] "macros" => [] // Optional, add custom Macro, For example: [\App\Macros\ListBuckets::class, \App\Macros\CreateBucket::class] ], // ... ]
使用方法
use Illuminate\Support\Facades\Storage; $storage = Storage::disk("oss");
使用存储方法
写入
Storage::disk("oss")->putFile("dir/path", "/local/path/file.txt"); Storage::disk("oss")->putFileAs("dir/path", "/local/path/file.txt", "file.txt"); Storage::disk("oss")->put("dir/path/file.txt", file_get_contents("/local/path/file.txt")); $fp = fopen("/local/path/file.txt","r"); Storage::disk("oss")->put("dir/path/file.txt", $fp); fclose($fp); Storage::disk("oss")->prepend("dir/path/file.txt", "Prepend Text"); Storage::disk("oss")->append("dir/path/file.txt", "Append Text"); Storage::disk("oss")->put("dir/path/secret.txt", "My secret", "private"); Storage::disk("oss")->put("dir/path/download.txt", "Download content", ["headers" => ["Content-Disposition" => "attachment;filename=download.txt"]]);
读取
Storage::disk("oss")->url("dir/path/file.txt"); Storage::disk("oss")->temporaryUrl("dir/path/file.txt", \Carbon\Carbon::now()->addMinutes(30)); Storage::disk("oss")->get("dir/path/file.txt"); Storage::disk("oss")->exists("dir/path/file.txt"); Storage::disk("oss")->size("dir/path/file.txt"); Storage::disk("oss")->lastModified("dir/path/file.txt");
删除
Storage::disk("oss")->delete("dir/path/file.txt"); Storage::disk("oss")->delete(["dir/path/file1.txt", "dir/path/file2.txt"]);
文件操作
Storage::disk("oss")->copy("dir/path/file.txt", "dir/path/file_new.txt"); Storage::disk("oss")->move("dir/path/file.txt", "dir/path/file_new.txt"); Storage::disk("oss")->rename("dir/path/file.txt", "dir/path/file_new.txt");
文件夹操作
Storage::disk("oss")->makeDirectory("dir/path"); Storage::disk("oss")->deleteDirectory("dir/path"); Storage::disk("oss")->files("dir/path"); Storage::disk("oss")->allFiles("dir/path"); Storage::disk("oss")->directories("dir/path"); Storage::disk("oss")->allDirectories("dir/path");
使用宏
默认宏
Storage::disk("oss")->appendObject("dir/path/news.txt", "The first line paragraph.", 0); Storage::disk("oss")->appendObject("dir/path/news.txt", "The second line paragraph.", 25); Storage::disk("oss")->appendObject("dir/path/news.txt", "The last line paragraph.", 51); Storage::disk("oss")->appendFile("dir/path/file.zip", "dir/path/file.zip.001", 0); Storage::disk("oss")->appendFile("dir/path/file.zip", "dir/path/file.zip.002", 1024); Storage::disk("oss")->appendFile("dir/path/file.zip", "dir/path/file.zip.003", 1024); Storage::disk("oss")->processObject("dir/path/image.jpg", "image/resize,l_1000");
添加自定义宏
- 添加宏
namespace App\Macros; use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AliyunMacro; class ListBuckets implements AliyunMacro { // ... }
参考代码: AppendObject.php - 修改配置
[ "macros" => [\App\Macros\ListBuckets::class] ]
- 使用宏
Storage::disk("oss")->listBuckets()
使用OssClient
use AlphaSnow\LaravelFilesystem\Aliyun\OssClientAdapter; $adapter = new OssClientAdapter(Storage::disk("oss")); $adapter->client()->appendObject($adapter->bucket(), $adapter->path("dir/path/file.txt"), "contents", 0, $adapter->options(["visibility" => "private"]));