Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

uploading json data to s3 bucket in node js

var AWS = require('aws-sdk');
AWS.config.update({ region: 'us-east-1' });
var s3 = new AWS.S3();

var obj = {
    firstname: "Navjot",
    lastname: "Dhanawat"
};

var buf = Buffer.from(JSON.stringify(obj));

var data = {
    Bucket: 'bucket-name',
    Key: 'filename.json',
    Body: buf,
    ContentEncoding: 'base64',
    ContentType: 'application/json',
    ACL: 'public-read'
};

s3.upload(data, function (err, data) {
    if (err) {
        console.log(err);
        console.log('Error uploading data: ', data);
    } else {
        console.log('succesfully uploaded!!!');
    }
});

// IMPORTANT !!!!

// real node js route for POST request
// S3 Upload Test Starts
// const express = require('express');
var AWS = require('aws-sdk');
// for s3 data from create-project
var data;
var buf;
var s3

AWS.config.update({
    accessKeyId: "",
    secretAccessKey: "",
    region: ''
});
s3 = new AWS.S3();

// Define POST route
router.post('/test-upload', (request, response) => {
    buf = Buffer.from(JSON.stringify(request.body)); 
    data = {
        ACL: 'public-read',
        Bucket: "",
        Key: `something/${Date.now().toString()}`,
        Body: buf,
        ContentEncoding: 'base64',
        ContentType: 'application/json',
    };
    s3.upload(data, function (err, data) {
        if (err) {
            console.log(err);
            console.log('Error uploading data: ', data);
        } else {
            console.log('succesfully uploaded!!!');
            response.json({"upload":"successful"});
        }
    });
    
});




// S3 Upload Test Ends
Source by gist.github.com #
 
PREVIOUS NEXT
Tagged: #uploading #json #data #bucket #node #js
ADD COMMENT
Topic
Name
5+3 =