Page updated Jan 16, 2024

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.

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.

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}

The file_picker package can be used to retrieve arbitrary file types from the user's device.

1import 'dart:io';
2
3import 'package:file_picker/file_picker.dart';
4
5Future<void> uploadFile() async {
6 // Select a file from the device
7 final result = await FilePicker.platform.pickFiles();
8
9 if (result == null) {
10 safePrint('No file selected');
11 return;
12 }
13
14 // Upload file with its filename as the key
15 final platformFile = result.files.single;
16 final path = platformFile.path!;
17 final key = platformFile.name;
18 final file = File(path);
19
20 try {
21 final result = await Amplify.Storage.uploadFile(
22 local: file,
23 key: key,
24 onProgress: (progress) {
25 safePrint('Fraction completed: ${progress.getFractionCompleted()}');
26 },
27 );
28 safePrint('Successfully uploaded file: ${result.key}');
29 } on StorageException catch (e) {
30 safePrint('Error uploading file: $e');
31 }
32}

Upload Options

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

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.