Siddu hadapad
Siddu hadapad

Reputation: 3703

How to upload file to aws s3 using xmlhttprequest

How can I upload file to s3 using xmlhttprequest. I tried following way but I got error like

'Anonymous access is forbidden for this operation'.

Here is my code:

const xhr = new XMLHttpRequest();
xhr.open('PUT', "https://region.amazonaws.com/bucket-name/");
xhr.setRequestHeader('X-Amz-ACL', 'public-read');
xhr.setRequestHeader('Content-Type', 'image/png');
xhr.send({
    file: file,
    type: 'image/png',
    name: "myfile_9898_0",
    acl: 'public-read',
    AWSAccessKeyId: 'aws key',
    AWSSecreKeyId: 'scret_key'
});

Is there anything wrong with this code?
Can anyone help me?

Upvotes: 2

Views: 4793

Answers (1)

court3nay
court3nay

Reputation: 2365

you spelled SecretKey wrong.

however, anyone viewing this page (assuming it's in a html page) can steal your credentials! it's a Really Bad Idea to put secretKey anywhere - you're better to generate a 'pre-signed key' on the server with your secret. You'll also need CORS set up on your server to allow the cross-post.

Upvotes: 1

Related Questions