fypyuhu/l5-gcs

Laravel 7.0+ Flysystem Google Cloud Storage Service Provider

4.0 2020-06-19 15:19 UTC

This package is auto-updated.

Last update: 2024-09-20 01:21:43 UTC


README

仅将cedricziel/flysystem-gcs包装在Laravel 5.5+兼容的服务提供者中,并添加了一些我需要的其他功能。

Laravel附加功能

  • 可以使用temporaryUrl方法。
  • 在写入对象时可以添加元数据。

安装

composer require fypyuhu/l5-gcs

app.php中注册服务提供者

'providers' => [
    // ...
    fypyuhu\GCSProvider\GoogleCloudStorageServiceProvider::class,
]

filesystems.php配置文件中添加新的磁盘

'gcs' => [
    'driver' => 'gcs',
    'projectId' => env('GCS_PROJECT_ID'),
    'bucket' => env('GCS_BUCKET'),
    'keyFilePath' => storage_path(env('GCS_KEY_FILE')),
    'url' => env('GCS_CUSTOM_URL'), // optional: for custom url only
],

使用方法

使用Storage-Facade像使用其他Flysystem Adapter一样使用它。

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

// Put a private file on the 'gcs' disk which is a Google Cloud Storage bucket
$disk->put('test.png', file_get_contents(storage_path('app/test.png')));

// Put a public-accessible file on the 'gcs' disk which is a Google Cloud Storage bucket
$disk->put(
    'test-public.png',
    file_get_contents(storage_path('app/test-public.png')),
    \Illuminate\Contracts\Filesystem\Filesystem::VISIBILITY_PUBLIC
);

// Put a public-accessible file with metadata on the 'gcs' disk which is a Google Cloud Storage bucket 
$disk->put(
    'test.png',
    file_get_contents(storage_path('app/test.png')),
    [
        'metadata' => [
            'contentDisposition' => 'attachment; filename=file.png',
            'contentType' => 'image/png',
        ],
        'visibility' => \Illuminate\Contracts\Filesystem\Filesystem::VISIBILITY_PUBLIC
    ]
);

// Retrieve a file
$file = $disk->get('test.png');

// Get a temporary url for 1 hour
$disk->temporaryUrl('test.png', time() + 3600);

// Get a temporary url for 1 hour with other options
// see other option: https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.45.1/storage/storageobject?method=signedUrl
$disk->temporaryUrl('test.pdf', time() + 3600, ['saveAsName' => 'file.png']);

许可

MIT