ntsoluzioninformatiche / laravel-google-cloud-storage
为Laravel提供的Google Cloud Storage文件系统
此包的规范存储库似乎已消失,因此该包已被冻结。
Requires
- php: >=5.5.9
- illuminate/filesystem: ^5.1|^5.2|^5.3
- illuminate/support: ^5.1|^5.2|^5.3
- superbalist/flysystem-google-storage: >=3.0 <8.0
Requires (Dev)
- phpunit/phpunit: >=4.8
This package is not auto-updated.
Last update: 2019-10-28 16:08:42 UTC
README
为Laravel提供的Google Cloud Storage文件系统。
此包是flysystem-google-storage的包装器,将其作为可用的存储磁盘桥接到Laravel中。
安装
composer require ntsoluzioninformatiche/laravel-google-cloud-storage
Laravel < 5.5
在app.php中注册服务提供者
'providers' => [ // ... Ntsoluzioninformatiche\LaravelGoogleCloudStorage\GoogleCloudStorageServiceProvider::class, ]
将新的磁盘添加到您的filesystems.php
配置中
'gcs' => [ 'driver' => 'gcs', 'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'), 'key_file' => env('GOOGLE_CLOUD_KEY_FILE', null), // optional: /path/to/service-account.json 'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'your-bucket'), 'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null), // optional: /default/path/to/apply/in/bucket 'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // see: Public URLs below 'visibility' => 'public', // optional: public|private ],
认证
Google客户端使用几种方法来确定它应该如何与Google API进行认证。
-
如果您在磁盘配置中指定了
key_file
,则将使用该json凭证文件。 -
如果设置了环境变量
GOOGLE_APPLICATION_CREDENTIALS
,则将使用它。putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
-
然后它将尝试从“已知路径”加载密钥文件
- Windows: %APPDATA%/gcloud/application_default_credentials.json
- 其他: $HOME/.config/gcloud/application_default_credentials.json
-
如果在Google App Engine上运行,将使用与应用程序关联的内置服务账户。
-
如果在Google Compute Engine上运行,将使用与虚拟机实例关联的内置服务账户。
公开URL
适配器实现了getUrl($path)
方法,该方法返回文件的公开URL。
注意:此方法适用于Laravel 5.2及更高版本。如果在5.1上使用,将引发异常。
$disk = Storage::disk('gcs'); $url = $disk->url('folder/my_file.txt'); >>> http://storage.googleapis.com/bucket-name/folder/my_file.txt
如果您在配置中配置了path_prefix
$disk = Storage::disk('gcs'); $url = $disk->url('folder/my_file.txt'); >>> http://storage.googleapis.com/bucket-name/path-prefix/folder/my_file.txt
如果您在配置中配置了自定义的storage_api_uri
$disk = Storage::disk('gcs'); $url = $disk->url('folder/my_file.txt'); >>> http://your-custom-domain.com/bucket-name/path-prefix/folder/my_file.txt
对于自定义域(存储API URI),您需要配置一个CNAME DNS条目,指向storage.googleapis.com
。
请参阅https://cloud.google.com/storage/docs/xml-api/reference-uris#cname获取进一步说明。
使用方法
$disk = Storage::disk('gcs'); // create a file $disk->put('avatars/1', $fileContents); // check if a file exists $exists = $disk->exists('file.jpg'); // get file modification date $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 url to file $url = $disk->url('folder/my_file.txt'); // See https://laravel.net.cn/docs/5.6/filesystem for full list of available functionality
您还可以更改单个文件的可见性
$disk = Storage::disk('gcs'); // during a file creation $disk->put('avatars/1', $fileContents, 'private'); //after creation $disk->setVisibility('avatars/1', 'public');