flutter_oss_aliyun 5.0.4 copy "flutter_oss_aliyun: ^5.0.4" to clipboard
flutter_oss_aliyun: ^5.0.4 copied to clipboard

oss aliyun plugin for flutter and upport STS to access OSS temporarily

Language: 中文简体 | English

flutter_oss_aliyun #

一个访问阿里云oss并且支持STS临时访问凭证访问OSS的flutter库,基本上涵盖阿里云oss sdk的所有功能。⭐

flutter pub: https://pub.dev/packages/flutter_oss_aliyun

oss sts document: https://help.aliyun.com/document_detail/100624.html

🐱 功能 #

  • 上传文件
  • 下载文件
  • 下载并保存文件
  • 删除文件
  • 多文件上传
  • 多文件删除
  • 上传文件的进度回调函数
  • 下载文件的进度回调函数
  • 获取签名的文件url
  • 获取多个签名的文件url
  • 列举所有的bucket
  • 列举bucket中所有文件
  • 获取bucket信息
  • 获取bucket的储容量以及文件数量
  • 上传本地文件
  • 批量上传本地文件
  • 获取文件元信息
  • 获取region的信息
  • bucket acl的crud操作
  • bucket policy的crud操作

🎨 使用 #

添加依赖

dependencies:
  flutter_oss_aliyun: ^5.0.4

1. 初始化oss client, 这里我们提供两种方式 #

提供sts server地址,需要后端添加这个api

Client.init(
    stsUrl: "server url get sts token",
    ossEndpoint: "oss-cn-beijing.aliyuncs.com",
    bucketName: "bucket name",
);

后端api至少需要返回以下数据:

{
  "AccessKeyId": "AccessKeyId",
  "AccessKeySecret": "AccessKeySecret",
  "SecurityToken": "SecurityToken",
  "Expiration": "2022-03-22T11:33:06Z"
}

当然你可以自定义使用其他的方式返回以下的json数据.

Client.init(
    ossEndpoint: "oss-cn-beijing.aliyuncs.com",
    bucketName: "bucketName",
    tokenGetter: _tokenGetterMethod
);

String _tokenGetterMethod() async {
  return '''{
        "AccessKeyId": "access id",
        "AccessKeySecret": "AccessKeySecret",
        "SecurityToken": "security token",
        "Expiration": "2022-03-22T11:33:06Z"
    }''';
}

你可以传入自定义的Dio

在init函数中,你可以传入dio,做到dio的定制化。比如日志或者其他的interceptors.

Client.init(
    ossEndpoint: "oss-cn-beijing.aliyuncs.com",
    bucketName: "bucketName",
    tokenGetter: _tokenGetterMethod,
    dio: Dio()
);

2. 上传文件附带进度回调 #

  • 存储类型:https://help.aliyun.com/document_detail/51374.htm?spm=a2c4g.11186623.0.0.56632b55htpEQX#concept-fcn-3xt-tdb
  • acl策略:https://help.aliyun.com/document_detail/100676.htm?spm=a2c4g.11186623.0.0.56637952SnxOWV#concept-blw-yqm-2gb
final bytes = "file bytes".codeUnits;

await Client().putObject(
  bytes,
  "test.txt",
  option: PutRequestOption(
    onSendProgress: (count, total) {
      print("send: count = $count, and total = $total");
    },
    onReceiveProgress: (count, total) {
      print("receive: count = $count, and total = $total");
    },
    isOverwrite: false,
    acl: AclMode.publicRead,
    storageType: StorageType.ia,
  ),
);

3. 批量上传文件 #

await Client().putObjects([
  AssetEntity(
    filename: "filename1.txt",
    bytes: "files1".codeUnits,
    option: PutRequestOption(
      onSendProgress: (count, total) {
        print("send: count = $count, and total = $total");
      },
      onReceiveProgress: (count, total) {
        print("receive: count = $count, and total = $total");
      },
      acl: AclMode.private,
    ),
  ),
  AssetEntity(filename: "filename2.txt", bytes: "files2".codeUnits),
]);

4. 上传本地文件 #

final Response<dynamic> resp = await Client().putObjectFile(
  File("/Users/aaa.pdf"),
  fileKey: "aaa.png",
  option: PutRequestOption(
    onSendProgress: (count, total) {
      print("send: count = $count, and total = $total");
    },
    onReceiveProgress: (count, total) {
      print("receive: count = $count, and total = $total");
    },
    acl: AclMode.private,
  ),
);

