[GCP]GCP Cloud Storage 踩坑日記 - 無法上傳
繼之前開發laravel設定cloud storage 以後
久違的沒有碰laravel了(被打)
這陣子因緣際會重回之前短暫碰的laravel懷抱XD
剛好跟身邊的大大踩到一個算是有點小無語點坑
趁還記得的時候把這些筆記寫下來
前情摘要
- 環境
- php 8.4
- laravel 12
- 套件
https://github.com/spatie/laravel-google-cloud-storage
- 遇到的情況
- 使用Google 原生的sdk可以進行操作上傳下載
- 使用Laravel File Systems設定以後只能進行下載
問題拆解
先來做個簡單的問題拆解
- 可以先排除連線問題
- google sdk可以完全操作功能
一開始同事提出這個問題的時候,大概先往權限問題去通靈XD
GCP那邊設定都是同一個的話,那可能會是使用這邊驗證的問題?
但這題最麻煩的是,發生的當下看不到錯誤訊息套件只回傳了個false
(這個故事告訴我們,自己在開發的時候,error 如果沒有任何資訊會想殺死當初的自己XD)
解方
最後在前人栽樹的文章中找到蛛絲馬跡,這邊要更正一下關於該不該丟error message的部分,這個部分真的就是看情境,沒有絕對的答案,就像下面連結有人分享的
『以使用者端來說,return false會是比較合適的處理』 https://www.facebook.com/groups/laravel.tw/posts/7663622883706694
laravel官方文件有個小地方有提到可以把throw exception 這個設定打開
# config/filesystem.php
'public' => [
'driver' => 'local',
// ...
'throw' => true,
],
當我們有 throw以後就比較好處理啦XD
回到套件設定(我先把其他設定資訊拔掉比較好看XD)
這一段其實就是說要把這個參數設定到那個class才能夠上傳~
'gcs' => [
'driver' => 'gcs',
'visibility_handler' => null,
// optional: set to \League\Flysystem\GoogleCloudStorage\UniformBucketLevelAccessVisibility::class to enable uniform bucket level access
],
轉一下套件readme提到的部分
簡單來說是GCP是可以針對bucket層級設定權限,
Cannot insert legacy ACL for an object when uniform bucket-level access is enabled
當你啟用了bucket管理權限,就不能啟用ACL物件做這個動作,這時候就需要另外指定Bucket Level的權限,所以套件說明當你需要上傳檔案到使用Bucket 層級管理權限的storage時,請加上以下設定
'visibility_handler' => \League\Flysystem\GoogleCloudStorage\UniformBucketLevelAccessVisibility::class
- 節錄自套件Readme說明
Uniform bucket-level access
Google Cloud Storage allows setting permissions at the bucket level i.e. "Uniform bucket-level access".
Initially, the error "Cannot insert legacy ACL for an object when uniform bucket-level access is enabled" is observed.
When uploading files to such buckets ensure the visibility_handler within the configuration file is set as follows:
'visibility_handler' => \League\Flysystem\GoogleCloudStorage\UniformBucketLevelAccessVisibility::class,
Please see https://cloud.google.com/storage/docs/access-control/signed-urls and https://laravel.com/docs/6.x/filesystem for more info.