luoyy/ali-oss-storage

支持Laravel 9+的阿里云OSS文件系统存储

v4.0.4 2024-05-15 08:51 UTC

README

阿里云OSS文件系统存储适配器,适用于Laravel 10。您可以使用阿里云OSS就像使用Laravel Storage一样。
借鉴了一些优秀的代码,综合各方,同时做了更多优化,将会添加更多完善的接口和插件,打造Laravel最好的OSS Storage扩展

灵感来源

要求

  • Laravel 10+
  • cURL扩展

安装

为了安装AliOSS-storage,只需在composer.json中添加

"luoyy/ali-oss-storage": "^4.0"

。然后运行composer installcomposer update
或者您可以直接运行以下命令进行安装

"composer require luoyy/ali-oss-storage:^4.0"

然后,在您的config/app.php中将以下行添加到providers数组中

luoyy\AliOSS\AliOssServiceProvider::class,

配置

app/filesystems.php中添加以下内容

'disks'=>[
    ...
    'oss' => [
            'driver' => 'oss',
            'access_id' => env('OSS_ACCESS_KEY_ID'),
            'access_key' => env('OSS_ACCESS_KEY_SECRET'),
            'bucket' => env('OSS_BUCKET'),
            'endpoint' => env('OSS_ENDPOINT'), // OSS 外网节点或自定义外部域名
            'endpoint_internal' => env('OSS_ENDPOINT_INTERNAL'), // 如果为空,则默认使用 endpoint 配置
            'cdnDomain' => env('OSS_DOMAIN'), // 如果不为空,getUrl会判断cdnDomain是否设定来决定返回的url,如果cdnDomain未设置,则使用endpoint来生成url,否则使用cdn
            'ssl' => env('OSS_SSL', false), // true to use 'https://' and false to use 'http://'. default is false,
            'prefix' => env('OSS_PREFIX'), // 路径前缀
            'options' => [],
            'throw' => true,
    ],
    ...
]

然后设置app/filesystems.php中的默认驱动

'default' => 'oss',

好了!您已经配置完成。现在可以自由使用阿里云OSS作为Storage了!

用法

查看Laravel 文档中的Storage,或者您也可以在这里学习

首先,您必须使用Storage门面

use Illuminate\Support\Facades\Storage;

然后,您可以使用Laravel Storage的所有API

Storage::disk('oss'); // if default filesystems driver is oss, you can skip this step

//fetch all files of specified bucket(see upond configuration)
Storage::files($directory);
Storage::allFiles($directory);

Storage::put('path/to/file/file.jpg', $contents); //first parameter is the target file path, second paramter is file content
Storage::putFile('path/to/file/file.jpg', 'local/path/to/local_file.jpg'); // upload file from local path

Storage::get('path/to/file/file.jpg'); // get the file object by path
Storage::exists('path/to/file/file.jpg'); // determine if a given file exists on the storage(OSS)
Storage::size('path/to/file/file.jpg'); // get the file size (Byte)
Storage::lastModified('path/to/file/file.jpg'); // get date of last modification

Storage::directories($directory); // Get all of the directories within a given directory
Storage::allDirectories($directory); // Get all (recursive) of the directories within a given directory

Storage::copy('old/file1.jpg', 'new/file1.jpg');
Storage::move('old/file1.jpg', 'new/file1.jpg');
Storage::rename('path/to/file1.jpg', 'path/to/file2.jpg');

Storage::prepend('file.log', 'Prepended Text'); // Prepend to a file.
Storage::append('file.log', 'Appended Text'); // Append to a file.

Storage::delete('file.jpg');
Storage::delete(['file1.jpg', 'file2.jpg']);

Storage::makeDirectory($directory); // Create a directory.
Storage::deleteDirectory($directory); // Recursively delete a directory.It will delete all files within a given directory, SO Use with caution please.

// upgrade logs
// new function for v2.0.1 version
Storage::url('path/to/img.jpg') // get the file url
Storage::temporaryUrl('path/to/img.jpg', 900) // Get a temporary URL for the file at the given path.

文档

更多开发细节请查看阿里云OSS文档

许可证

源代码在MIT许可证下发布。阅读LICENSE文件获取更多信息。