Node.js 创建GCP存储桶时指定projectId

Node.js 创建GCP存储桶时指定projectId,node.js,google-cloud-platform,google-cloud-storage,gcloud,Node.js,Google Cloud Platform,Google Cloud Storage,Gcloud,我正在尝试使用Node.js库创建GCP存储桶。 我一直在使用以下步骤: 代码粘贴在下面。 挑战在于我的bucket总是在错误的项目中创建。我的项目在gcloud cli中设置,在节点环境中设置,在脚本中设置。 是否有方法在传递给库的createBucket函数的值中设置项目 /** * TODO(developer): Uncomment the following line before running the sample. */ // const bucketName = 'Nam

我正在尝试使用Node.js库创建GCP存储桶。 我一直在使用以下步骤:

代码粘贴在下面。 挑战在于我的bucket总是在错误的项目中创建。我的项目在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();

您可以在初始化存储类时指定projectId:

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

初始化存储类时,可以指定projectId:

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