English | 简体中文
Aliyun Oss Storage for Laravel
Alibaba Cloud Object Storage Service For Laravel
This package is a wrapper bridging aliyun-oss-flysystem into Laravel as an available storage disk.
Requirements
- PHP >= 7.0
- laravel/framework >= 5.5
Installation
-
If you use the composer to manage project dependencies, run the following command in your project's root directory:
$ composer require alphasnow/aliyun-oss-laravel
Then run
composer install
to install the dependency. -
Modify the environment file
.env
ALIYUN_OSS_ACCESS_ID = <Your aliyun accessKeyId, Required> ALIYUN_OSS_ACCESS_KEY= <Your aliyun accessKeySecret, Required> ALIYUN_OSS_BUCKET = <Your oss bucket name, Required> ALIYUN_OSS_ENDPOINT = <Your oss endpoint domain, Required>
-
(Optional) Modify the configuration file
config/filesystems.php
'default' => env('FILESYSTEM_DRIVER', 'aliyun'), // ... 'disks'=>[ // ... 'aliyun' => [ 'driver' => 'aliyun', 'access_id' => env('ALIYUN_OSS_ACCESS_ID'), // AccessKey ID, For example: LTAI4**************qgcsA 'access_key' => env('ALIYUN_OSS_ACCESS_KEY'), // AccessKey Secret, For example: PkT4F********************Bl9or 'bucket' => env('ALIYUN_OSS_BUCKET'), // For example: my-storage 'endpoint' => env('ALIYUN_OSS_ENDPOINT'), // For example: oss-cn-shanghai.aliyuncs.com 'internal' => env('ALIYUN_OSS_INTERNAL', null), // For example: oss-cn-shanghai-internal.aliyuncs.com 'domain' => env('ALIYUN_OSS_DOMAIN', null), // For example: oss.my-domain.com 'use_ssl' => env('ALIYUN_OSS_USE_SSL', false), // Whether to use https 'prefix' => env('ALIYUN_OSS_PREFIX', null), // The prefix of the store path ], // ... ]
Usage
use Illuminate\Support\Facades\Storage;
$storage = Storage::disk('aliyun');
Write
Storage::disk('aliyun')->putFile('dir/path', '/local/path/file.txt');
Storage::disk('aliyun')->putFileAs('dir/path', '/local/path/file.txt', 'file.txt');
Storage::disk('aliyun')->put('dir/path/file.txt', file_get_contents('/local/path/file.txt'));
$fp = fopen('/local/path/file.txt','r');
Storage::disk('aliyun')->put('dir/path/file.txt', $fp);
fclose($fp);
Storage::disk('aliyun')->prepend('dir/path/file.txt', 'Prepend Text');
Storage::disk('aliyun')->append('dir/path/file.txt', 'Append Text');
Storage::disk('aliyun')->put('dir/path/secret.txt', 'My secret', 'private');
Storage::disk('aliyun')->put('dir/path/download.txt', 'Download content', ["headers" => ["Content-Disposition" => "attachment; filename=file.txt"]]);
Read
Storage::disk('aliyun')->url('dir/path/file.txt');
Storage::disk('aliyun')->temporaryUrl('dir/path/file.txt');
Storage::disk('aliyun')->temporaryUrl('dir/path/file.txt', \Carbon\Carbon::now()->addMinutes(30));
Storage::disk('aliyun')->get('dir/path/file.txt');
Storage::disk('aliyun')->exists('dir/path/file.txt');
Storage::disk('aliyun')->size('dir/path/file.txt');
Storage::disk('aliyun')->lastModified('dir/path/file.txt');
Delete
Storage::disk('aliyun')->delete('dir/path/file.txt');
Storage::disk('aliyun')->delete(['dir/path/file1.txt', 'dir/path/file2.txt']);
File operation
Storage::disk('aliyun')->copy('dir/path/file.txt', 'dir/path/file_new.txt');
Storage::disk('aliyun')->move('dir/path/file.txt', 'dir/path/file_new.txt');
Storage::disk('aliyun')->rename('dir/path/file.txt', 'dir/path/file_new.txt');
Folder operation
Storage::disk('aliyun')->makeDirectory('dir/path');
Storage::disk('aliyun')->deleteDirectory('dir/path');
Storage::disk('aliyun')->files('dir/path');
Storage::disk('aliyun')->allFiles('dir/path');
Storage::disk('aliyun')->directories('dir/path');
Storage::disk('aliyun')->allDirectories('dir/path');
Use Plugin
Storage::disk('aliyun')->appendContent('dir/path/news.txt', 'The first line paragraph.', 0);
Storage::disk('aliyun')->appendContent('dir/path/news.txt', 'The second line paragraph.', 25);
Storage::disk('aliyun')->appendContent('dir/path/news.txt', 'The last line paragraph.', 51);
Use OssClient
$adapter = Storage::disk('aliyun')->getAdapter();
$client = $adapter->getClient();
$client->appendObject($adapter->getBucket(), $adapter->applyPathPrefix('dir/path/file.txt'), 'Append Text', 0);