cloudbase_storage 0.0.1

  • Readme
  • Changelog
  • Installing
  • 57

cloudbase_storage #

cloudbase_storage 是 CloudBase Flutter SDK plugin 之一,提供了在 Flutter 项目中操作 CloudBase 文件存储的能力。

安装 #

在使用 cloudbase_storage 时,你也需要安装 cloudbase_core 等模块

在 flutter 项目的 pubspec.yaml 文件的 dependencies 中添加

dependencies:
  cloudbase_core: ^0.0.1
  cloudbase_storage: ^0.0.1

简单示例 #

import 'package:cloudbase_core/cloudbase_core.dart';
import 'package:cloudbase_storage/cloudbase_storage.dart';

void main() async {
  CloudBaseCore core = CloudBaseCore.init({'envId': 'xxx'});

  // 微信登录
  CloudBaseAuth auth = CloudBaseWxAuth(core);
  bool isLogin = await auth.isLogin();

  if (!isLogin) {
      await auth.login();
  }

  CloudBaseStorage storage = CloudBaseStorage(core);

  String fileId = 'cloud://xxxxx/flutter/data.txt';
  String savePath = 'xxxx';
  await storage.downloadFile(fileId, savePath);
}

API #

上传文件 #

Future<void> uploadFile({String cloudPath, String filePath}) async {}

参数说明:

  • cloudPath:云端文件的路径
  • filePath:本地文件的路径,需要为能直接访问的路径

响应结果:void

上传文件成功时,uploadFile 接口会返回空,失败时则会抛出错误。

调用示例

import 'package:cloudbase_core/cloudbase_core.dart';
import 'package:cloudbase_storage/cloudbase_storage.dart';
import 'package:path_provider/path_provider.dart';

// 获取 flutter Document 路径
// 参考文档:https://flutter.cn/docs/cookbook/persistence/reading-writing-files
_getDocumentsPath() async {
  final directory = await getApplicationDocumentsDirectory();
  String path = directory.path;
  return path;
}

void main() async {
  CloudBaseCore core = CloudBaseCore.init({'envId': 'xxxx'});
  CloudBaseStorage storage = CloudBaseStorage(core);
  // 微信登录
  CloudBaseAuth auth = CloudBaseWxAuth(core);
  bool isLogin = await auth.isLogin();

  if (!isLogin) {
      await auth.login();
  }

  String path = await _getDocumentsPath();

  await storage.uploadFile(
      cloudPath: 'flutter/data.txt', filePath: '$path/data.txt');
}

下载文件 #

Future<void> downloadFile({String fileId, String savePath}) async {}

参数说明

  • fileId:需要下载的文件 id
  • savePath:保存文件的本地路径

响应:void

调用示例

import 'package:cloudbase_core/cloudbase_core.dart';
import 'package:cloudbase_storage/cloudbase_storage.dart';
import 'package:path_provider/path_provider.dart';

void main async {
  // _getDocumentsPath() 同上
  String docPath = await _getDocumentsPath();

  // 本地存储文件的路径
  String savePath = '$docPath/favicon.png';

  // 初始化实例
  CloudBaseCore core = CloudBaseCore.init({'envId': 'xxx'});
  CloudBaseStorage storage = CloudBaseStorage(core);
  // 微信登录
  CloudBaseAuth auth = CloudBaseWxAuth(core);
  bool isLogin = await auth.isLogin();

  if (!isLogin) {
      await auth.login();
  }

  // 下载文件
  String fileId = 'cloud://xxxx';
  await storage.downloadFile(fileId: fileId, savePath: savePath);
}

删除云端文件 #

Future<CloudBaseStorageRes<List<DeleteMetadata>>> deleteFiles(List<String> fileIdList) async {}

参数说明

  • fileIdList:需要删除的文件 Id 组成的数组

响应结果

{
  // 请求 Id
  'requestId': 'xx',
  // 被调函数的返回结果
  'data': [
    {
      'fileId': 'xxx',
      'code': 'SUCCESS'
    }
  ]
}

调用示例

import 'package:cloudbase_core/cloudbase_core.dart';
import 'package:cloudbase_storage/cloudbase_storage.dart';

