如何使用nodeJS将CSV文件上传到Google Cloud Storage

时间:2019-11-28 13:36:31

标签: node.js google-cloud-platform

我需要使用Google Cloud和nodeJS上传和共享我的文档,但是我不确定如何准确地做到这一点。

2 个答案:

答案 0 :(得分:0)

查看此GitHub存储库,其中包含examples的Nodejs云存储

答案 1 :(得分:0)

最好的方法是检查documentation并了解有关Google Cloud Storage的更多信息以及如何将文件或文档上传到Google Cloud Storage。

关于将CSV文件上传到Google Cloud Storage,您可以访问此link,在这里您可以使用其他方式将文件上传到Google Cloud Storage。

对于您的案例,这是使用nodeJS上传文件的代码示例。在这种情况下,只需在文件名的const字段中更改文件路径即可,而不是.txt,您应该指定.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();