【问题标题】:Specifying projectId when creating GCP Storage bucket创建 GCP 存储桶时指定 projectId
【发布时间】:2021-05-10 01:36:56
【问题描述】:

我正在尝试使用 Node.js 库创建一个 GCP 存储桶。 我一直在使用这里的步骤: https://cloud.google.com/storage/docs/creating-buckets#storage-create-bucket-nodejs

下面粘贴的代码。 挑战在于我的存储桶一直在错误的项目中创建。我的项目在我的 gcloud cli 中设置,在我的节点环境中设置,在我的脚本中设置。 有没有办法在传递给库的 createBucket 函数的值中设置项目?

/**
 * TODO(developer): Uncomment the following line before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const storageClass = 'Name of a storage class, e.g. coldline';
// const location = 'Name of a location, e.g. ASIA';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function createBucketWithStorageClassAndLocation() {
  // For default values see: https://cloud.google.com/storage/docs/locations and
  // https://cloud.google.com/storage/docs/storage-classes

  const [bucket] = await storage.createBucket(bucketName, {
    location,
    [storageClass]: true,
  });

  console.log(
    `${bucket.name} created with ${storageClass} class in ${location}.`
  );
}

createBucketWithStorageClassAndLocation();

【问题讨论】:

    标签: node.js google-cloud-platform google-cloud-storage gcloud


    【解决方案1】:

    你可以在初始化Storage类的时候指定projectId:

    const storage = new Storage({
      projectId: 'my-project-id',
      keyFilename: '/path/to/keyfile.json'
    });
    

    Source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      • 2020-10-24
      相关资源
      最近更新 更多