使用Node.js将文件上传到Google云端存储('bucketName'已经声明)

问题描述 投票:1回答:1

我正在使用AWS Lambda中的Google Cloud Storage和我的笔记本电脑本地第一次玩。我使用Lambda设置环境变量

GOOGLE_APPLICATION_CREDENTIALS

但是,当我尝试在zip中上传demo.txt文件时,我得到了

'bucketName' has already been declared

我在Google Cloud中创建了存储桶,并启用了API。任何人都可以帮助修复代码吗? (主要取自谷歌云文档)

async function uploadFile(bucketName, filename) {
  // [START storage_upload_file]
  // Imports the Google Cloud client library
  const { Storage } = require('@google-cloud/storage');

  // Your Google Cloud Platform project ID
  const projectId = 'apple-ration-27434';

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

  var bucketName = 'mybucket-saturday';
  var filename = 'demo.txt';

  // Uploads a local file to the bucket
  await storage.bucket(bucketName).upload(filename, {
    // Support for HTTP requests made with `Accept-Encoding: gzip`
    gzip: true,
    metadata: {
      // Enable long-lived HTTP caching headers
      // Use only if the contents of the file will never change
      // (If the contents will change, use cacheControl: 'no-cache')
      cacheControl: 'public, max-age=31536000',
    },
  });

  console.log(`${filename} uploaded to ${bucketName}.`);
  // [END storage_upload_file]
}
javascript node.js google-cloud-platform aws-lambda google-cloud-storage
1个回答
1
投票

你和bucketName有冲突:

  • 你得到它作为uploadFile的论据: async function uploadFile(bucketName, filename) {
  • 你也在uploadFile当地宣布它: const bucketName = 'mynewbucket-saturday';

您只需选择一种指定存储桶名称的方法,然后删除另一种方法。

© www.soinside.com 2019 - 2024. All rights reserved.