Flutter阿里云OSS存储插件aliyun_oss_plugin的使用
Flutter阿里云OSS存储插件aliyun_oss_plugin的使用
简介
aliyun_oss_plugin
是一个用于在 Flutter 应用中与阿里云对象存储(OSS)进行交互的插件。通过该插件,开发者可以轻松地实现文件的上传、下载等操作。
使用示例
以下是一个完整的示例,展示了如何使用 aliyun_oss_plugin
插件来上传文件到阿里云OSS。
示例代码
import 'package:aliyun_oss_plugin_example/file_util.dart';
import 'package:flutter/material.dart';
import 'package:aliyun_oss_plugin/aliyun_oss_plugin.dart' as AliyunOSSPlugin;
import 'package:aliyun_oss_platform_interface/aliyun_oss_platform_interface.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:uuid/uuid.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
dynamic initResult;
double? progress;
String? error;
bool pause = false;
String objectKey = "";
[@override](/user/override)
void initState() {
super.initState();
initOSS();
AliyunOSSPlugin.addUploadMethodCallListener(UploadMethodCallListener(
init: (initData) {},
progress: (progressData) {
progress = progressData.totalBytesSent / progressData.totalBytesExpectedToSend;
setState(() {});
},
success: (successData) {
objectKey = "";
},
fail: (failData) {
// error = failData.error;
setState(() {});
}));
}
initOSS() async {
initResult = await AliyunOSSPlugin.initialize(
'endpoint',
'',
OSSPlainTextCredentialProvider(
accessKeyId: 'accessKeyId',
secretKeyId: 'secretKeyId',
),
);
setState(() {});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text(error != null
? "上传错误: $error"
: 'Running on: $initResult, progress: $progress'),
),
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton(
onPressed: () async {
await Permission.storage.request();
pickFile(context, (filePath, name) {
String uuid = const Uuid().v4();
AliyunOSSPlugin.upload('', filePath, uuid, uuid);
});
},
child: const Icon(Icons.send),
),
const SizedBox(
height: 10,
),
FloatingActionButton(
onPressed: () async {
final result = pause
? await AliyunOSSPlugin.upload('', objectKey, '', '')
: await AliyunOSSPlugin.cancelUpload(objectKey);
if (result != null) {
setState(() {
pause = !pause;
});
}
},
child: pause ? Icon(Icons.play_arrow) : Icon(Icons.pause)),
],
)),
);
}
}
更多关于Flutter阿里云OSS存储插件aliyun_oss_plugin的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter阿里云OSS存储插件aliyun_oss_plugin的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
阿里云OSS(Object Storage Service)是一种海量、安全、低成本、高可靠的云存储服务。在Flutter中,你可以使用 aliyun_oss_plugin
插件来与阿里云OSS进行交互。以下是如何使用 aliyun_oss_plugin
插件的步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 aliyun_oss_plugin
插件的依赖:
dependencies:
flutter:
sdk: flutter
aliyun_oss_plugin: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 初始化OSS客户端
在使用阿里云OSS之前,你需要初始化OSS客户端。通常在你的 main.dart
或某个初始化文件中进行初始化。
import 'package:aliyun_oss_plugin/aliyun_oss_plugin.dart';
void initOSS() async {
final String endpoint = 'your-oss-endpoint'; // 例如: 'https://oss-cn-hangzhou.aliyuncs.com'
final String accessKeyId = 'your-access-key-id';
final String accessKeySecret = 'your-access-key-secret';
final String securityToken = 'your-security-token'; // 如果有的话
await AliyunOSS.init(
endpoint: endpoint,
accessKeyId: accessKeyId,
accessKeySecret: accessKeySecret,
securityToken: securityToken,
);
}
3. 上传文件
你可以使用 AliyunOSS.upload
方法来上传文件到OSS。
void uploadFile() async {
final String bucketName = 'your-bucket-name';
final String objectKey = 'your-object-key'; // 例如: 'images/my_image.jpg'
final String filePath = 'path/to/your/local/file'; // 本地文件路径
try {
final response = await AliyunOSS.upload(
bucketName: bucketName,
objectKey: objectKey,
filePath: filePath,
);
print('Upload successful: ${response.url}');
} catch (e) {
print('Upload failed: $e');
}
}
4. 下载文件
你可以使用 AliyunOSS.download
方法来从OSS下载文件。
void downloadFile() async {
final String bucketName = 'your-bucket-name';
final String objectKey = 'your-object-key'; // 例如: 'images/my_image.jpg'
final String savePath = 'path/to/save/file'; // 本地保存路径
try {
await AliyunOSS.download(
bucketName: bucketName,
objectKey: objectKey,
savePath: savePath,
);
print('Download successful');
} catch (e) {
print('Download failed: $e');
}
}
5. 删除文件
你可以使用 AliyunOSS.delete
方法来从OSS删除文件。
void deleteFile() async {
final String bucketName = 'your-bucket-name';
final String objectKey = 'your-object-key'; // 例如: 'images/my_image.jpg'
try {
await AliyunOSS.delete(
bucketName: bucketName,
objectKey: objectKey,
);
print('Delete successful');
} catch (e) {
print('Delete failed: $e');
}
}