【问题标题】:Uploading a file to Google Cloud Storage with Node.js ('bucketName' has already been declared)使用 Node.js 将文件上传到 Google Cloud Storage('bucketName' 已声明)
【发布时间】:2019-03-09 15:08:09
【问题描述】:

我第一次在 AWS Lambda 中使用Google Cloud Storage,也在我的笔记本电脑上进行了本地操作。我使用

在 Lambda 中设置了环境变量

GOOGLE_APPLICATION_CREDENTIALS

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

'bucketName' has already been declared

我在 Google Cloud 中创建了存储桶并启用了 API。任何人都可以帮助修复代码吗? (反正大部分取自 Google Cloud 文档)

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】:

    bucketName 有冲突:

    • 您将其作为uploadFile 的参数:

      async function uploadFile(bucketName, filename) {
      
    • 你也在 uploadFile 中本地声明它:

      const bucketName = 'mynewbucket-saturday';
      

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

    【讨论】:

    猜你喜欢
    • 2023-03-13
    • 2019-04-10
    • 2018-10-31
    • 1970-01-01
    • 2021-01-05
    • 2018-12-23
    • 1970-01-01
    • 2017-04-02
    • 2019-11-07
    相关资源
    最近更新 更多