我已经在GCP IAM服务中创建了一个服务帐户,然后,我为该服务帐户授予了权限storage.buckets.create
,storage.buckets.get
和storage.buckets.list
。之后,我设法授权了凭据。
但是当我尝试创建存储桶时,出现以下错误:
@ cloud-functions-intro.iam.gserviceaccount.com没有对项目的storage.buckets.create访问权限
代码如下:
const projectId = 'cloud-functions-intro';
const bucketName = `${projectId}-packaging-data`;
const storage = new Storage({
projectId: projectId,
keyFilename: 'my_credentials.json'
});
const bucket = storage.bucket(bucketName);
答案 0 :(得分:1)
我自己进行了测试,并且能够按照documentation的代码段进行测试,但是在执行操作时指向密钥文件:
const {Storage} = require('@google-cloud/storage');
const projectId = 'project_id';
const storage = new Storage({
projectId: projectId,
keyFilename: 'credentials.json'
});
const bucketName = 'bucket_name';
storage
.createBucket(bucketName)
.then(() => {
console.log(`Bucket ${bucketName} created.`);
})
.catch(err => {
console.error('ERROR:', err);
});
这应该有效。如果您仍然遇到相同的错误,那么我的建议是为该服务帐户提供一个新密钥,并确保100%确保您传递的密钥文件是正确的。