10

I want to upload images to amazon web server and for that I am using aws-sdk with nodejs.

I am able to upload images to the s3 bucket, but when I click on the URL to access them I get access-denied error.

Here is my configuration

     var AWS = require('aws-sdk');

     //aws credentials
     AWS.config = new AWS.Config();
     AWS.config.accessKeyId = "<access_key>";
     AWS.config.secretAccessKey = "<secret_key>";
     AWS.config.region = "ap-southeast-1";
     AWS.config.apiVersions = {
        "s3": "2006-03-01"
     }

    var s3 = new AWS.S3();

    var bodystream = fs.createReadStream(req.files.pic.path);

    var params = {
        'Bucket': '<bucket_name>',
        'Key': 'uploads/images/' + req.files.pic.name,
        'Body': bodystream,
        'ContentEncoding': 'base64', 
        'ContentType ': 'image/jpeg'
     };

     //also tried with s3.putObject
     s3.upload(params, function(err, data){
        console.log('after s3 upload====', err, data);
     }) 

Images are uploading successfully but their content-type is application/octet. Also I think that there is some permission issue, because when I add a new permission then I am able to download the image but not able to see it.

Can you tell me what is wrong with this configuration and also I want to know the difference between s3.upload and s3.putObject method.

1 Answer 1

4

In order to specify Content-Type header you need to define Metadata section:

var params = {
    'Bucket': '<bucket_name>',
    'Key': 'uploads/images/' + req.files.pic.name,
    'Body': bodystream,
    'ContentEncoding': 'base64', 
    Metadata: {
        'Content-Type': 'image/jpeg'
    }

 };
Sign up to request clarification or add additional context in comments.

8 Comments

getting this error if I try to specify content-type this way { [SignatureDoesNotMatch: The request signature we calculated does not match the signature you provided. Check your key and signing method.] message: 'The request signature we calculated does not match the signature you provided. Check your key and signing method.', code: 'SignatureDoesNotMatch', time: Fri Oct 30 2015 12:55:30 GMT+0530 (IST), statusCode: 403, retryable: false, retryDelay: 30 }
@BhushanGoel I've fixed the code, forgot about the dash in content-type
yeah it worked, now in s3 console I can see this in the metadata: x-amz-meta-content-type: image/png
but how can I set the permission, I am still getting access denied error
@BhushanGoel you can either use putObject and specify ACL there or you can use putBucketAcl to setACL separately
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.