void main async {
  CloudBaseCore core = CloudBaseCore.init({'envId': 'xxx'});
  CloudBaseStorage storage = CloudBaseStorage(core);
  // 微信登录
  CloudBaseAuth auth = CloudBaseWxAuth(core);
  bool isLogin = await auth.isLogin();

  if (!isLogin) {
      await auth.login();
  }

  String fileId = 'cloud://xxx';
  CloudBaseStorageRes<List<DeleteMetadata>> res = await storage.deleteFiles([fileId]);
  print(res.data[0]); // {'fileId': 'xxx', 'code': 'SUCCESS'}
}

获取文件下载链接 #

Future<CloudBaseStorageRes<List<DownloadMetadata>>> getFileDownloadURL(List<String> fileIdList) async {}

获取文件下载链接,可以自定义实现下载文件的方法。

参数说明

  • fileIdList:需要删除的文件 Id 组成的数组

响应结果

{
  // 请求 Id
  'requestId': 'xx',
  // 被调函数的返回结果
  'data': [
    {
      'fileId': 'xxx',
      'downloadUrl': 'https://xxx'
    }
  ]
}

调用示例

import 'package:cloudbase_core/cloudbase_core.dart';
import 'package:cloudbase_storage/cloudbase_storage.dart';

void main() async{
  CloudBaseCore core = CloudBaseCore.init({'envId': 'xxxx'});
  CloudBaseStorage storage = CloudBaseStorage(core);

  // 微信登录
  CloudBaseAuth auth = CloudBaseWxAuth(core);
  bool isLogin = await auth.isLogin();

  if (!isLogin) {
      await auth.login();
  }

  List<String> fileIds = [
    'cloud://xxxx'
  ];
  CloudBaseStorageRes<List<DeleteMetadata>> res res = await storage.getFileDownloadURL(fileIds);
  print(res.data[0]); // {'fileId': 'xxx', 'downloadUrl': 'https://xxx'}
}

获取上传文件自定义属性 #

Future<CloudBaseStorageRes<UploadMetadata>> getUploadMetadata(String cloudPath) async{}

getUploadMetadata 方法可以获取上传文件需要的属性,可用于实现自定义上传文件的逻辑。

参数说明

  • cloudPath:云端文件路径

响应结果

{
  // 请求 Id
  'requestId': 'xx',
  // 被调函数的返回结果
  'data': {
    // 上传文件的 url
    url: 'xxxx',
    // 访问 token
    token: '',
    // 访问授权信息
    authorization: '',
    // 文件 id
    cosFileId: ''
  }
}

调用示例

import 'package:cloudbase_core/cloudbase_core.dart';
import 'package:cloudbase_storage/cloudbase_storage.dart';

void main() async {
  CloudBaseCore core = CloudBaseCore.init({'envId': 'xxxx'});
  CloudBaseStorage storage = CloudBaseStorage(core);
  // 微信登录
  CloudBaseAuth auth = CloudBaseWxAuth(core);
  bool isLogin = await auth.isLogin();

  if (!isLogin) {
      await auth.login();
  }

  CloudBaseStorageRes<UploadMetadata>> res = await storage.getUploadMetadata('test/index.txt');
  print(res.data);
}

[0.0.1] - 2020.01.14 #

  • 支持使用对象存储

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  cloudbase_storage: ^0.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:cloudbase_storage/cloudbase_storage.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
31
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
60
Overall:
Weighted score of the above. [more]
57
Learn more about scoring.

We analyzed this package on Feb 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Format lib/cloudbase_storage.dart.

Run flutter format to format lib/cloudbase_storage.dart.

Maintenance suggestions

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and cloudbase_storage.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
cloudbase_core ^0.0.1 0.0.2
flutter 0.0.0
Transitive dependencies
charcode 1.1.3
collection 1.14.11 1.14.12
dio 3.0.9
flutter_web_plugins 0.0.0
http_parser 3.1.3
meta 1.1.8
path 1.6.4
shared_preferences 0.5.6+2
shared_preferences_macos 0.0.1+6
shared_preferences_platform_interface 1.0.3
shared_preferences_web 0.1.2+4
sky_engine 0.0.99
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
path_provider ^1.5.1