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

Choose your framework/language

Page updated May 3, 2024

Set up Storage

In this guide, you will learn how to set up storage in your Amplify app. You will set up your backend resources, and enable listing, uploading, and downloading files.

If you have not yet created an Amplify app, visit the quickstart guide.

Amplify Storage seamlessly integrates file storage and management capabilities into frontend web and mobile apps, built on top of Amazon Simple Storage Service (Amazon S3). It provides intuitive APIs and UI components for core file operations, enabling developers to build scalable and secure file storage solutions without dealing with cloud service complexities.

Building your storage backend

First, create a file amplify/storage/resource.ts. This file will be the location where you configure your storage backend. Instantiate storage using the defineStorage function and providing a name for your storage bucket. This name is a friendly name to identify your bucket in your backend configuration. Amplify will generate a unique identifier for your app using a UUID, the name attribute is just for use in your app.

amplify/storage/resource.ts
1import { defineStorage } from '@aws-amplify/backend';
2
3export const storage = defineStorage({
4 name: 'amplifyTeamDrive'
5});

Import your storage definition in your amplify/backend.ts file that contains your backend definition. Add storage to defineBackend.

amplify/backend.ts
1import { defineBackend } from '@aws-amplify/backend';
2import { auth } from './auth/resource';
4
5defineBackend({
6 auth,
8});

Now when you run npx ampx sandbox or deploy your app on Amplify, it will configure an Amazon S3 bucket where your files will be stored. Before files can be accessed in your application, you must configure storage access rules.

To deploy these changes, commit them to git and push the changes upstream. Amplify's CI/CD system will automatically pick up the changes and build and deploy the updates.

Terminal
git commit -am "add storage backend"
git push

Define File Path Access

By default, no users or other project resources have access to any files in the storage bucket. Access must be explicitly granted within defineStorage using the access callback.

The access callback returns an object where each key in the object is a file path and each value in the object is an array of access rules that apply to that path.

The following example shows you how you can set up your file storage structure for a generic photo sharing app. Here,

  1. Guests have access to see all profile pictures and only the users that uploaded the profile picture can replace or delete them. Users are identified by their Identity Pool ID in this case i.e. identityID.
  2. There's also a general pool where all users can submit pictures.

Learn more about customizing access to file path.

amplify/storage/resource.ts
1export const storage = defineStorage({
2 name: 'amplifyTeamDrive',
3 access: (allow) => ({
4 'profile-pictures/{entity_id}/*': [
5 allow.guest.to(['read'])
6 allow.entity('identity').to(['read', 'write', 'delete'])
7 ],
8 'picture-submissions/*': [
9 allow.authenticated.to(['read','write']),
10 allow.guest.to(['read', 'write'])
11 ],
12 })
13});

Connect your app code to the storage backend

The Amplify Storage library provides client APIs that connect to the backend resources you defined.

Configure Amplify in project

Import and load the configuration file in your app. It's recommended you add the Amplify configuration step to your app's root entry point. For example index.js in React or main.ts in Angular.

1import { Amplify } from 'aws-amplify';
2import outputs from '../amplify_outputs.json';
3
4Amplify.configure(outputs);

Make sure you call Amplify.configure as early as possible in your application’s life-cycle. A missing configuration or NoCredentials error is thrown if Amplify.configure has not been called before other Amplify JavaScript APIs.

Upload your first file

Next, let's a photo to the picture-submissions/ path.

1import React from 'react';
2import { uploadData } from 'aws-amplify/storage';
3
4function App() {
5 const [file, setFile] = React.useState();
6
7 const handleChange = (event: any) => {
8 setFile(event.target.files[0]);
9 };
10
11 return (
12 <div>
13 <input type="file" onChange={handleChange} />
14 <button
15 onClick={() =>
16 uploadData({
17 path: `picture-submissions/${file.name}`,
18 data: file,
19 })
20 }
21 >
22 Upload
23 </button>
24 </div>
25 );
26}

Manage files in Amplify console

After successfully publishing your storage backend and connecting your project with client APIs, you can manage files and folders in the Amplify console. You can perform on-demand actions like upload, download, copy, and more under the Storage tab in the console.

Showing Amplify console showing Storage tab selected

Conclusion

Congratulations! You finished the Set up Amplify Storage guide. In this guide, you set up and connected to backend resources, customized your file paths and access definitions, and connected your application to the backend to implement features like file uploads and downloads.

Next steps

Now that you have completed setting up storage in your Amplify app, you can proceed to add file management features to your app. You can use the following guides to implement upload and download functionality, or you can access more capabilities from the side navigation.