使用 NodeJS 从 GCP 的存储中下载对象
Download object from GCP's Storage using NodeJS
我正在使用 @google-cloud/storage 从节点应用程序访问 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.
查看存储桶的权限选项卡,我可以看到服务帐户列在那里带有 inherited 注释,我还没有为对象设置任何特定权限。
我的代码如下所示:
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.创建项目
2。转到 IAM 和 select 服务帐户
3。 Select 创建服务帐户
4。 select 服务帐户角色
5.创建密钥
下面是工作代码:
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()
注意:确保
在项目下创建服务帐户和存储桶。例如,我为项目 storage-dem01232020
创建了一个存储桶和服务帐户
您正在正确地将密钥传递给代码
文件下载方式
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];
});
请参阅下方 link 了解更多详情:
https://googleapis.dev/nodejs/storage/latest/File.html#download
我正在使用 @google-cloud/storage 从节点应用程序访问 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.
查看存储桶的权限选项卡,我可以看到服务帐户列在那里带有 inherited 注释,我还没有为对象设置任何特定权限。
我的代码如下所示:
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.创建项目
2。转到 IAM 和 select 服务帐户
3。 Select 创建服务帐户
4。 select 服务帐户角色
5.创建密钥
下面是工作代码:
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()
注意:确保
在项目下创建服务帐户和存储桶。例如,我为项目 storage-dem01232020
创建了一个存储桶和服务帐户您正在正确地将密钥传递给代码
文件下载方式
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];
});
请参阅下方 link 了解更多详情: https://googleapis.dev/nodejs/storage/latest/File.html#download