5. 批量上传本地文件 #

final List<Response<dynamic>> resp = await Client().putObjectFiles(
  [
    AssetFileEntity(
      file: File("//Users/private.txt"),
      option: PutRequestOption(
        onSendProgress: (count, total) {
          print("send: count = $count, and total = $total");
        },
        onReceiveProgress: (count, total) {
          print("receive: count = $count, and total = $total");
        },
        isOverride: false,
        acl: AclMode.private,
      ),
    ),
    AssetFileEntity(
      file: File("//Users/splash.png"),
      filename: "aaa.png",
      option: PutRequestOption(
        onSendProgress: (count, total) {
          print("send: count = $count, and total = $total");
        },
        onReceiveProgress: (count, total) {
          print("receive: count = $count, and total = $total");
        },
        isOverride: true,
      ),
    ),
  ],
);

6. 下载文件附带进度回调 #

await Client().getObject(
  "test.txt",
  onReceiveProgress: (count, total) {
    debugPrint("received = $count, total = $total");
  },
);

7. 下载并保存文件附带进度回调 #

await Client().downloadObject(
  "test.txt",
  "./example/test.txt",
  onReceiveProgress: (count, total) {
    debugPrint("received = $count, total = $total");
  },
);

8. 删除文件 #

await Client().deleteObject("test.txt");

9. 批量删除文件 #

await Client().deleteObjects(["filename1.txt", "filename2.txt"]);

10. 获取已签名的文件url,这个url可以直接在浏览器访问 #

需要注意的是:这个操作并不安全,因为url包含security-token信息,即使过期时间比较短

final String url = await Client().getSignedUrl("filename1.txt");

11. 获取多个已签名的文件url #

需要注意的是:这个操作并不安全,因为url包含security-token信息,即使过期时间比较短

final Map<String, String> result = await Client().getSignedUrls(["test.txt", "filename1.txt"]);

12. 列举所有的存储空间 #

列举请求者拥有的所有存储空间(Bucket)。您还可以通过设置prefix、marker或者max-keys参数列举满足指定条件的存储空间。参考:https://help.aliyun.com/document_detail/31957.html

final Response<dynamic> resp = await Client().listBuckets({"max-keys": 2});

13. 列举存储空间中所有文件 #

接口用于列举存储空间(Bucket)中所有文件(Object)的信息。请求参数和返回结果,请参考: https://help.aliyun.com/document_detail/187544.html

final Response<dynamic> resp = await Client().listFiles({});

14. 获取bucket信息 #

查看存储空间(Bucket)的相关信息。返回结果请参考: https://help.aliyun.com/document_detail/31968.html

final Response<dynamic> resp = await Client().getBucketInfo();

15. 获取bucket的储容量以及文件数量 #

获取指定存储空间(Bucket)的存储容量以及文件(Object)数量。返回结果请参考: https://help.aliyun.com/document_detail/426056.html

final Response<dynamic> resp = await Client().getBucketStat();

16. 获取文件元信息 #

final Response<dynamic> resp = await Client().getObjectMeta("huhx.csv");

17. regions的查询 #

  • 查询所有
final Response<dynamic> resp = await Client().getAllRegions();
  • 查询特定
final Response<dynamic> resp = await Client().getRegion("oss-ap-northeast-1");

18. bucket acl的操作 #

  • 查询
final Response<dynamic> resp = await Client().getBucketAcl(
  bucketName: "bucket-name",
);
  • 更新
final Response<dynamic> resp = await Client().putBucketAcl(
  AciMode.publicRead, 
  bucketName: "bucket-name",
);

19. bucket policy的操作 #

  • 查询
final Response<dynamic> resp = await Client().getBucketPolicy(
  bucketName: "bucket-name",
);
  • 更新
final Response<dynamic> resp = await Client().putBucketPolicy(
  {}, 
  bucketName: "bucket-name",
);
  • 删除
final Response<dynamic> resp = await Client().deleteBucketPolicy(
  bucketName: "bucket-name",
);

Drop a ⭐ if it is help to you. #

44
likes
0
pub points
92%
popularity

Publisher

verified publisherhuhx.cn

oss aliyun plugin for flutter and upport STS to access OSS temporarily

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

async, crypto, dio, flutter, mime_type

More

Packages that depend on flutter_oss_aliyun