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
Amplify Flutter requires a minimum target platform for iOS (13.0), Android (API level 24), and macOS (10.15). Additional setup is required for some target platforms. Please see the platform setup guide for more details on platform specific setup.
Provision backend storage
To start provisioning storage resources in the backend, go to your project directory and execute the command:
amplify add storage
Enter the following when prompted:
? Please select from one of the below mentioned services: `Content (Images, audio, video, etc.)`? 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? `Yes`? Do you want to use the default authentication and security configuration? `Default configuration`? How do you want users to be able to sign in? `Username`? Do you want to configure advanced settings? `No, I am done.`? Please provide a friendly name for your resource that will be used to label this category in the project: `S3friendlyName`? Please provide bucket name: `storagebucketname`? Who should have access: `Auth and guest users`? What kind of access do you want for Authenticated users? `create/update, read, delete`? What kind of access do you want for Guest users? `create/update, read, delete`? Do you want to add a Lambda Trigger for your S3 Bucket? `No`
To push your changes to the cloud, execute the command:
amplify 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:
dependencies: flutter: sdk: flutter
amplify_auth_cognito: ^2.0.0 amplify_flutter: ^2.0.0 amplify_storage_s3: ^2.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:
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';import 'package:amplify_flutter/amplify_flutter.dart';import 'package:amplify_storage_s3/amplify_storage_s3.dart';import 'package:flutter/material.dart';
import 'amplifyconfiguration.dart';
Future<void> _configureAmplify() async { try { final auth = AmplifyAuthCognito(); final storage = AmplifyStorageS3(); await Amplify.addPlugins([auth, storage]);
// call Amplify.configure to use the initialized categories in your app await Amplify.configure(amplifyconfig); } on Exception catch (e) { safePrint('An error occurred configuring Amplify: $e'); }}
Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); await _configureAmplify(); runApp(const MyApp());}
class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key);
// ...}