spatie/flysystem-google-cloud-storage

Google Cloud Storage 的 Flysystem 适配器

1.1.2 2023-07-20 13:21 UTC

This package is auto-updated.

Last update: 2024-09-20 15:56:25 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

此包包含用于 Google Cloud StorageFlysystem v1 驱动程序。

(如果您需要在 Flysystem v2 或更高版本(或 Laravel 9)上使用 Google Cloud Storage 支持,则此包不适合您)

注意

此包是从 superbalist/flysystem-google-cloud-storage 分支出来的。更改包括

  • PHP 8 仅支持
  • 合并了 Superbalist 包中的随机开放 PR

支持我们

我们投入了大量资源来创建 一流的开放源代码包 。您可以通过 购买我们的付费产品之一 来支持我们。

我们非常感谢您从家乡寄给我们一张明信片,说明您正在使用我们的哪个包。您可以在 我们的联系页面 上找到我们的地址。我们将在 我们的虚拟明信片墙 上发布所有收到的明信片。

安装

您可以通过 composer 安装此包

composer require spatie/flysystem-google-cloud-storage

身份验证

在您可以使用此包之前,您需要通过 Google 进行身份验证。当可能时,凭据将由 Google Cloud Client 自动加载。

  1. 客户端首先查看 GOOGLE_APPLICATION_CREDENTIALS 环境变量。您可以使用 putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json'); 来设置凭据文件的位置。
  2. 客户端将在以下路径中查找凭据文件
    • Windows: %APPDATA%/gcloud/application_default_credentials.json
    • MacOS/Unix: $HOME/.config/gcloud/application_default_credentials.json
  3. 如果在 Google App Engine、Google Compute Engine 或 GKE 上运行,则将使用与应用程序、实例或集群关联的内置服务帐户。

在 Kubernetes 集群中使用此功能?请参阅 Workload Identity

用法

以下示例显示了您如何调用各种函数来操作 Google Cloud 上的文件。

use Google\Cloud\Storage\StorageClient;
use League\Flysystem\Filesystem;
use Spatie\GoogleCloudStorageAdapter\GoogleCloudStorageAdapter;

$storageClient = new StorageClient([
    'projectId' => 'your-project-id',
    // The credentials can manually be specified by passing in a keyFilePath.
    // 'keyFilePath' => '/path/to/service-account.json',
]);

$bucket = $storageClient->bucket('your-bucket-name');

$adapter = new GoogleCloudStorageAdapter($storageClient, $bucket);

$filesystem = new Filesystem($adapter);

$filesystem->write('path/to/file.txt', 'contents');
$filesystem->update('path/to/file.txt', 'new contents');
$contents = $filesystem->read('path/to/file.txt');
$exists = $filesystem->has('path/to/file.txt');
$filesystem->delete('path/to/file.txt');
$filesystem->rename('filename.txt', 'newname.txt');
$filesystem->copy('filename.txt', 'duplicate.txt');
$filesystem->deleteDir('path/to/directory');

有关完整功能列表,请参阅 https://flysystem.thephpleague.com/v1/docs/usage/filesystem-api/

使用自定义存储 URI

您可以配置此适配器以使用自定义存储 URI。有关为 Google Cloud Storage 配置自定义域的更多信息,请参阅此处

使用自定义存储 URI 时,将不会将存储桶名称添加到文件路径之前

use Google\Cloud\Storage\StorageClient;
use League\Flysystem\Filesystem;
use Spatie\GoogleCloudStorageAdapter\GoogleCloudStorageAdapter;

$storageClient = new StorageClient([
    'projectId' => 'your-project-id',
]);
$bucket = $storageClient->bucket('your-bucket-name');
$adapter = new GoogleCloudStorageAdapter($storageClient, $bucket);

// URI defaults to "https://storage.googleapis.com":
$filesystem = new Filesystem($adapter);
$filesystem->getUrl('path/to/file.txt');
// "https://storage.googleapis.com/your-bucket-name/path/to/file.txt"

// Using custom storage uri:
$adapter->setStorageApiUri('http://example.com');
$filesystem = new Filesystem($adapter);
$filesystem->getUrl('path/to/file.txt');
// "http://example.com/path/to/file.txt"

// You can also prefix the file path if needed:
$adapter->setStorageApiUri('http://example.com');
$adapter->setPathPrefix('extra-folder/another-folder/');
$filesystem = new Filesystem($adapter);
$filesystem->getUrl('path/to/file.txt');
// "http://example.com/extra-folder/another-folder/path/to/file.txt"

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

安全漏洞

请查看我们的安全策略如何报告安全漏洞

致谢

许可证

MIT 许可证(MIT)。有关更多信息,请参阅许可证文件