Flutter AWS存储服务插件aws_storage_service的使用
Flutter AWS存储服务插件aws_storage_service的使用
aws_storage_service
是一个用于在 Dart 和 Flutter 应用程序中处理文件上传、多部分上传和下载的简单包。以下是该插件的使用示例和详细说明。
示例代码
文件上传
import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:aws_storage_service/src/aws_storage_service.dart';
AwsCredentialsConfig credentialsConfig = AwsCredentialsConfig(
accessKey: 'YOURACCESSKEY', // 这是一个测试访问密钥
bucketName: 'testbucket', // 桶名称
region: 'us-west-2', // 您的 AWS 桶所在区域
secretKey: 'YOURAWSSECRETKEY', // 您的密钥
);
Future testFileUpload() async {
File file = File(p.join(Directory.current.path, 'web.pdf'));
print(file.lengthSync());
UploadTaskConfig config = UploadTaskConfig(
credentailsConfig: credentialsConfig,
url: 'file/web23.pdf',
uploadType: UploadType.file,
file: file,
);
UploadFile uploadFile = UploadFile(config: config);
uploadFile.uploadProgress.listen((event) {
print('${event[0]}/ ${event[1]}');
});
await uploadFile.upload().then((value) {
print(value);
uploadFile.dispose();
});
}
多部分上传
Future testMultipartUpload() async {
String imageName = 'test.mp4';
File file = File(p.join(Directory.current.path, 'test_files', imageName));
MultipartUploadConfig config = MultipartUploadConfig(
file: file,
url: 'file/f$imageName',
credentailsConfig: credentialsConfig,
);
MultipartFileUpload multipartFileUpload = MultipartFileUpload(
config: config,
onError: (error, list, s) {
print('An error occurred $error');
},
onVersionIdCreated: (versionId) => print('version id created $versionId'),
onPartUploadComplete: (etagList, versionId) => print(
'Part upload completed ---> etagList is $etagList and versionId is $versionId'),
onUploadComplete: (etagList, versionId) => print(
'Part upload completed ---> etagList is $etagList and versionId is $versionId'),
);
multipartFileUpload.uploadCompletedState.listen((event) {
// Event is a boolean. This is true when the file upload is done
print('Upload State $event');
});
multipartFileUpload.uploadProgress.listen((event) {
print('Upload progress \n${event[0]} / ${event[1]}');
});
bool preparingSuccessful = await multipartFileUpload.prepareMultipartRequest();
if (preparingSuccessful) {
await multipartFileUpload.upload();
}
}
恢复多部分上传
Future testResumeMultipartUpload() async {
String imageName = 'test.mp4'; // 要上传的文件名
File file = File(p.join(Directory.current.path, 'test_files', imageName));
// EtagLists 是已上传的 Etag 列表。
// 对于每次上传到 AWS 的块,都会返回一个 Etag ID。
// 包含了一个方法,每次成功上传一个块时都会返回所有已上传的 Etag 列表。
final eTagLists = [
[2, "1ee28a7ac06786aff26d47298674c847"],
[1, "18cc73a82389202aa085ee5751666726"],
[4, "bf41ee73393dbfa1a5327fbb50aff054"],
[5, "2848ec759573e3c5f6cadf1145e9efd9"]
];
// 版本 ID 是在上传开始时分配给此上传的唯一 ID。
final versionIdOrUploadId =
'Zm9F2QhTuSZQHhnw1O8sThNk9fZ71lAUf20oTUQ11NrUaQy5DUwZ8oZ7fXuxCZywyY8mwUqsa54M3yg1S2QFIWGV.kv_QiMAxGlNHC55OAzBlDIusdJJ2jLF7qaQk9yg';
// 创建配置对象。
MultipartUploadConfig config = MultipartUploadConfig(
credentailsConfig: credentialsConfig,
file: file,
url: 'file/f$imageName',
resumeMultipart: true,
versionId: versionIdOrUploadId,
etagsLists: eTagLists,
);
MultipartFileUpload multipartFileUpload = MultipartFileUpload(
config: config,
onError: (error, list, s) {
print('An error occurred $error');
},
onVersionIdCreated: (versionId) => print('version id created $versionId'),
onPartUploadComplete: (etagList, versionId) => print(
'Part upload completed ---> etagList is $etagList and versionId is $versionId'),
onUploadComplete: (etagList, versionId) => print(
'Part upload completed ---> etagList is $etagList and versionId is $versionId'),
);
multipartFileUpload.uploadCompletedState.listen((event) {
// Event is a boolean. This is true when the file upload is done
print('Upload State $event');
});
multipartFileUpload.uploadProgress.listen((event) {
print('Upload progress \n${event[0]} / ${event[1]}');
});
bool preparingSuccessful = await multipartFileUpload.prepareMultipartRequest();
if (preparingSuccessful) {
await multipartFileUpload.upload();
}
}
下载文件
Future testDownload() async {
// 新建一个下载任务
String filePath = p.join(Directory.current.path, 'test_download.mp4');
String fileUrl = 'file/ftest.mp4'; // 文件在桶中的 URL
DownloadFileConfig config = DownloadFileConfig(
credentailsConfig: credentialsConfig,
url: fileUrl,
downloadPath: filePath,
);
DownloadFile downloadFile = DownloadFile(
config: config,
onRecieveProgress: ((totalDownloaded, totalSize) =>
print('Upload Status Callback ===> $totalDownloaded/$totalSize')),
errorCallback: (errorMessage, statusCode) =>
print('An error occurred $errorMessage'),
);
bool prepSuccessful = await downloadFile.prepareDownload();
print('The download was prepared Successfully $prepSuccessful');
downloadFile.downloadProgress.listen((event) {
print('Upload Status Stream ===> ${event[0]}/${event[1]}');
});
if (prepSuccessful) {
await downloadFile.download().then((value) {
downloadFile.dispose();
});
}
}
恢复下载
Future testResumeDownload() async {
// 这个测试上传文件
String filePath = p.join(Directory.current.path, 'test_download.mp4');
String fileUrl = 'file/ftest.mp4'; // 文件在桶中的 URL
// 创建一个下载配置
// 下载配置包含了下载器需要的所有配置信息来下载文件
DownloadFileConfig config = DownloadFileConfig(
credentailsConfig: credentialsConfig,
url: fileUrl,
resumeDownload: true, // 如果要恢复下载,则设置为 true
downloadPath: filePath, // 要恢复的文件路径
);
DownloadFile downloadFile = DownloadFile(
config: config,
onRecieveProgress: ((totalDownloaded, totalSize) =>
print('Upload Status Callback ===> $totalDownloaded/$totalSize')),
errorCallback: (errorMessage, statusCode) =>
print('An error occurred $errorMessage'),
); // 创建一个下载文件实例
bool prepSuccessful = await downloadFile.prepareDownload();
print('The download was prepared Successfully $prepSuccessful');
downloadFile.downloadProgress.listen((event) {
// 您可以监听下载进度
print('Upload Status Stream ===> ${event[0]}/${event[1]}');
});
if (prepSuccessful) {
await downloadFile.download().then((value) {
downloadFile.dispose();
});
}
}
更多关于Flutter AWS存储服务插件aws_storage_service的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复