Page updated Nov 9, 2023

Upload files

Amplify Flutter v0 is now in Maintenance Mode until July 19th, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v0.

Please use the latest version (v1) of Amplify Flutter to get started.

If you are currently using v0, follow these instructions to upgrade to v1.

Upload File

To upload to S3 from a file, specify the key and the local file to be uploaded. A file can be created locally, or retrieved from the user's device using a package such as image_picker or file_picker.

Upload a local file

Here you use the path_provider package to get a directory where you can create a temporary file for uploading.

import 'dart:io'; import 'package:path_provider/path_provider.dart'; Future<void> createAndUploadFile() async { // Create a dummy file const exampleString = 'Example file contents'; final tempDir = await getTemporaryDirectory(); final exampleFile = File('${tempDir.path}/example.txt'); await exampleFile.create(); await exampleFile.writeAsString(exampleString); // Upload the file to S3 try { final result = await Amplify.Storage.uploadFile( local: exampleFile, key: 'ExampleKey', onProgress: (progress) { safePrint('Fraction completed: ${progress.getFractionCompleted()}'); }, ); safePrint('Successfully uploaded file: ${result.key}'); } on StorageException catch (e) { safePrint('Error uploading file: $e'); } finally { await exampleFile.delete(); } }
1import 'dart:io';
2
3import 'package:path_provider/path_provider.dart';
4
5Future<void> createAndUploadFile() async {
6 // Create a dummy file
7 const exampleString = 'Example file contents';
8 final tempDir = await getTemporaryDirectory();
9
10 final exampleFile = File('${tempDir.path}/example.txt');
11 await exampleFile.create();
12 await exampleFile.writeAsString(exampleString);
13
14 // Upload the file to S3
15 try {
16 final result = await Amplify.Storage.uploadFile(
17 local: exampleFile,
18 key: 'ExampleKey',
19 onProgress: (progress) {
20 safePrint('Fraction completed: ${progress.getFractionCompleted()}');
21 },
22 );
23 safePrint('Successfully uploaded file: ${result.key}');
24 } on StorageException catch (e) {
25 safePrint('Error uploading file: $e');
26 } finally {
27 await exampleFile.delete();
28 }
29}

Upload file with Flutter file picker packages

Make sure to follow the setup instructions on the image_picker homepage.

import 'dart:io'; import 'package:image_picker/image_picker.dart'; final picker = ImagePicker(); Future<void> uploadImage() async { // Select image from user's gallery final pickedFile = await picker.pickImage(source: ImageSource.gallery); if (pickedFile == null) { safePrint('No image selected'); return; } // Upload image with the current time as the key final key = DateTime.now().toString(); final file = File(pickedFile.path); try { final result = await Amplify.Storage.uploadFile( local: file, key: key, onProgress: (progress) { safePrint('Fraction completed: ${progress.getFractionCompleted()}'); }, ); safePrint('Successfully uploaded image: ${result.key}'); } on StorageException catch (e) { safePrint('Error uploading image: $e'); } }
1import 'dart:io';
2
3import 'package:image_picker/image_picker.dart';
4
5final picker = ImagePicker();
6
7Future<void> uploadImage() async {
8 // Select image from user's gallery
9 final pickedFile = await picker.pickImage(source: ImageSource.gallery);
10
11 if (pickedFile == null) {
12 safePrint('No image selected');
13 return;
14 }
15
16 // Upload image with the current time as the key
17 final key = DateTime.now().toString();
18 final file = File(pickedFile.path);
19 try {
20 final result = await Amplify.Storage.uploadFile(
21 local: file,
22 key: key,
23 onProgress: (progress) {
24 safePrint('Fraction completed: ${progress.getFractionCompleted()}');
25 },
26 );
27 safePrint('Successfully uploaded image: ${result.key}');
28 } on StorageException catch (e) {
29 safePrint('Error uploading image: $e');
30 }
31}

Upload Options

You may attach metadata while uploading data or a file via specifying metadata in options.

await Amplify.Storage.uploadFile( key: 'file', local: File('path/to/file'), options: S3UploadFileOptions( metadata: const { 'project': 'ExampleProject', }, ), );
1await Amplify.Storage.uploadFile(
2 key: 'file',
3 local: File('path/to/file'),
4 options: S3UploadFileOptions(
5 metadata: const {
6 'project': 'ExampleProject',
7 },
8 ),
9);

In S3 console, you should see the metadata attached to your file. You can learn more about the different access levels in File access levels.

S3 Metadata

Multipart upload

Amplify will automatically perform a S3 multipart upload for files larger than 5MB. For more information about S3's multipart upload support, see Uploading and copying objects using multipart upload.