创建 GCP Storage 存储分区时指定 projectId

sin*_*tor 1 node.js google-cloud-storage google-cloud-platform gcloud

我正在尝试使用 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();
Run Code Online (Sandbox Code Playgroud)

Bri*_*ton 5

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

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

来源