I have been trying for days to get upload file from the aws-sdk for javascript to an s3 bucket. I have added the correct CORS policy and for testing purposes the bucket has public permissions. File upload only works 50% of the time though.
I am trying to upload 3-4 files in immediate succession from my website but I am getting access denied after the 2nd one. For some reason the first 2 upload successfully but the 3rd onwards fail. I have no idea if i have uncovered a glitch or if there is something I have done wrong.
My CORs policy is. I have also tried
`<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>http://*</AllowedOrigin>
<AllowedOrigin>https://*</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<ExposeHeader>ETag</ExposeHeader>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>`
I have also tried:
<AllowedOrigin>*</AllowedOrigin>
To no success.
To upload files I use temporary credentials from the aws-cognito sdk for js. The code I use to upload files is as follows:
function uploadFile(fileData,callback){
var file = fileData[4];
var key = "";
for(i=0; i<3; i++){
var temp = Math.ceil(Math.random()*1000000000000)
key=key+temp;
}
AWS.config.sessionToken= mySessionToken;
AWS.config.accessKeyId= myAccessKey;
AWS.config.secretAccessKey= mySecretKey;
AWS.config.region= myRegion;
var bucket = new AWS.S3({params: {Bucket: myBucketName}});
var params = {Key: key, ContentType: file.type, Body: file, ACL:'bucket-owner-
full-control'};
bucket.upload(params, function (err, data) {
if (err) {
callback(false,err,fileData);
}
else{
callback(true,"",fileData);
}
});
}
If it matters the website I am uploading from is hosted on an ec2 instance.
I know there are alot of posts about how to get file uploads to work with S3 but I have never seen any where it only works a portion of the time. I assume the credentials I use and my CORS configuration is correct because otherwise the uploads would never work right?
Thanks in advance for the help!
Edit:
This error only occurs on the 3rd sequential upload and onwards. The first and second file I upload have no errors and are successful.
The error code I receive is as follows:
Put [S3Instance] 403 Forbidden
<Error>
<Code>
AccessDenied
</Code>
<Message>
Access Denied
</Message>
<RequestId>
RequestId
</RequestId>
<HostId>
HostId
</HostId>
</Error>
I had the same problem, I attached the two strategies: AWSLambdaExecute and AWSLambdaBasicExecutionRole to the IAM role associated with my lambda and that solved the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With