简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM