Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Choose your framework/language

Page updated May 21, 2024

Upload files

Implement upload functionality

Note: Refer to the Transfer Acceleration documentation to learn how to enable transfer acceleration for storage APIs.

Upload from file

The following is an example of how you would upload a file from a file object, this could be retrieved from the local machine or a different source.

import React from 'react';
import { uploadData } from 'aws-amplify/storage';
function App() {
const [file, setFile] = React.useState();
const handleChange = (event: any) => {
setFile(event.target.files[0]);
};
return (
<div>
<input type="file" onChange={handleChange} />
<button
onClick={() =>
uploadData({
path: `photos/${file.name}`,
data: file,
})
}
>
Upload
</button>
</div>
);
}

Upload from data

You can following this example if you have data saved in memory and would like to upload this data to the cloud.

import { uploadData } from 'aws-amplify/storage';
try {
const result = await uploadData({
path: "album/2024/1.jpg",
// Alternatively, path: ({identityId}) => `album/{identityId}/1.jpg`
data: file,
options: {
onProgress // Optional progress callback.
}
}).result;
console.log('Succeeded: ', result);
} catch (error) {
console.log('Error : ', error);
}

Monitor upload progress

Monitor progress of upload by using the onProgress options

import { uploadData } from 'aws-amplify/storage';
const monitorUpload = async () => {
try {
const result = await uploadData({
path: "album/2024/1.jpg",
// Alternatively, path: ({identityId}) => `album/{identityId}/1.jpg`
data: file,
options: {
onProgress: ({ transferredBytes, totalBytes }) => {
if (totalBytes) {
console.log(
`Upload progress ${Math.round(
(transferredBytes / totalBytes) * 100
)} %`
);
}
},
},
}).result;
console.log("Path from Response: ", result.path);
} catch (error) {
console.log("Error : ", error);
}
}

Pause, resume, and cancel uploads

We have callback functions that support resuming, pausing, and cancelling uploadData requests.

import { uploadData, isCancelError } from 'aws-amplify/storage';
// Pause, resume, and cancel a task
const uploadTask = uploadData({ path, data: file });
//...
uploadTask.pause();
//...
uploadTask.resume();
//...
uploadTask.cancel();
//...
try {
await uploadTask.result;
} catch (error) {
if (isCancelError(error)) {
// Handle error thrown by task cancellation
}
}

More upload options

OptionTypeDescription
contentTypeStringThe default content-type header value of the file when downloading it.

Read more at Content-Type documentation
contentEncodingStringThe default content-encoding header value of the file when downloading it.

Read more at Content-Encoding documentation
contentDispositionStringSpecifies presentational information for the object.

Read more at Content-Disposition documentation
metadatamap<String>A map of metadata to store with the object in S3.

Read more at S3 metadata documentation
useAccelerateEndpointbooleanWhether to use accelerate endpoint.

Read more at Transfer Acceleration

Uploads that were initiated over one hour ago will be cancelled automatically. There are instances (e.g. device went offline, user logs out) where the incomplete file remains in your Amazon S3 account. It is recommended to setup a S3 lifecycle rule to automatically cleanup incomplete upload requests.

MultiPart upload

Amplify will automatically perform an Amazon S3 multipart upload for objects that are larger than 5MB. For more information about S3's multipart upload, see Uploading and copying objects using multipart upload