node.js - 使用 NodeJS 从 GCP 的存储中下载对象

标签 node.js google-cloud-platform google-cloud-storage google-iam

我正在使用 @google-cloud/storage从 Node 应用程序访问 Google Cloud Storage 存储桶内的对象,但我无法使其工作。

我已经在 GCP 的控制台上创建了一个服务帐户,并为其分配了 Storage Admin 角色,但是当我尝试获取文件时,我收到了这条消息:

service-account-user@my-project-5411148.iam.gserviceaccount.com does not have storage.objects.get access to my-bucket-45826813215/some-object.

查看存储桶的权限选项卡,我可以看到服务帐户列在那里,带有继承注释,我没有为对象设置任何特定权限。

我的代码是这样的:

const { Storage } = require('@google-cloud/storage');
const config = require('./config');
const storage = new Storage({ 'keyFilename': config.configFullPath('gcloud') });

const privateBucket = storage.bucket('my-bucket-45826813215'); 

let objectDownload = async (filename) => {
    let file = privateBucket.file(filename);
    let result = await file.download();
    return result;
}

objectDownload('some-object')
    .then(() => {
        console.log('Done');
    })
    .catch((err) => {
        console.log(err.message);
    });

对我做错了什么有什么想法吗?

最佳答案

我可以使用 Storage Admin Role 下载文件。以下是我遵循的过程

<强>1。创建项目 enter image description here

<强>2。转到 IAM 并选择服务帐户

enter image description here

<强>3。选择创建服务帐户 enter image description here

<强>4。为服务帐户选择角色 enter image description here

<强>5。创建 key

enter image description here enter image description here enter image description here

下面是工作代码:

const path = require('path');
const {Storage} = require('@google-cloud/storage');


async function test() {

const serviceKey = path.join(__dirname, './keys.json')


const storageConf = {keyFilename:serviceKey}

const storage = new Storage(storageConf)

const downlaodOptions = {
      destination: __dirname+'/test.jpg'
    };

    try {
    let res =await storage
      .bucket('storage1232020')
      .file('test.jpg')
      .download(downlaodOptions); 
   }
   catch(err){
    console.log(err)
   }

}

test()

注意:确保

  1. 在项目下创建服务帐户和存储桶。例如,我为项目 storage-dem01232020

    创建了一个存储桶和服务帐户
  2. 您正在将 key 正确地传递给代码

文件下载方式

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const myBucket = storage.bucket('my-bucket');

const file = myBucket.file('my-file');

//-
// Download a file into memory. The contents will be available as the
second
// argument in the demonstration below, `contents`.
//-
file.download(function(err, contents) {});

//-
// Download a file to a local destination.
//-
file.download({
  destination: '/Users/me/Desktop/file-backup.txt'
}, function(err) {});

//-
// If the callback is omitted, we'll return a Promise.
//-
file.download().then(function(data) {
  const contents = data[0];
});

更多详情请引用以下链接: https://googleapis.dev/nodejs/storage/latest/File.html#download

关于node.js - 使用 NodeJS 从 GCP 的存储中下载对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59865677/

相关文章:

javascript - 如何在 TypeScript 的监视模式下运行 Jasmine 测试

node.js - 为什么 webrtc blob 地址位于信令服务器上?

python - 使用 Google App Engine 的 Google Cloud Storage 签名 URL

r - 如何使用 bigrquery 中的 bq_load_table() 将数据从谷歌云存储加载到 R 中?

node.js - Groovy Script for Nodejs 插件无法在 Jenkins 上配置

node.js - 使用 Node.js 执行 Sync Get 请求

google-app-engine - 如何在Google云平台的VM实例上运行python脚本

google-cloud-storage - Google Cloud Storage 中的 GET 操作有何不同?

google-cloud-platform - 我在哪里可以获得实际 Compute Engine 镜像名称的列表?

hadoop - 无法在谷歌云上部署 hadoop 集群