--- title: GCP Storage # 簡報的名稱 tags: GCP # 簡報的標籤 --- # GCP Storage ![](https://i.imgur.com/aZc6jEV.png) ## 1. 建立值區 ![](https://i.imgur.com/IaHWQxy.png) ### 1.1 為值區命名([命名規則](https://cloud.google.com/storage/docs/naming?hl=zh-tw&_ga=2.34841378.-2061541235.1564627678&_gac=1.238814516.1584927681.EAIaIQobChMIgPz0oueo6AIVU7eWCh0wpwQ3EAAYASAAEgJ8DvD_BwE)) ![](https://i.imgur.com/WEJpAQw.png) ### 1.2 選取資料的[儲存位置](https://cloud.google.com/storage/docs/locations?hl=zh-TW&_ga=2.47832100.-2061541235.1564627678&_gac=1.36965268.1584927681.EAIaIQobChMIgPz0oueo6AIVU7eWCh0wpwQ3EAAYASAAEgJ8DvD_BwE) ![](https://i.imgur.com/ZI0MJle.png) ### 1.3 為資料選擇預設儲存空間[級別](https://cloud.google.com/storage/docs/storage-classes?hl=zh-TW&_ga=2.218858266.-2061541235.1564627678&_gac=1.246539696.1584927681.EAIaIQobChMIgPz0oueo6AIVU7eWCh0wpwQ3EAAYASAAEgJ8DvD_BwE) ![](https://i.imgur.com/B6FUIWo.png) ### 1.4 選取如何控制物件的存取權 ![](https://i.imgur.com/a0VKb2V.png) ### 1.5 進階設定 ![](https://i.imgur.com/yc8bQwZ.png) ## 2.上傳檔案 ### 2.1 上傳檔案 ![](https://i.imgur.com/jQGC7QZ.png) ### 2.2 上傳完成 ![](https://i.imgur.com/LvBzwWq.png) ### 2.3 檢視上傳的圖片 ![](https://i.imgur.com/9o7FsiN.png) ![](https://i.imgur.com/YsNg4aP.png) ![](https://i.imgur.com/JTs59yY.png) ## 3.編輯項目 ### 編輯權限 ![](https://i.imgur.com/pFIfJv6.png) ![](https://i.imgur.com/HVzWFmU.png) ### 編輯中繼資料 ![](https://i.imgur.com/0kjR7zn.png) ![](https://i.imgur.com/WRYMwEN.png) ### 下載 ![](https://i.imgur.com/XTzECcz.png) ### 複製 ![](https://i.imgur.com/jAbDamS.png) ![](https://i.imgur.com/rbHSTsn.png) ### 移動 ![](https://i.imgur.com/ihBvahc.png) ![](https://i.imgur.com/v3jqLRD.png) ![](https://i.imgur.com/EcahJEM.png) ### 重新命名 ![](https://i.imgur.com/77t2RUp.png) ![](https://i.imgur.com/Bmx7rPE.png) ## 4.透過 GCP LoadBalcne 取得檔案 ### 4.1 建立 LB ![](https://i.imgur.com/R2H3aBh.png) ![](https://i.imgur.com/vJwaJSw.png) ### 4.2 Http 負載平衡 ![](https://i.imgur.com/bxoOPrt.png) ### 4.3 從網際網路到我的 VM ![](https://i.imgur.com/Mn2xeia.png) ### 4.4 後端設定 ![](https://i.imgur.com/MdUZ6bQ.png) ![](https://i.imgur.com/phY05ym.png) ### 4.5 前端設定 ![](https://i.imgur.com/7CsZVTx.png) ### 4.6 [公開storage](https://cloud.google.com/storage/docs/access-control/making-data-public?hl=zh-tw) ![](https://i.imgur.com/LOBgpmW.png) ![](https://i.imgur.com/LNxU0ht.png) ![](https://i.imgur.com/ecw33js.png) ### 4.7 存取檔案 ![](https://i.imgur.com/BWKtpy3.png) ## 5.使用 [gsutil](https://cloud.google.com/storage/docs/quickstart-gsutil?hl=zh-tw) 操作GCS ### 5.1 建立 bucket ```bash= gsutil mb -b on -l asia gs://gsutil-bucket-tt/ ``` > bucket的名稱不可以重複 ### 5.2 上傳檔案 ```bash= gsutil cp ./tmp/avatar.png gs://gsutil-bucket-tt ``` ### 5.3 下載檔案 ```bash= gsutil cp gs://gsutil-bucket-tt/avatar.png ./tmp/avatar2.png ``` ### 5.4 查看storage檔案列表 ```bash= gsutil ls gs://gsutil-bucket-tt ``` ### 5.5 查看單一檔案明細 ```bash= gsutil ls -l gs://gsutil-bucket-tt/avatar.png ``` ![](https://i.imgur.com/7xoeB1t.png) ### 5.6 公開bucket ```bash= gsutil iam ch allUsers:objectViewer gs://gsutil-bucket-tt ``` ![](https://i.imgur.com/TqYnI22.png) ### 5.7 存取檔案 ![](https://i.imgur.com/cbfyXlO.png) ### 5.8 版號 #### 檢查是否啟用了對象版本控制 ``` gsutil versioning get gs://[BUCKET_NAME] ``` #### 啟用對象版本控制 ``` gsutil versioning set on gs://[BUCKET_NAME] ``` #### 禁用對象版本控制 ``` gsutil versioning set off gs://[BUCKET_NAME] ``` #### 列出非當前對象版本 ``` gsutil ls -a gs:// [BUCKET_NAME] ``` ## 6. 使用 [Node js](https://googleapis.dev/nodejs/storage/latest/) 上傳檔案至GCS ### 6.1 安裝套件 ```bash= npm install --save @google-cloud/storage ``` ### 6.2 客戶端程式 ```javascript= const { Storage } = require('@google-cloud/storage'); const storage = new Storage(); async function uploadFile() { const bucketName = 'storage-tt'; const filename = 'test.txt'; const re = await storage.bucket(bucketName).upload(filename, { // Support for HTTP requests made with `Accept-Encoding: gzip` gzip: true, // By setting the option `destination`, you can change the name of the // object you are uploading to a bucket. metadata: { // Enable long-lived HTTP caching headers // Use only if the contents of the file will never change // (If the contents will change, use cacheControl: 'no-cache') cacheControl: 'public, max-age=31536000', }, }); console.log(`${filename} uploaded to ${bucketName}.`); } uploadFile().catch(console.error); ``` ### 6.3 產生 服務帳戶並下載 key.json ![](https://i.imgur.com/9pRg40N.png) ![](https://i.imgur.com/pkZHb1u.png) ### 6.4 測試上傳 ```bash= export GOOGLE_APPLICATION_CREDENTIALS=\"key.json\" && node index.js ``` # Reference * [Cloud Storage](https://cloud.google.com/storage?hl=zh-tw) * [Cloud Storage documentation](https://cloud.google.com/storage/docs?hl=zh-tw) * [Making data public]( https://cloud.google.com/storage/docs/access-control/making-data-public?hl=zh-tw) * [GCS 定價](https://cloud.google.com/storage/pricing?hl=zh-tw)