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

Page updated May 16, 2024

Upload files

Storage for Gen 2 is not yet available for Flutter

Implement upload functionality

Note: Refer to the Transfer Acceleration documentation to learn how to enable transfer acceleration for storage APIs.

Upload from file

Note: To use AWSFilePlatform, add aws_common package to your Flutter project by running: flutter pub add aws_common

import 'package:amplify_flutter/amplify_flutter.dart';
Future<void> uploadFile() async {
try {
final result = await Amplify.Storage.uploadFile(
localFile: AWSFile.fromPath('/path/to/local/file.txt'),
path: const StoragePath.fromString('public/file.txt'),
).result;
safePrint('Uploaded file: ${result.uploadedItem.path}');
} on StorageException catch (e) {
safePrint(e.message);
}
}
import 'dart:io' show File;
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:aws_common/vm.dart';
Future<void> uploadFile(File file) async {
try {
final result = await Amplify.Storage.uploadFile(
localFile: AWSFilePlatform.fromFile(file),
path: const StoragePath.fromString('public/file.png'),
).result;
safePrint('Uploaded file: ${result.uploadedItem.path}');
} on StorageException catch (e) {
safePrint(e.message);
}
}
import 'dart:html' show File;
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:aws_common/web.dart';
Future<void> uploadFile(File file) async {
final awsFile = AWSFilePlatform.fromFile(file);
try {
final result = await Amplify.Storage.uploadFile(
localFile: awsFile,
path: const StoragePath.fromString('public/file.png'),
).result;
safePrint('Uploaded file: ${result.uploadedItem.path}');
} on StorageException catch (e) {
safePrint(e.message);
}
}

Upload from Flutter's file_picker plugin

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

import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:file_picker/file_picker.dart';
Future<void> uploadImage() async {
// Select a file from the device
final result = await FilePicker.platform.pickFiles(
type: FileType.custom,
withData: false,
// Ensure to get file stream for better performance
withReadStream: true,
allowedExtensions: ['jpg', 'png', 'gif'],
);
if (result == null) {
safePrint('No file selected');
return;
}
// Upload file using the filename
final platformFile = result.files.single;
try {
final result = await Amplify.Storage.uploadFile(
localFile: AWSFile.fromStream(
platformFile.readStream!,
size: platformFile.size,
),
path: StoragePath.fromString('public/${platformFile.name}'),
onProgress: (progress) {
safePrint('Fraction completed: ${progress.fractionCompleted}');
},
).result;
safePrint('Successfully uploaded file: ${result.uploadedItem.path}');
} on StorageException catch (e) {
safePrint(e.message);
}
}

Upload from data

To upload from a data object, specify the path and data, where data is an instance of S3DataPayload created from various data formats.

Future<void> uploadData() async {
try {
final result = await Amplify.Storage.uploadData(
data: StorageDataPayload.string(
'hello world',
contentType: 'text/plain',
),
path: const StoragePath.fromString('public/example.txt'),
).result;
safePrint('Uploaded data: ${result.uploadedItem.path}');
} on StorageException catch (e) {
safePrint(e.message);
}
}
Future<void> uploadData() async {
try {
final result = await Amplify.Storage.uploadData(
data: StorageDataPayload.json({
'title': 'example',
'author': {
'firstName': 'Jane',
'lastName': 'Doe',
},
}),
path: const StoragePath.fromString('public/example.json'),
).result;
safePrint('Uploaded data: ${result.uploadedItem.path}');
} on StorageException catch (e) {
safePrint(e.message);
}
}

See more info about data URL.

Future<void> uploadData() async {
// dataUrl should be a valid Data Url.
// see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs
const dataUrl = 'data:text/plain;charset=utf-8;base64,aGVsbG8gd29ybGQ=';
try {
final result = await Amplify.Storage.uploadData(
data: StorageDataPayload.dataUrl(dataUrl),
path: const StoragePath.fromString('public/example.txt'),
).result;
safePrint('Uploaded data: ${result.uploadedItem.path}');
} on StorageException catch (e) {
safePrint(e.message);
}
}
Future<void> uploadBytes() async {
try {
final bytes = 'hello world'.codeUnits;
final result = await Amplify.Storage.uploadData(
data: StorageDataPayload.bytes(
bytes,
contentType: 'text/plain',
),
path: const StoragePath.fromString('public/example.txt'),
).result;
safePrint('Uploaded data: ${result.uploadedItem.path}');
} on StorageException catch (e) {
safePrint(e.message);
}
}

Monitor upload progress

final operation = Amplify.Storage.uploadFile(
localFile: AWSFile.fromPath('/path/to/local/file'),
path: const StoragePath.fromString('public/example.txt'),
onProgress: (progress) {
safePrint('fraction totalBytes: ${progress.totalBytes}');
safePrint('fraction transferredBytes: ${progress.transferredBytes}');
safePrint('fraction completed: ${progress.fractionCompleted}');
}
);

Pause, resume, and cancel uploads

A call to Amplify.Storage.uploadFile or Amplify.Storage.uploadData returns a reference to the operation that is performing the upload.

Future<void> upload() async {
final operation = Amplify.Storage.uploadFile(
localFile: AWSFile.fromPath('/path/to/local/file'),
path: const StoragePath.fromString('public/example.txt'),
);
// pause operation
await operation.pause();
// resume operation
await operation.resume();
// cancel operation
await operation.cancel();
}

More upload options

OptionTypeDescription
getPropertiesbooleanWhether to retrieve properties for the uploaded object using theAmplify.Storage.getProperties() after the operation completes. When set to true the returned item will contain additional info such as metadata and content type.
useAccelerateEndpointbooleanWhether to use accelerate endpoint.

Read more at Transfer Acceleration

Example of uploadFile with options:

final operation = Amplify.Storage.uploadFile(
localFile: AWSFile.fromPath('/path/to/local/file'),
path: const StoragePath.fromString('public/example.txt'),
options: const StorageUploadFileOptions(
metadata: {'key': 'value'},
pluginOptions: S3UploadFilePluginOptions(
getProperties: true,
useAccelerateEndpoint: true,
),
),
);

Example of uploadData with options:

final operation = Amplify.Storage.uploadData(
data: StorageDataPayload.string('example'),
path: const StoragePath.fromString('public/example.txt'),
options: const StorageUploadDataOptions(
metadata: {'key': 'value'},
pluginOptions: S3UploadDataPluginOptions(
getProperties: true,
useAccelerateEndpoint: true,
),
),
);

MultiPart upload

Amplify will automatically perform an Amazon S3 multipart upload for objects that are larger than 5MB. For more information about S3's multipart upload, see Uploading and copying objects using multipart upload