【问题标题】:How to Upload CSV file on S3 Bucket using NodeJS?如何使用 NodeJS 在 S3 存储桶上上传 CSV 文件?
【发布时间】:2018-05-31 11:23:30
【问题描述】:

我正在从 JSON 内容动态创建 CSV 文件并将生成的 CSV 文件上传到 S3 存储桶,而不是先将文件保存在本地。

下面是我的代码 sn-p,因为使用下面的代码我的 CSV 文件上传到 S3 存储桶,但它似乎不是正确的 CSV 格式。

var uploadCSVFileOnS3Bucket = function(next, csvFileContent,results) {
    console.log("uploadCSVFileOnS3Bucket function started");
    var bufferObject = new Buffer.from(JSON.stringify(csvFileContent));
    var filePath = configurationHolder.config.s3UploadFilePath;
    var s3 = new AWS.S3();
    var params = {
        Bucket: 'bucket_name'
        Key: 's3UploadFilePath',
        Body: bufferObject,
        CacheControl:'public, max-age=86400'
    }
    s3.upload(params, function(err, data) {
        if (err) {
            console.log("Error at uploadCSVFileOnS3Bucket function",err);
            next(err);
        } else {
            console.log("File uploaded Successfully");
            next(null, filePath);
        }
    });
};

另外,我正在使用“json2csv”npm 模块从 JSON 生成 csv 文件内容。

下面是代码:

var generateCSVFile = function(next,callback,csvFileContent) {
   console.log("generateCSVFile function started",csvFileContent);
   if(csvFileContent && csvFileContent.length>0) {
     var fields = ['field1','field2','field3',........];
     var csv = json2csv({ data: csvFileContent, fields: fields });
     console.log('created',csv);
     next(null,csv);
   }
   else {
     next(null,[]);
   }
 }

请让我们知道上面的代码哪里出错了。

【问题讨论】:

    标签: node.js csv amazon-web-services amazon-s3


    【解决方案1】:

    试试这个,它对我有用:

    var fs = require('file-system')
    var AWS = require('aws-sdk')
    
    AWS.config.update({
        region: '', // use appropriate region
        accessKeyId: '', // use your access key
        secretAccessKey: '' // user your secret key
    })
    
    var s3 = new AWS.S3()
    
    fs.readFile('contacts.csv','utf-8',  (err, data) => {
        if (err) throw err;
        const params = {
          Bucket: 'testBucket', // pass your bucket name
          Key: 'contacts.csv', // file will be saved as testBucket/contacts.csv
          Body : data
        };
    
        s3.upload(params, (s3Err, data) => {
          if (s3Err) throw s3Err
          console.log(`File uploaded successfully at ${data.Location}`)
        });
       });
    

    【讨论】:

      【解决方案2】:

      您好,我再次尝试使用以下标题值,它对我有用。下面是代码:

      var s3 = new AWS.S3();
      var params = {
          Bucket: bucketName,
          Key: filePath,
          Body: csvFileContent,
          ContentType: 'application/octet-stream',
          ContentDisposition: contentDisposition(filePath, {
              type: 'inline'
          }),
          CacheControl: 'public, max-age=86400'
      }
      s3.putObject(params, function(err, data) {
          if (err) {
              console.log("Error at uploadCSVFileOnS3Bucket function", err);
              next(err);
          } else {
              console.log("File uploaded Successfully");
              next(null, filePath);
          }
      });
      

      【讨论】:

      • 还有这个contentDisposition函数在哪里??
      • CacheControl - 此选项是否会在上传文件之前清除缓存?
      • @ArjunSingh 不,实际上,当我们尝试从其链接的 CDN URL 访问 S3 对象时,此 CacheControl 为 CDN 设置了缓存时间。要移除缓存,我们需要手动移除 CDN 缓存,否则它只会在缓存时间到期后指向 S3 对象。
      【解决方案3】:

      在您的参数中添加 ContentDisposition: 'attachment'。

      否则你也可以读取文件并上传到s3

      fs.readFile(FILEPATH, function(err, file_buffer) {
                  var params = {
                      Bucket:  //bucketname,
                      Key:key,
                      ContentDisposition: 'attachment',
                      Body: file_buffer
                  };
                  s3.upload(params, function(err, data) {
                      if (err) {
                          console.log("Error in upload");
                          callback(err, null)
                      }
                      if (data) {
                          console.log("Upload Success", data);
                          callback(null, data)
                      }
                  });
      });
      

      【讨论】:

      • 我尝试使用上面的 Header 但它没有为我提供正确的结果。
      • 您好,我再次尝试使用上面的标题并且它有效,我在单独的注释中添加了整个代码。谢谢
      猜你喜欢
      • 1970-01-01
      • 2018-06-02
      • 2019-06-12
      • 2022-11-05
      • 2018-12-30
      • 2017-09-17
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      相关资源
      最近更新 更多