0

So I am using AWS Javascript SDK to S3 object, using this method:

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getObject-property

The call completed successfully, however it only shows the Object data with string.

How can I download this file from browser with only plain Javascript?

2 Answers 2

3

The documentation clearly states in the Callback section of the getObject method that data.Body will return a "Buffer, Typed Array, Blob, String, ReadableStream".

That's your file.

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

Comments

0

Here is how you can download a file from AWS using vanilla JavaScript.

let bucket_name = 'name of your bucket';
let folder_name = 'name of your folder';
let file_name = 'name of your file';
fetch('https://' + bucket_name + '.s3.amazonaws.com/' + folder_name + '/' + file_name + '.jpg')
.then(resp => resp.blob())
.then(blob => {
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.style.display = 'none';
    a.href = url;
    a.download = file_name.split("/").pop();
    document.body.appendChild(a);
    a.click();
    window.URL.revokeObjectURL(url);
    alert('your file has downloaded!');
})
.catch(() => console.log('oh no!'));

1 Comment

To help people understand your answer better, please provide some extra information along with the code you shared.

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.