今天來看 Set Up an App Dev Environment on Google Cloud 這course 的 Cloud Storage: Qwik Start - CLI/SDK,用指令(Cloud Shell/CLI)完成「把檔案放到 GCS、取回、整理、公開」的整個基本流程, 我們開始 ~~ 。
gcloud storage
(新一代 CLI)與一次 gsutil
(舊工具)完成:建立 bucket → 上傳檔案 → 下載/複製/列出 → 設定公開讀取。在專案底下 建立bucket (buckets create
)
<YOUR_BUCKET_NAME>
為全域命名空間且對外可見,所以有嚴格的命名原則。
gcloud storage buckets create gs://<YOUR-BUCKET-NAME>
bucket 命名原則
- Do not include sensitive information in the bucket name, because the bucket namespace is global and publicly visible.
- Bucket names must contain only lowercase letters, numbers, dashes (-), underscores (_), and dots (.). Names containing dots require verification.
- Bucket names must start and end with a number or letter.
- Bucket names must contain 3 to 63 characters. Names containing dots can contain up to 222 characters, but each dot-separated component can be no longer than 63 characters.
- Bucket names cannot be represented as an IP address in dotted-decimal notation (for example, 192.168.5.4).
- Bucket names cannot begin with the "goog" prefix.
- Bucket names cannot contain "google" or close misspellings of "google".
- Also, for DNS compliance and future compatibility, you should not use underscores (_) or have a period adjacent to another period or dash. For example, ".." or "-." or ".-" are not valid in DNS names.
cp
上傳到 bucket (本機→雲端)Download this image (ada.jpg) into your bucket
curl https://upload.wikimedia.org/wikipedia/commons/thumb/a/a4/Ada_Lovelace_portrait.jpg/800px-Ada_Lovelace_portrait.jpg --output ada.jpg
Use the gcloud storage cp
command to upload the image from the location where you saved it to the bucket you created:
gcloud storage cp ada.jpg gs://YOUR-BUCKET-NAME
Remove the downloaded image:
rm ada.jpg
從 GCS 把剛剛上傳的檔案拉回本機 (雲端→本機)
gcloud storage cp
: download the image you stored in your bucket
gcloud storage cp -r gs://YOUR-BUCKET-NAME/ada.jpg .
gcloud storage cp
: create a folder called image-folder
and copy the image (ada.jpg) into it
gcloud storage cp gs://YOUR-BUCKET-NAME/ada.jpg gs://YOUR-BUCKET-NAME/image-folder/
補充:
在 GCS 裡的「資料夾」其實不是真正的檔案系統資料夾,而是物件名稱的字首(prefix)加上慣例的斜線/
。我們看到的資料夾,只是用「名字長得像路徑」來視覺化分組, 藉由命名來分組、列出、搬移與套規則,但底層沒有真正的目錄樹。
- e.g.
move “folder” (本質是改名=改物件 key)
gcloud storage mv gs://my-bucket/ada.jpg gs://my-bucket/image-folder/ada.jpg
(底層是 copy+delete 的概念,但mv
幫你處理)
- GCS 不會真的建立目錄結構,但因為名字裡包含 /,Cloud Console 與 ls 會把同一個前綴(例如 image-folder/)當成「一個資料夾」來展示。
可以看到 image-folder 對 Cloud Console 是 imgae ;image-folder/是資料夾![]()
列出 bucket 內有哪些物件或前綴
gcloud storage ls
: list the contents of the bucket
gcloud storage ls gs://YOUR-BUCKET-NAME
顯示物件細節(大小、更新時間、儲存級別等)
gcloud storage ls
: with the -l
flag to get some details about the image file you uploaded to your bucket
gcloud storage ls -l gs://YOUR-BUCKET-NAME/ada.jpg
把單一物件設成「任何人可讀取」的公開 URL。
安全觀念:只讓需要公開的特定物件公開;避免整桶公開。
gsutil acl ch
: grant all users read permission for the object stored in your bucket
gsutil acl ch -u AllUsers:R gs://YOUR-BUCKET-NAME/ada.jpg
-u
:針對「user / 單一主體」新增授權AllUsers
)。AllUsers:R
:主體 + 權限。R
=Readgs://.../ada.jpg
:目標是物件而非整個 bucket。小提醒:
R
/W
/O
是物件 ACL 的傳統權限碼:R
(Reader):可讀。W
(Writer):可寫(較少用在物件 ACL)。O
(Owner):擁有者,可改 ACL、刪除等。
Validate that your image is publicly available.
可以看到我們照片欄位的 Public link 旁出現 Copy URL 按鈕,複製URL, 開啟新的瀏覽器貼上可看到照片。
Remove this permission
刪除物件 ada.jpg
上 ACL 的 AllUsers
身份,因此不再「任何人」可讀
gsutil acl ch -d AllUsers gs://YOUR-BUCKET-NAME/ada.jpg
-d
:delete 指定主體在 ACL 中的授權。驗證方式: 在 Console 重新整理,Public access 會變成 Not public;若用原公開連結打開,通常會變成Access Denied
gcloud storage rm
:delete an object - the image file in your bucket
gcloud storage rm gs://YOUR-BUCKET-NAME/ada.jpg
ada.jpg
Refresh the console. The copy of the image file is no longer stored on Cloud Storage (though the copy you made in the image-folder/
folder still exists).