Gracie
Gracie

Reputation: 956

Uploading a file to Google Cloud Storage with Node.js ('bucketName' has already been declared)

I am having my first play with Google Cloud Storage from within AWS Lambda and also locally on my laptop. I have the Environment Variables set in Lambda using

GOOGLE_APPLICATION_CREDENTIALS

However, when I try and upload the demo.txt file within the zip I get

'bucketName' has already been declared

I have created the bucket in Google Cloud and also Enabled the API. Can anyone help fix the code? (mostly taken from Google Cloud docs anyway)

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]
}

Upvotes: 1

Views: 992

Answers (1)

Dan Cornilescu
Dan Cornilescu

Reputation: 39814

You have a conflict for bucketName:

  • you're getting it as argument to uploadFile:

    async function uploadFile(bucketName, filename) {
    
  • you're also declaring it locally inside uploadFile:

    const bucketName = 'mynewbucket-saturday';
    

You need to pick only one method of specifying the bucket name and drop the other.

Upvotes: 1

Related Questions