Flutter AWS S3存储插件amplify_storage_s3_dart的使用
Flutter AWS S3 存储插件 amplify_storage_s3_dart 的使用
Dart-only 实现的 Amplify Storage 插件。这是在 amplify_storage_s3
Flutter 库内部使用的。
示例代码
以下是一个使用 amplify_storage_s3_dart
插件的完整示例 Demo。
import 'package:flutter/material.dart';
import 'package:amplify_storage_s3_dart/amplify_storage_s3_dart.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _storagePlugin = AmplifyStorageS3Dart();
@override
void initState() {
super.initState();
// 初始化 Amplify 插件
_configureAmplify();
}
// 配置 Amplify 插件
Future<void> _configureAmplify() async {
try {
// 添加 AWS S3 存储插件
await Amplify.addPlugin(
storage: _storagePlugin,
);
// 配置 Amplify
await Amplify.configure(amplifyconfig);
print('Successfully configured');
} catch (e) {
print('Error configuring Amplify: $e');
}
}
// 上传文件到 S3
Future<void> _uploadFile() async {
try {
final result = await Amplify.Storage.uploadFile(
key: 'my-file.txt',
localFile: File('/path/to/local/file.txt'),
options: S3UploadFileOptions(
accessLevel: StorageAccessLevel.guest,
),
);
print('Uploaded file: ${result.key}');
} catch (e) {
safePrint('Failed to upload file: $e');
}
}
// 下载文件从 S3
Future<void> _downloadFile() async {
try {
final result = await Amplify.Storage.downloadFile(
key: 'my-file.txt',
destination: Directory('/path/to/download/directory'),
options: S3DownloadFileOptions(
accessLevel: StorageAccessLevel.guest,
),
);
print('Downloaded file: ${result.key}');
} catch (e) {
safePrint('Failed to download file: $e');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Amplify Storage S3 Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _uploadFile,
child: Text('上传文件'),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _downloadFile,
child: Text('下载文件'),
),
],
),
),
),
);
}
}
说明
-
初始化 Amplify 插件
_configureAmplify()
这个方法用于配置和添加 AWS S3 存储插件。
-
上传文件
_uploadFile()
这个方法用于将本地文件上传到 S3 存储桶。
-
下载文件
_downloadFile()
更多关于Flutter AWS S3存储插件amplify_storage_s3_dart的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter AWS S3存储插件amplify_storage_s3_dart的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,关于Flutter中使用amplify_storage_s3_dart
插件来与AWS S3进行交互,以下是一个基本的使用示例,展示了如何配置、上传和下载文件。
首先,确保你已经安装了amplify_flutter
和amplify_storage_s3
插件。在你的pubspec.yaml
文件中添加以下依赖:
dependencies:
flutter:
sdk: flutter
amplify_flutter: ^0.x.y # 请替换为最新版本号
amplify_storage_s3: ^0.x.y # 请替换为最新版本号
然后运行flutter pub get
来安装这些依赖。
配置Amplify
在使用Amplify之前,你需要在AWS Amplify控制台中配置你的项目,并获取配置文件。在Flutter项目中,你需要将这个配置文件(通常是amplifyconfiguration.dart
)放在项目的根目录下。
接下来,在你的应用中初始化Amplify:
import 'package:amplify_flutter/amplify_flutter.dart';
import 'amplifyconfiguration.dart'; // 确保这个文件在你的项目根目录下
Future<void> configureAmplify() async {
try {
await Amplify.addPlugins([
AmplifyStorageS3()
// 你可以在这里添加其他插件,如Auth, API等
]);
await Amplify.configure(amplifyconfig);
print('Amplify configured successfully');
} catch (e) {
print('Failed to configure Amplify: $e');
}
}
在你的main.dart
文件或任何适当的生命周期方法中调用configureAmplify()
,例如在initState
中:
@override
void initState() {
super.initState();
configureAmplify();
}
上传文件到S3
import 'package:amplify_storage_s3/amplify_storage_s3.dart';
import 'dart:io';
Future<void> uploadFile(File file) async {
try {
String key = 'my-unique-file-key.txt'; // 在S3中的文件名
StorageUploadFileOptions options = StorageUploadFileOptions(
accessLevel: StorageAccessLevel.guest, // 访问级别,可以是guest, protected, 或private
);
await Amplify.Storage.uploadFile(
key: key,
file: file,
options: options,
);
print('File uploaded successfully');
} catch (e) {
print('Failed to upload file: $e');
}
}
下载文件从S3
Future<File?> downloadFile(String key) async {
try {
StorageDownloadFileResult result = await Amplify.Storage.downloadFile(
key: key,
);
File file = File(result.localUri.toFilePath());
print('File downloaded successfully: ${file.path}');
return file;
} catch (e) {
print('Failed to download file: $e');
return null;
}
}
完整示例
以下是一个完整的示例,展示了如何配置Amplify、上传和下载文件:
import 'package:flutter/material.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:amplify_storage_s3/amplify_storage_s3.dart';
import 'dart:io';
import 'amplifyconfiguration.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('AWS S3 with Flutter'),
),
body: MyHomePage(),
),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
configureAmplify();
}
Future<void> configureAmplify() async {
try {
await Amplify.addPlugins([AmplifyStorageS3()]);
await Amplify.configure(amplifyconfig);
print('Amplify configured successfully');
} catch (e) {
print('Failed to configure Amplify: $e');
}
}
Future<void> uploadFileExample() async {
File file = File('path/to/your/local/file.txt'); // 替换为你的文件路径
await uploadFile(file);
}
Future<void> downloadFileExample() async {
String key = 'my-unique-file-key.txt'; // 替换为你上传时使用的key
File? downloadedFile = await downloadFile(key);
if (downloadedFile != null) {
// 处理下载的文件
print('Downloaded file content: ${downloadedFile.readAsStringSync()}');
}
}
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: uploadFileExample,
child: Text('Upload File'),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: downloadFileExample,
child: Text('Download File'),
),
],
),
);
}
Future<void> uploadFile(File file) async {
try {
String key = 'my-unique-file-key.txt';
StorageUploadFileOptions options = StorageUploadFileOptions(
accessLevel: StorageAccessLevel.guest,
);
await Amplify.Storage.uploadFile(
key: key,
file: file,
options: options,
);
print('File uploaded successfully');
} catch (e) {
print('Failed to upload file: $e');
}
}
Future<File?> downloadFile(String key) async {
try {
StorageDownloadFileResult result = await Amplify.Storage.downloadFile(
key: key,
);
File file = File(result.localUri.toFilePath());
print('File downloaded successfully: ${file.path}');
return file;
} catch (e) {
print('Failed to download file: $e');
return null;
}
}
}
请确保替换示例中的文件路径和S3键名为你实际的文件路径和键名。同时,不要忘记在AWS Amplify控制台中配置你的项目,并正确设置amplifyconfiguration.dart
文件。