Mohammed Mudassir
Mohammed Mudassir

Reputation: 390

How do i upload an CSV file to Google Cloud Storage, using nodeJS

I need to upload and share my documents using Google Cloud and nodeJS but I'm not sure how to do it exactly.

Upvotes: 0

Views: 3079

Answers (2)

Stefan Neacsu
Stefan Neacsu

Reputation: 693

The best way is to check the documentation and to find out more about Google Cloud Storage and how to upload your files or documents to Google Cloud Storage.

Regarding uploading a CSV file to Google Cloud Storage, you can access this link where you can use different ways to upload files to Google Cloud Storage.

For your case use, here is the code sample to upload a file using nodeJS. Here in your case just change in the const field for the filename to the path of your file and instead of .txt you should specify .csv.

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const filename = 'Local file to upload, e.g. ./local/path/to/file.txt';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

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

async function uploadFile() {
  // Uploads a local file to the bucket
  await storage.bucket(bucketName).upload(filename, {
    // Support for HTTP requests made with `Accept-Encoding: gzip`
    gzip: true,
    // By setting the option `destination`, you can change the name of the
    // object you are uploading to a bucket.
    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}.`);
}

uploadFile();

Upvotes: 1

Fahad Abid
Fahad Abid

Reputation: 1092

Check out this GitHub repository its has examples for cloud storage with Nodejs

Upvotes: 0

Related Questions