2016年7月5日 星期二

google storage nodejs 上傳教學

最近在整合 node server 檔案上傳到google cloud storage , 發現可以的工具只有 google 官方推出gcloud 工具. 但缺少和前端 檔案上傳 和 ACL 設定, 所以為大家介紹第三方套件, 針對上傳檔案到 google cloud storage 而設計 ,

https://www.npmjs.com/package/gcloud-storage-api



  • 提供3個google cloud storage 檔案的function , 

前2個上傳function, uploadLocalFile 和 uploadBuffer  是以 formidable 方式 獲得multipart 檔案和 json:

  • googleAPI.uploadLocalFile(BUCKET_NAME, fileName, fileLocalPath)
  • googleAPI.uploadBuffer(BUCKET_NAME, fileName, buffer)
  • googleAPI.deleteStorageFile(url)



  • 安裝:

$ npm install --save gcloud-storage-api



  • 使用:

service-key 和 account  的設定可以參考官方教學:

https://www.npmjs.com/package/gcloud


1- 到google cloud console 中新增 Credentials, 選 service account key



2- key 類型選 JSON , 檔案下載請放到nodejs 的目錄底下


3- 成功下載的service-key.json 

4- service-key.json 的內容如下
{
  "private_key_id": "******",
  "private_key": "******",
  "client_email": "******",
  "client_id": "******",
  "type": "service_account"
}

5- node_modules/gcloud-upload-api 有一個測試用的html 檔案, 可以用作測試












沒有留言:

張貼留言