Page updated Mar 6, 2024

Set up Amplify Storage

The Amplify Storage category provides an interface for managing user content for your app in public, protected, or private storage buckets. The Storage category comes with default built-in support for Amazon Simple Storage Service (S3). The Amplify CLI helps you to create and configure the storage buckets for your app. The Amplify AWS S3 Storage plugin leverages Amazon S3.

Goal

To setup and configure your application with Amplify Storage and go through a simple upload file example

Prerequisites

  • A Flutter application targeting Flutter SDK >= 3.3.0 with Amplify libraries integrated

    The following are also required, depending on which platforms you are targeting:

    • An iOS configuration targeting at least iOS 13.0 and XCode version >=13.2
    • An Android configuration targeting at least Android API level 24 (Android 7.0) or above
    • Any browser supported by Flutter for Web (you can check the list of supported browsers here)
    • Any Windows OS meeting Flutter minimums
    • macOS version 10.15 or higher
    • Any Ubuntu Linux distribution meeting Flutter minimums
    • For a full example please follow the project setup walkthrough

Provision backend storage

To start provisioning storage resources in the backend, go to your project directory and execute the command:

1amplify add storage

Enter the following when prompted:

1? Please select from one of the below mentioned services:
2 `Content (Images, audio, video, etc.)`
3? You need to add auth (Amazon Cognito) to your project in order to add storage for user files. Do you want to add auth now?
4 `Yes`
5? Do you want to use the default authentication and security configuration?
6 `Default configuration`
7? How do you want users to be able to sign in?
8 `Username`
9? Do you want to configure advanced settings?
10 `No, I am done.`
11? Please provide a friendly name for your resource that will be used to label this category in the project:
12 `S3friendlyName`
13? Please provide bucket name:
14 `storagebucketname`
15? Who should have access:
16 `Auth and guest users`
17? What kind of access do you want for Authenticated users?
18 `create/update, read, delete`
19? What kind of access do you want for Guest users?
20 `create/update, read, delete`
21? Do you want to add a Lambda Trigger for your S3 Bucket?
22 `No`

To push your changes to the cloud, execute the command:

1amplify push

Upon completion, amplifyconfiguration.dart will be updated to reference a newly provisioned S3 bucket. Note that this file should already be a part of your project if you followed the Project setup walkthrough.

Install Amplify Libraries

Add the following dependency to your app's pubspec.yaml along with others you added above in Prerequisites:

1environment:
2 sdk: ">=2.18.0 <4.0.0"
3 flutter: ">=3.3.0"
4
5dependencies:
6 flutter:
7 sdk: flutter
8
9 amplify_auth_cognito: ^1.0.0
10 amplify_flutter: ^1.0.0
11 amplify_storage_s3: ^1.0.0

Initialize Amplify Storage

To initialize the Amplify Auth and Storage categories, call Amplify.addPlugin() for each plugin or pass all the plugins in Amplify.addPlugins(). To complete initialization, call Amplify.configure().

Your code should look like this:

1import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
2import 'package:amplify_flutter/amplify_flutter.dart';
3import 'package:amplify_storage_s3/amplify_storage_s3.dart';
4import 'package:flutter/material.dart';
5
6import 'amplifyconfiguration.dart';
7
8Future<void> _configureAmplify() async {
9 try {
10 final auth = AmplifyAuthCognito();
11 final storage = AmplifyStorageS3();
12 await Amplify.addPlugins([auth, storage]);
13
14 // call Amplify.configure to use the initialized categories in your app
15 await Amplify.configure(amplifyconfig);
16 } on Exception catch (e) {
17 safePrint('An error occurred configuring Amplify: $e');
18 }
19}
20
21Future<void> main() async {
22 WidgetsFlutterBinding.ensureInitialized();
23 await _configureAmplify();
24 runApp(const MyApp());
25}
26
27class MyApp extends StatefulWidget {
28 const MyApp({Key? key}) : super(key: key);
29
30 // ...
31}

Uploading data to your bucket

To upload to S3 from a data object, specify the key and the data object to be uploaded.

1import 'package:amplify_flutter/amplify_flutter.dart';
2import 'package:amplify_storage_s3/amplify_storage_s3.dart';
3
4Future<void> uploadExampleData() async {
5 const dataString = 'Example file contents';
6
7 try {
8 final result = await Amplify.Storage.uploadData(
9 data: S3DataPayload.string(dataString),
10 key: 'ExampleKey',
11 onProgress: (progress) {
12 safePrint('Transferred bytes: ${progress.transferredBytes}');
13 },
14 ).result;
15
16 safePrint('Uploaded data to location: ${result.uploadedItem.key}');
17 } on StorageException catch (e) {
18 safePrint(e.message);
19 }
20}

Upon successfully executing this code, you should see a new folder in your bucket, called public. It should contain a file called ExampleKey, whose contents is Example file contents.

Next Steps

Congratulations! You've uploaded a file to an s3 bucket. Check out the following links to see other Amplify Storage use cases: