1
votes

I created a bucket on aws and IAM user, then set up access permissions for the user.

Now I'm stuck because I don't know how to use aws-sdk to upload an image. I can't find any example (all the examples I found use key and secret key, not IAM).

Please give me the working example or fix my code that is apparently not working:

var awsFileName = entityType.awsFolder + entityId + '/' + fileName; //get filename like /folder1/folder2/filename

aws.config.update({
    region: 'ca-central-1',
    accessKeyId: awsKey // like 'ADXZJMPSVCU3KOAXGJJL'
  });
var s3 = new aws.S3();

var s3Bucket = new aws.S3( { params: {Bucket: 'testbucket'} } );
var awsFileData = {Key: awsFileName, Body: data};
s3Bucket.putObject(data, function(err, data){
  if (!err) 
    { 
        callbackOk(fileName, awsFileName);
        console.log('succesfully uploaded the image!');
    } else {
        console.log('Error uploading data: ', data); 
        callbackFail();
    }
});

What I have now is error "Missing required key 'Key' in params" and 45,000 (!!!) errors with the same text "Error: Unexpected key 'N' found in params(…)" where N is number of errors.

1

1 Answers

3
votes

This doesn't appear to be related to IAM at all. You are not passing the Key parameter to the S3 put operation. I believe you should be passing fileData to putObject, instead of data. Like so:

var awsFileData = {Key: awsFileName, Body: data};
s3Bucket.putObject(awsFileData, function(err, data){