aws_uploader 0.1.0
aws_uploader: ^0.1.0 copied to clipboard
A Flutter plugin to upload any file to AWS S3 using Cognito Developer Identity (token + identityId). Supports real-time progress streaming, upload cancellation, and multi-part uploads on Android and iOS.
import 'package:flutter/material.dart';
import 'package:aws_uploader/aws_uploader.dart';
import 'package:image_picker/image_picker.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
double _progress = 0;
AWSUploadStatus? _status;
AWSUploadResult? _result;
@override
void initState() {
super.initState();
AwsUploader.progressStream.listen((event) {
setState(() {
_progress = event.progress.toDouble();
_status = event.status;
});
debugPrint('Progress event: $event');
});
}
Future<void> _pickAndUpload() async {
final ImagePicker picker = ImagePicker();
final XFile? file = await picker.pickImage(source: ImageSource.gallery);
if (file == null) return;
setState(() {
_progress = 0;
_status = null;
_result = null;
});
final result = await AwsUploader.startFileUpload(
uploadId: DateTime.now().millisecondsSinceEpoch.toString(),
awsToken: 'AWS TOKEN GENERATED BY YOUR BACKEND',
identityId: 'AWS IDENTITY ID GENERATED BY YOUR BACKEND',
bucketName: 'BUCKET NAME',
filePath: file.path,
fileName: file.name,
uploadFolder: 'stage/testing',
identityPoolId: 'AWS IDENTITY POOL ID FROM AWS CONSOLE',
providerName: 'PROVIDER NAME FROM AWS CONSOLE',
region: 'YOUR BUCKET REGION',
);
setState(() => _result = result);
if (result.success) {
debugPrint('Upload complete: ${result.url}');
} else {
debugPrint('Upload error: ${result.errorMessage}');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('AWS Uploader Plugin')),
body: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ElevatedButton(onPressed: _pickAndUpload, child: const Text('Pick & Upload File')),
const SizedBox(height: 20),
LinearProgressIndicator(value: _progress / 100),
const SizedBox(height: 10),
Text('Progress: ${_progress.toStringAsFixed(0)}%'),
Text('Status: ${_status?.name ?? '—'}'),
if (_result != null) ...[
const SizedBox(height: 16),
if (_result!.success) ...[
Text('Upload ID: ${_result!.uploadId}'),
Text('Bucket: ${_result!.bucket}'),
Text('Key: ${_result!.key}'),
const SizedBox(height: 8),
SelectableText('URL: ${_result!.url}'),
] else
Text(
'Upload failed: ${_result!.errorMessage}',
style: const TextStyle(color: Colors.red),
),
],
],
),
),
),
);
}
}