【问题标题】:Uploading file to Google Cloud storage locally using NodeJS使用 NodeJS 将文件上传到本地 Google Cloud 存储
【发布时间】:2017-05-20 23:41:48
【问题描述】:

我正在尝试使用我的服务帐户在我的机器上本地使用简单代码将图像上传到 Google Cloud Storage:

const storage = require('@google-cloud/storage');
const fs = require('fs');
const gcs = storage({
    projectId: 'ID',
    keyFilename: 'KEYNAME'
});
var bucket = gcs.bucket('BUCKET NAME');

bucket.upload('hiking-image.jpg', function(err, file) {
    if (err) throw new Error(err);
});

if (err) throw new Error(err);
         ^

但是,我收到以下错误消息。 Google Cloud API 是否应该仅在部署在 App Engine 上时才能工作,还是我在这里做错了什么?我能够使用同一个服务帐户让 Google Vision API 在本地工作。

Error: ApiError: Forbidden
    at /Users/user/google-cloud/upload/upload.js:10:20
    at Pumpify.<anonymous> (/Users/user/node_modules/@google-cloud/storage/src/bucket.js:1218:9)
    at emitOne (events.js:101:20)
    at Pumpify.emit (events.js:188:7)
    at Pumpify.Duplexify._destroy (/Users/user/node_modules/duplexify/index.js:184:15)
    at /Users/user/node_modules/duplexify/index.js:175:10
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

【问题讨论】:

  • 问题已经很老了,但是您是否授权服务帐户API访问云存储?
  • 确实 :) 这只是 Google Cloud Console 中服务帐户的错误
  • 我已经做了一个更有文档的答案,如果它适合你,请随时接受。干杯!

标签: node.js google-cloud-storage


【解决方案1】:

您需要为您的服务帐号赋予适当的角色。转到谷歌云控制台,菜单“IAM 和管理员”

  1. 创建服务帐号
  2. 转到 IAM 面板
  3. 编辑您刚刚创建的服务帐户的角色。选择云存储并将其设置为完全管理员,但最好是根据您的需要将其设置为存储对象的创建者或读取者。

【讨论】:

    【解决方案2】:

    指出我在 Overdrivr 的服务帐户在云控制台中出现错误

    【讨论】:

      猜你喜欢
      • 2016-12-07
      • 2019-08-30
      • 2021-09-07
      • 2022-01-26
      • 1970-01-01
      • 2019-10-29
      • 2018-08-01
      • 1970-01-01
      • 2016-02-03
      相关资源
      最近更新 更多