在Node.js上创建存储分区的问题

时间:2018-09-21 07:40:14

标签: google-cloud-platform google-cloud-storage

我已经在GCP IAM服务中创建了一个服务帐户,然后,我为该服务帐户授予了权限storage.buckets.createstorage.buckets.getstorage.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);

1 个答案:

答案 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%确保您传递的密钥文件是正确的。