Node.js 使用NodeJS本地上传文件到谷歌云存储

Node.js 使用NodeJS本地上传文件到谷歌云存储,node.js,google-cloud-storage,Node.js,Google Cloud Storage,我正在尝试使用我的服务帐户在我的机器上本地使用简单代码将图像上载到Google云存储: 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('

我正在尝试使用我的服务帐户在我的机器上本地使用简单代码将图像上载到Google云存储:

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);
         ^
但是,我得到下面的错误消息。谷歌云API应该只在部署到AppEngine上时才起作用,还是我在这里做错了什么?我能够使用相同的服务帐户让GoogleVisionAPI在本地工作

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)
错误:ApiError:禁止
在/Users/user/googlecloud/upload/upload.js:10:20
在Pumpify。(/Users/user/node_modules/@google cloud/storage/src/bucket.js:1218:9)
在emitOne(events.js:101:20)
在Pumpify.emit(events.js:188:7)
在Pumpify.Duplexify.\u destroy(/Users/user/node\u modules/Duplexify/index.js:184:15)
at/Users/user/node_modules/duplexify/index.js:175:10
at_combinedTickCallback(内部/流程/下一步勾选js:67:7)
在进程中。_tick回调(内部/process/next_tick.js:98:9)

我只是在云控制台中发现了一个错误,我在Overdrivr的服务帐户指出了这一点

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

  • 创建服务帐户
  • 转到IAM面板
  • 编辑刚刚创建的服务帐户的角色。选择云存储并使其成为一个完整的管理员,但最好的方法是根据您的需要使其成为存储对象的创建者或读取器

  • 这个问题很老了,但您是否授权服务帐户API访问云存储?事实上:)这只是谷歌云控制台中服务帐户的一个错误。我已经给出了一个更详细的答案,如果适合您,请随意接受。干杯