Laravel Google Cloud Storage 使用教程

Laravel Google Cloud Storage 使用教程

laravel-google-cloud-storageGoogle Cloud Storage filesystem driver for Laravel项目地址:https://gitcode.com/gh_mirrors/lar/laravel-google-cloud-storage

1. 项目的目录结构及介绍

spatie/laravel-google-cloud-storage
├── src
│   ├── GoogleCloudStorageServiceProvider.php
│   ├── GoogleCloudStorageAdapter.php
│   └── ...
├── tests
│   ├── GoogleCloudStorageAdapterTest.php
│   └── ...
├── .editorconfig
├── .gitattributes
├── .gitignore
├── .php_cs.dist
├── CHANGELOG.md
├── LICENSE.md
├── README.md
├── composer.json
├── phpunit.xml.dist
├── psalm.xml.dist
└── testbench.yaml

目录结构说明

  • src/: 包含项目的主要源代码文件,如服务提供者(GoogleCloudStorageServiceProvider.php)和适配器(GoogleCloudStorageAdapter.php)。
  • tests/: 包含项目的测试文件,如适配器测试(GoogleCloudStorageAdapterTest.php)。
  • .editorconfig, .gitattributes, .gitignore: 配置文件,用于代码格式化和版本控制。
  • .php_cs.dist: PHP CS Fixer 配置文件。
  • CHANGELOG.md, LICENSE.md, README.md: 项目文档,包括变更日志、许可证和使用说明。
  • composer.json: Composer 依赖管理文件。
  • phpunit.xml.dist, psalm.xml.dist: 测试和静态分析配置文件。
  • testbench.yaml: Laravel 测试工具配置文件。

2. 项目的启动文件介绍

GoogleCloudStorageServiceProvider.php

namespace Spatie\GoogleCloudStorage;

use Illuminate\Support\ServiceProvider;
use League\Flysystem\Filesystem;
use Google\Cloud\Storage\StorageClient;

class GoogleCloudStorageServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // 注册配置文件
        $this->publishes([
            __DIR__.'/../config/google-cloud-storage.php' => config_path('google-cloud-storage.php'),
        ]);

        // 注册文件系统适配器
        $this->app->bind('google-cloud-storage', function ($app) {
            $config = config('google-cloud-storage');
            $storageClient = new StorageClient([
                'projectId' => $config['project_id'],
                'keyFilePath' => $config['key_file_path'],
            ]);
            $bucket = $storageClient->bucket($config['bucket']);

            return new Filesystem(new GoogleCloudStorageAdapter($bucket));
        });
    }

    public function register()
    {
        // 合并配置文件
        $this->mergeConfigFrom(
            __DIR__.'/../config/google-cloud-storage.php', 'google-cloud-storage'
        );
    }
}

启动文件说明

  • GoogleCloudStorageServiceProvider.php: 服务提供者文件,负责注册配置文件和文件系统适配器。
  • boot() 方法: 发布配置文件并绑定文件系统适配器到应用程序。
  • register() 方法: 合并配置文件到应用程序配置中。

3. 项目的配置文件介绍

config/google-cloud-storage.php

return [
    'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'),
    'key_file_path' => env('GOOGLE_CLOUD_KEY_FILE', null),
    'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'your-bucket-name'),
];

配置文件说明

  • project_id: Google Cloud 项目 ID,从环境变量中读取或使用默认值。
  • key_file_path: 服务账号密钥文件路径,从环境变量中读取或使用默认值。
  • bucket: 存储桶名称,从环境变量中读取或使用默认值。

以上是 Laravel Google Cloud Storage 项目的目录结构、启动文件和配置文件的详细介绍。通过这些信息,您可以更好地理解和使用该项目。

laravel-google-cloud-storageGoogle Cloud Storage filesystem driver for Laravel项目地址:https://gitcode.com/gh_mirrors/lar/laravel-google-cloud-storage

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔡鸿烈Hope

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值