cedricziel / flysystem-gcs
使用 gcloud-php 的 Google Cloud Storage (GCS) Flysystem 适配器
Requires
- php: >=7.2
- google/cloud-storage: ^1.3
- league/flysystem: ^1.0.40
Requires (Dev)
- phpunit/phpunit: >=8.5
README
使用 gcloud PHP 库的 Google 云存储 Flysystem 适配器
安装
使用 composer
composer require cedricziel/flysystem-gcs
如何使用
use CedricZiel\FlysystemGcs\GoogleCloudStorageAdapter; use League\Flysystem\Filesystem; $adapterOptions = [ 'projectId' => 'my-project-id', 'bucket' => 'my-project-bucket.appspot.com', 'prefix' => 'my-path-prefix/inside/the/bucket', ]; $adapter = new GoogleCloudStorageAdapter(null, $adapterOptions); $filesystem = new Filesystem($adapter);
身份验证
该库利用 PHP 库 google/cloud
,该库又使用 google/auth。
为什么这很重要?这是因为如果你通过 gcloud
命令行工具或运行在 Google 云平台 VM 上进行本地身份验证,在许多情况下,你已经是认证的,你不需要做任何关于身份验证的事情。
对于任何其他情况,你很可能需要导出环境变量 GOOGLE_APPLICATION_CREDENTIALS
,其值为授权使用 Storage: Full Access
oAuth2 范围的服务账户凭证的绝对路径,然后你就可以设置了。
所有示例,包括测试,都使用此行为。
如果你不希望这样做,你可以创建自己的 StorageClient
对象,该对象以不同的方式认证,并将其作为第一个参数传递给适配器类构造函数。
演示
有一个 演示项目,它展示了文件系统管理器中的简单操作。
存储对象的公开 URL
该适配器提供了两种不同的方法来生成公开 URL
- 一个 flysystem 插件,它在你的
FilesystemInterface
实例上公开一个getUrl($path)
方法 - 适配器本身上的
getUrl($path)
方法来生成 URL
下面将说明何时使用其中一个或另一个。
flysystem 插件
使用此适配器生成公开 URL 的标准方式是向你的 FilesystemInterface
实例添加一个 flysystem 插件。
插件 需要 一段配置,告诉它是否以及使用哪个存储桶,结合标准的 https://storage.googleapis.com/bucket/object.url
或自定义 CNAME URL(如 http://storage.my-domain.com
)。
请注意,通过 CNAME 的 GCS 公开访问仅限于 http
(没有 https
)。
示例:标准存储桶的公开 URL
假设你有存储桶 my-application-bucket
,按以下方式配置插件
// create your adapter as usual $adapter = new GoogleCloudStorageAdapter(..); // add it to your `FilesystemInterface` instance $filesystem = new Filesystem($adapter); // create your configuration $path = 'text-object.txt'; $config = ['bucket' => 'my-application-bucket']; $filesystem->addPlugin(new GoogleCloudStoragePublicUrlPlugin($config)); $publicUrl = $filesystem->getUrl($path); // $publicUrl == 'https://storage.googleapis.com/my-application-bucket/text-object.txt';
示例:具有目录前缀的存储桶的标准公开 URL
假设你有存储桶 my-application-bucket
,其目录前缀为 my/prefix
,将前缀与反斜杠连接到存储桶名称
// create your adapter as usual $adapter = new GoogleCloudStorageAdapter(null, ['prefix' => 'my/prefix', ...]); // add it to your `FilesystemInterface` instance $filesystem = new Filesystem($adapter); // create your configuration $path = 'text-object.txt'; $config = ['bucket' => 'my-application-bucket/my/prefix']; $filesystem->addPlugin(new GoogleCloudStoragePublicUrlPlugin($config)); $publicUrl = $filesystem->getUrl($path); // $publicUrl == 'https://storage.googleapis.com/my-application-bucket/my/prefix/text-object.txt';
示例:存储桶的自定义域名
假设你已设置一个 CNAME assets.example.com
指向文档中提到的公开端点,按以下方式配置插件
// create your adapter as usual $adapter = new GoogleCloudStorageAdapter(..); // add it to your `FilesystemInterface` instance $filesystem = new Filesystem($adapter); // create your configuration $path = 'text-object.txt'; $config = ['url' => 'http://assets.example.com']; $filesystem->addPlugin(new GoogleCloudStoragePublicUrlPlugin($config)); $publicUrl = $filesystem->getUrl($path); // $publicUrl == 'http://assets.example.com/text-object.txt'
示例:具有目录前缀的存储桶的自定义域名
假设您已设置一个CNAME assets.example.com
指向文档中提到的公共端点(文档),并且您的文件系统使用目录前缀为 my/prefix
,您需要将前缀附加到配置中的 url
,并按以下方式配置插件:
// create your adapter as usual $adapter = new GoogleCloudStorageAdapter(null, ['prefix' => 'my/prefix', ...]); // add it to your `FilesystemInterface` instance $filesystem = new Filesystem($adapter); // create your configuration $path = 'text-object.txt'; $config = ['url' => 'http://assets.example.com/my/prefix']; $filesystem->addPlugin(new GoogleCloudStoragePublicUrlPlugin($config)); $publicUrl = $filesystem->getUrl($path); // $publicUrl == 'http://assets.example.com/my/prefix/text-object.txt'
getUrl
在适配器 / Laravel 5
Laravel 5 中使用的存储服务不使用flysystem插件。
Laravel 5 特定的flysystem实例会检查适配器对象上是否存在 getUrl
方法。
此方法在适配器上实现,这也是您可以直接添加适配器并立即使用它的原因。
// create the adapter Storage::extend('gcs', function($app, $config) { $adapter = new GoogleCloudStorageAdapter(null, ['bucket' => 'my-bucket', ...]); // add it to your `FilesystemInterface` instance return new Filesystem($adapter); }); // register a new disk of type 'gcs' and name it 'gcs' // use it $gcs = Storage::disk('gcs'); $path = 'test-laravel.txt'; $gcs->put($path, 'test-content', AdapterInterface::VISIBILITY_PUBLIC); $publicUrl = $gcs->url($path); // $publicUrl == 'https://storage.googleapis.com/my-application-bucket/test-laravel.txt';
开发
一些测试需要实际访问GCS。它们可以通过环境进行配置。
代码风格
您可以使用PHP-CS-Fixer格式化代码。
# install fixer
$ composer install --working-dir=tools/php-cs-fixer
# execute fixer
$ tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --allow-risky=yes
许可证
MIT