1

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]
}
Gracie
  • 896
  • 5
  • 14
  • 34
  • @Dan Cornilescu, can you check this https://stackoverflow.com/questions/74081978/error-creating-gcs-signed-post-policy-for-uploading-file – Husain Batatawala Oct 16 '22 at 22:19

1 Answers1

1

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.

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
  • Thanks Dan, I have got the AWS Lambda function working using your comments but when I try and run it locally as per the Google docs in the link it doesn't work. https://github.com/googleapis/nodejs-storage/blob/master/samples/files.js – Gracie Mar 09 '19 at 18:04
  • 1
    You should ask a separate question. But be specific, "doesn't work" isn't useful. See https://stackoverflow.com/help/mcve. – Dan Cornilescu Mar 09 '19 at 22:04