--- title: GCP Storage # 簡報的名稱 tags: GCP # 簡報的標籤 --- # GCP Storage  ## 1. 建立值區  ### 1.1 為值區命名([命名規則](https://cloud.google.com/storage/docs/naming?hl=zh-tw&_ga=2.34841378.-2061541235.1564627678&_gac=1.238814516.1584927681.EAIaIQobChMIgPz0oueo6AIVU7eWCh0wpwQ3EAAYASAAEgJ8DvD_BwE))  ### 1.2 選取資料的[儲存位置](https://cloud.google.com/storage/docs/locations?hl=zh-TW&_ga=2.47832100.-2061541235.1564627678&_gac=1.36965268.1584927681.EAIaIQobChMIgPz0oueo6AIVU7eWCh0wpwQ3EAAYASAAEgJ8DvD_BwE)  ### 1.3 為資料選擇預設儲存空間[級別](https://cloud.google.com/storage/docs/storage-classes?hl=zh-TW&_ga=2.218858266.-2061541235.1564627678&_gac=1.246539696.1584927681.EAIaIQobChMIgPz0oueo6AIVU7eWCh0wpwQ3EAAYASAAEgJ8DvD_BwE)  ### 1.4 選取如何控制物件的存取權  ### 1.5 進階設定  ## 2.上傳檔案 ### 2.1 上傳檔案  ### 2.2 上傳完成  ### 2.3 檢視上傳的圖片    ## 3.編輯項目 ### 編輯權限   ### 編輯中繼資料   ### 下載  ### 複製   ### 移動    ### 重新命名   ## 4.透過 GCP LoadBalcne 取得檔案 ### 4.1 建立 LB   ### 4.2 Http 負載平衡  ### 4.3 從網際網路到我的 VM  ### 4.4 後端設定   ### 4.5 前端設定  ### 4.6 [公開storage](https://cloud.google.com/storage/docs/access-control/making-data-public?hl=zh-tw)    ### 4.7 存取檔案  ## 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 ```  ### 5.6 公開bucket ```bash= gsutil iam ch allUsers:objectViewer gs://gsutil-bucket-tt ```  ### 5.7 存取檔案  ### 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   ### 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)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up