Download files
The following options are available for retrieving previously uploaded data:
Generate a download URL
Get a presigned URL of a stored file and expiration of URL.
1import { getUrl } from 'aws-amplify/storage';2
3const getUrlResult = await getUrl({4 path: 'public/album/2024/1.jpg',5 // Alternatively, path: ({identityId}) => `protected/${identityId}/album/2024/1.jpg`6 options: {7 validateObjectExistence?: false, // Check if object exists before creating a URL8 expiresIn?: 20 // validity of the URL, in seconds. defaults to 900 (15 minutes) and maxes at 3600 (1 hour)9 useAccelerateEndpoint?: true; // Whether to use accelerate endpoint10 },11});12console.log('signed URL: ', getUrlResult.url);13console.log('URL expires at: ', getUrlResult.expiresAt);
1import { getUrl } from 'aws-amplify/storage';2
3const getUrlResult = await getUrl({4 key: 'album/2024/1.jpg',5 options: {6 accessLevel?: 'protected' , // can be 'private', 'protected', or 'guest' but defaults to `guest`7 targetIdentityId?: 'XXXXXXX', // id of another user, if `accessLevel` is `protected`8 validateObjectExistence?: false, // Check if object exists before creating a URL9 expiresIn?: 20 // validity of the URL, in seconds. defaults to 900 (15 minutes) and maxes at 3600 (1 hour)10 useAccelerateEndpoint?: true; // Whether to use accelerate endpoint11 },12});13console.log('signed URL: ', getUrlResult.url);14console.log('URL expires at: ', getUrlResult.expiresAt);
getUrl
returns a signed URL in the url
property of the result. You can use this to create a download link for users to click on. The expiresAt
property is a Date
object that represents the time at which the URL will expire.
Inside your template or JSX code, you can use the url
property to create a link to the file:
1<a href="{signedURL.url.toString()}" target="_blank" rel="noreferrer">2 {fileName} 3</a>
Check for existence of a file
You can check for the existence of a file in the storage category's getUrl
API using the validateObjectExistence
option. When this flag is enabled a getUrl
call will return a pre-signed URL if the file exists and raise a 404
error if it does not. This allows you to check if an object exists during generating the presigned URL, which you can then use to download that object.
1import { getUrl } from 'aws-amplify/storage';2
3// To check for existence of a file4await getUrl({5 path: 'public/album/2024/1.jpg',6 // Alternatively, path: ({identityId}) => `protected/${identityId}/album/2024/1.jpg`7 options: {8 validateObjectExistence: true // defaults to false9 }10});
1import { getUrl } from 'aws-amplify/storage';2
3// To check for existence of a file4await getUrl({5 key: 'album/2024/1.jpg',6 options: {7 validateObjectExistence: true // defaults to false8 }9});
getUrl expiry
You can use expiresIn
option to limit the availability of your URLs. This configuration returns the pre-signed URL that expires in 60 seconds:
1import { getUrl } from 'aws-amplify/storage';2
3await getUrl({ 4 path: 'public/album/2024/1.jpg',5 // Alternatively, path: ({identityId}) => `protected/${identityId}/album/2024/1.jpg`6 options: { expiresIn: 60 } 7});
1import { getUrl } from 'aws-amplify/storage';2
3await getUrl({ 4 key: 'album/2024/1.jpg', 5 options: { expiresIn: 60 } 6});
downloadData
Download a file to in-memory buffer.
1import { downloadData } from 'aws-amplify/storage';2
3// Downloads file content to memory4const { body, eTag } = await downloadData({5 path: 'public/album/2024/1.jpg',6 // Alternatively, path: ({identityId}) => `protected/${identityId}/album/2024/1.jpg`7 options: {8 onProgress: (event) => {9 console.log(event.transferredBytes);10 } // optional progress callback11 bytesRange: {12 start: 1024,13 end: 204814 } // optional bytes range parameter to download a part of the file, the 2nd MB of the file in this example15 }16}).result;
1import { downloadData } from 'aws-amplify/storage';2
3// Downloads file content to memory4const { body, eTag } = await downloadData({5 key: 'album/2024/1.jpg',6 options: {7 accessLevel: 'guest', // access level of the file being downloaded8 targetIdentityId: 'xxxxxxx', // the identity id of another user, required when setting accessLevel to 'protected'9 onProgress: (event) => {10 console.log(event.transferredBytes);11 } // optional progress callback12 bytesRange: {13 start: 1024,14 end: 204815 } // optional bytes range parameter to download a part of the file, the 2nd MB of the file in this example16 }17}).result;
Get the text value of downloaded File
You can consume the value of file in any of the three formats: blob
, json
, or text
. You can call the respective method on the body
property to consume the set data in the respective format.
1import { downloadData } from 'aws-amplify/storage';2
3try {4 const downloadResult = await downloadData({ 5 path: 'public/album/2024/1.jpg',6 // Alternatively, path: ({identityId}) => `protected/${identityId}/album/2024/1.jpg`7 }).result;8 const text = await downloadResult.body.text();9 // Alternatively, you can use `downloadResult.body.blob()`10 // or `downloadResult.body.json()` get read body in Blob or JSON format.11 console.log('Succeed: ', text);12} catch (error) {13 console.log('Error : ', error);14}
1import { downloadData } from 'aws-amplify/storage';2
3try {4 const downloadResult = await downloadData({ 5 key: 'album/2024/1.jpg' 6 }).result;7 const text = await downloadResult.body.text();8 // Alternatively, you can use `downloadResult.body.blob()`9 // or `downloadResult.body.json()` get read body in Blob or JSON format.10 console.log('Succeed: ', text);11} catch (error) {12 console.log('Error : ', error);13}
Track the progress of a download task
To track the progress of your download, you can use onProgress
:
1import { downloadData } from 'aws-amplify/storage';2
3// Download a file from s3 bucket4const { body, eTag } = await downloadData(5 {6 path: 'public/album/2024/1.jpg',7 // Alternatively, path: ({identityId}) => `protected/${identityId}/album/2024/1.jpg`8 options: {9 onProgress: (progress) {10 console.log(`Download progress: ${(progress.transferredBytes/progress.totalBytes) * 100}%`);11 }12 }13 }14).result;
1import { downloadData } from 'aws-amplify/storage';2
3// Download a file from s3 bucket4const { body, eTag } = await downloadData(5 {6 key: 'album/2024/1.jpg',7 options: {8 onProgress: (progress) {9 console.log(`Download progress: ${(progress.transferredBytes/progress.totalBytes) * 100}%`);10 }11 }12 }13).result;
Cancel a download task
1import { downloadData, isCancelError } from 'aws-amplify/storage';2
3const downloadTask = downloadData({ 4 path: 'public/album/2024/1.jpg',5 // Alternatively, path: ({identityId}) => `protected/${identityId}/album/2024/1.jpg` 6});7downloadTask.cancel();8try {9 await downloadTask.result;10} catch (error) {11 if (isCancelError(error)) {12 // Handle error thrown by task cancellation.13 }14}
1import { downloadData, isCancelError } from 'aws-amplify/storage';2const downloadTask = downloadData({ key: 'album/2024/1.jpg' });3downloadTask.cancel();4try {5 await downloadTask.result;6} catch (error) {7 if (isCancelError(error)) {8 // Handle error thrown by task cancellation.9 }10}
Frequently Asked Questions
Users can run into unexpected issues, so we are giving you advance notice in documentation with links to open issues - please vote for what you need, to help the team prioritize.
downloadData
is cached; if you have recently modified a file you may not get the latest version right away. You can pass incacheControl: 'no-cache'
to get the latest version.downloadData
only returns the latest cached version of the file; there is not yet an API to view prior versions.- Image compression or CloudFront CDN caching for your S3 buckets is not yet possible.
- There is no API for Cognito Group-based access to files.
- There is currently no API for getting the identityId of other users; you have to retrieve this from elsewhere before calling
Storage.get
.