Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I upload the image to AWS S3

How to access that key name <code>pin_img</code> in my post API

pin.js

function uploadPinMedia(req, res, next) {

    var s3 = new AWS.S3({
        accessKeyId: 'AKIAIUCBE3TQGL6ATPBA',
        secretAccessKey: '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',
        Bucket: 'mapery-v2'
    });

    var data = {
        Bucket: 'mapery-v2',
        Key: 'upload-v2',
        Body: req.files.pin_img
    };

    s3.putObject(data, function(err, media_res) {
        if(err) 
            console.log('error uploading data', media_res);
        console.log(media_res);
    });
    
}
I am trying to upload an image to s3 bucket from postman, but it seems to be the form data req.files.pin_img I am passing is not correct. Am I wrong anywhere ??
like image 404
M gowda Avatar asked Oct 20 '25 12:10

M gowda


1 Answers

below works for me. make sure you have correct config. bucket. you'll found uploaded image using https://appname.s3-accelerate.amazonaws.com/aws_test.jpg

const AWS = require('aws-sdk');
AWS.config.update({
        accessKeyId: process.env.AWS_KeyId,
        secretAccessKey: process.env.AWS_secret,
        // region:process.env.AWS_region  
});

const s3 = new AWS.S3({   signatureVersion: 'v4',  })
var photoBucket = new AWS.S3({
    params: {
        Bucket: 'bucket'
    }
})
photoBucket.upload({
        ACL: 'public-read', 
        Body: fs.createReadStream('./test.jpg'), 
        // file upload by below name
        Key: 'aws_test.jpg',
        ContentType: 'application/octet-stream' // force download if it's accessed as a top location
},(err, response)=>{
    console.log(err, response)
});
like image 179
shivshankar Avatar answered Oct 22 '25 04:10

shivshankar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!