freyo / flysystem-qcloud-cos-v4
腾讯云COS SDK V4的Flysystem适配器
2.1.1
2018-07-27 08:28 UTC
Requires
- php: >=5.5.0
- guzzlehttp/guzzle: ~6.0
- league/flysystem: ~1.0
- nesbot/carbon: ~1.0
- qcloud/cos-sdk-v4: ~4.0
Requires (Dev)
- php: >=5.5.0
- phpunit/phpunit: ~4.8
README
这是一个为qcloud-cos-sdk-php v4提供的Flysystem适配器。
腾讯云COS对象存储 V4
注意
如果您是在2016年10月之后新注册的用户,应使用v4版本。
2016年10月以后新注册的用户默认使用V4版本
如果您在2016年10月之前已使用COS,则可以继续使用v3版本。
2016年10月之前使用COS的用户可以继续使用V3版本
安装
composer require freyo/flysystem-qcloud-cos-v4
引导
<?php use Freyo\Flysystem\QcloudCOSv4\Adapter; use League\Flysystem\Filesystem; include __DIR__ . '/vendor/autoload.php'; $config = [ 'protocol' => 'http', 'domain' => 'your-domain', 'app_id' => 'your-app-id', 'secret_id' => 'your-secret-id', 'secret_key' => 'your-secret-key', 'timeout' => 60, 'bucket' => 'your-bucket-name', 'region' => 'gz', 'debug' => false, ]; $adapter = new Adapter($config); $filesystem = new Filesystem($adapter);
API
bool $flysystem->write('file.md', 'contents'); bool $flysystem->writeStream('file.md', fopen('path/to/your/local/file.jpg', 'r')); bool $flysystem->update('file.md', 'new contents'); bool $flysystem->updateStram('file.md', fopen('path/to/your/local/file.jpg', 'r')); bool $flysystem->rename('foo.md', 'bar.md'); bool $flysystem->copy('foo.md', 'foo2.md'); bool $flysystem->delete('file.md'); bool $flysystem->has('file.md'); string|false $flysystem->read('file.md'); array $flysystem->listContents(); array $flysystem->getMetadata('file.md'); int $flysystem->getSize('file.md'); string $flysystem->getUrl('file.md'); string $flysystem->getTemporaryUrl('file.md', date_create('2018-12-31 18:12:31')); string $flysystem->getMimetype('file.md'); int $flysystem->getTimestamp('file.md'); string $flysystem->getVisibility('file.md'); bool $flysystem->setVisibility('file.md', 'public'); //or 'private'
在Laravel中使用
Laravel 5.5使用包自动发现,因此不需要您手动添加ServiceProvider。
- 在
config/app.php
中注册服务提供者
'providers' => [ // ... Freyo\Flysystem\QcloudCOSv4\ServiceProvider::class, ]
- 配置
config/filesystems.php
'disks'=>[ // ... 'cosv4' => [ 'driver' => 'cosv4', 'protocol' => env('COSV4_PROTOCOL', 'http'), 'domain' => env('COSV4_DOMAIN'), 'app_id' => env('COSV4_APP_ID'), 'secret_id' => env('COSV4_SECRET_ID'), 'secret_key' => env('COSV4_SECRET_KEY'), 'timeout' => env('COSV4_TIMEOUT', 60), 'bucket' => env('COSV4_BUCKET'), 'region' => env('COSV4_REGION', 'gz'), 'debug' => env('COSV4_DEBUG', false), ], ],
在Lumen中使用
- 将以下代码添加到您的
bootstrap/app.php
$app->singleton('filesystem', function ($app) { $app->alias('filesystem', Illuminate\Contracts\Filesystem\Factory::class); return $app->loadComponent( 'filesystems', Illuminate\Filesystem\FilesystemServiceProvider::class, 'filesystem' ); });
- 和这个
$app->register(Freyo\Flysystem\QcloudCOSv4\ServiceProvider::class);
- 配置
.env
COSV4_PROTOCOL=http COSV4_DOMAIN= COSV4_APP_ID= COSV4_SECRET_ID= COSV4_SECRET_KEY= COSV4_TIMEOUT=60 COSV4_BUCKET= COSV4_REGION=gz COSV4_DEBUG=true
使用方法
$disk = Storage::disk('cosv4'); // create a file $disk->put('avatars/1', $fileContents); // check if a file exists $exists = $disk->has('file.jpg'); // get timestamp $time = $disk->lastModified('file1.jpg'); // copy a file $disk->copy('old/file1.jpg', 'new/file1.jpg'); // move a file $disk->move('old/file1.jpg', 'new/file1.jpg'); // get file contents $contents = $disk->read('folder/my_file.txt'); // get url $url = $disk->url('new/file1.jpg'); $temporaryUrl = $disk->temporaryUrl('new/file1.jpg', Carbon::now()->addMinutes(5)); // create a file from remote(plugin support) $disk->putRemoteFile('avatars/1', 'http://example.org/avatar.jpg'); $disk->putRemoteFileAs('avatars/1', 'http://example.org/avatar.jpg', 'file1.jpg');