node.jsamazon-s3aws-sdkmultermulter-s3

Upload JSON file to aws s3 bucket from aws-sdk in node.js


I am receiving an array of objects from 3rd party API and I want to store its data to S3 bucket in JSON file. Below are the steps which I want to achieve

1> Create new JSON file in S3 bucket I have following:

var AWS = require('aws-sdk')
var express = require('express')
var multer = require('multer')
var multerS3 = require('multer-s3')
AWS.config.loadFromPath('./lib/configFile.json');
var s3 = new AWS.S3();
var data =  [
             {
              name: abc,
              address: xyz, def-12345
             },{
              name: fgh,
              address: cvb, def-09876
             },{
              name: jkl,
              address: nbm, def-34566
             }
            ]

Now, I want to upload this data as a JSON file in S3 bucket. Is there any way to upload this data to S3 without creating any temporary json file in a path and directly store this data to S3.

2> Already have JSON file on S3: Let's say I already have JSON file on S3 bucket and I want to update the same file. So, to achieve this is there any way to directly store JSON data somewhere temporarily and update this file.

What is the best way to achieve this?


Solution

  • I know this is late. But my comment may help some other.

    A few days back even I had the same situation. I had javascript object which was huge. I looked for a solution. Then I looked here.

    I created a generic function to upload a file which might be buffer, base64 or JSON data.

    let data = {
      version: "2.3.4",
      objects: [{
        a: "imagas dae",
        verasdasdsion: "2.3.4",
        originX: "center",
        originY: "center"
      }],
      background: "#232326",
      canvasWidth: 619,
      canvasHeight: 413
    }
    uploadFile(JSON.stringify(data), 'test.json').then(r=>{}).catch(e=>{})
    
    
    
    function uploadFile(file, file_name, path = '/defualt') {
        return new Promise((resolve, reject) => {
            if (Buffer.isBuffer(file) || (file && Buffer.isBuffer(file.buffer)) || String(file_name).includes('.json') || file.indexOf('data:') === 0) {
                file = Buffer.isBuffer(file) ? file : String(file_name).includes('.json') ? file : file.indexOf('data:') === 0 ? new Buffer(img_src.replace(/^data:\w+\/\w+;base64,/, ""), 'base64') : file.buffer;
                var data = {
                    Key: file_name,
                    Body: file,
                    Bucket: "path/to/your/bucket" + path,
                    CacheControl: 'no-cache'
                };
    
    
                if (file.indexOf('data:') === 0) {
                    data['ContentType'] = String(file).substr(file.indexOf('data:') + 'data:'.length, file.indexOf(';'))
                } else if (String(file_name).includes('.json')) {
                    data['ContentType'] = 'application/pdf';
                }
    
                s3.putObject(data, function (err, data) {
                    if (err) {
                        console.log('Error uploading data: ', err);
                        return reject(err);
                    } else {
                        return resolve({
                            name: file_name,
                            path: path
                        });
                    }
                });
            } else {
                return reject('File is required');
            }
        });
    }