Skip to content

Commit

Permalink
[huhx] -feat: add customize way to get sts token and release v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gohuhx committed Mar 22, 2022
1 parent 67d002e commit 8eb61f7
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 25 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@
* Correct the document

## 0.1.1
* Enable Log output in the dio
* Enable Log output in the dio

## 1.0.0
* Add customize way to get sts information
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ Oss aliyun plugin for flutter. Use sts policy to authenticate the user.
First, add `flutter_oss_aliyun` as a dependency in your `pubspec.yaml` file.
```yaml
dependencies:
flutter_oss_aliyun: ^0.1.1
flutter_oss_aliyun: ^1.0.0
```
Don't forget to `flutter pub get`.

### 1. init the client
### 1. init the client, we provide two ways to do it.
#### use sts server, just provide the sts url from our backend server:
```dart
Client.init(
stsUrl: "server url get sts token",
Expand All @@ -35,6 +36,30 @@ Client.init(
);
```

This sts url api at least return the data:
```json
{
"AccessKeyId": "AccessKeyId",
"AccessKeySecret": "AccessKeySecret",
"SecurityToken": "SecurityToken",
"Expiration": "2022-03-22T11:33:06Z"
}
```

#### you can also customize the way to get sts json response.
```dart
Client.init(
ossEndpoint: "oss-cn-beijing.aliyuncs.com",
bucketName: "back_name",
tokenGetter: () => '''{
"AccessKeyId": "access id",
"AccessKeySecret": "AccessKeySecret",
"SecurityToken": "security token",
"Expiration": "2022-03-22T11:33:06Z"
}'''
);
```

### 2. put the object to oss
```dart
final bytes = "file bytes".codeUnits;
Expand All @@ -51,7 +76,6 @@ await Client().getObject("test.txt");
await Client().downloadObject("test.txt", "./example/test.txt");
```


### 5. delete the object from oss
```dart
await Client().deleteObject("test.txt");
Expand Down
8 changes: 7 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class _HomeScreenState extends State<HomeScreen> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Flutter aliyun oss exa"),
title: const Text("Flutter aliyun oss example"),
),
body: Container(
alignment: Alignment.center,
Expand All @@ -51,6 +51,12 @@ class _HomeScreenState extends State<HomeScreen> {
},
child: const Text("Download object"),
),
TextButton(
onPressed: () async {
await Client().deleteObject("filename.txt");
},
child: const Text("Delete object"),
),
],
),
),
Expand Down
27 changes: 14 additions & 13 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,28 @@ class Client {

factory Client() => _instance!;

final String stsRequestUrl;
final String endpoint;
final String bucketName;
final Function(String) tokenGetter;
final Function? tokenGetter;

Client._(
this.stsRequestUrl,
this.endpoint,
this.bucketName,
this.tokenGetter,
);

static void init({
required String stsUrl,
required String ossEndpoint,
required String bucketName,
}) {
_instance = Client._(stsUrl, ossEndpoint, bucketName, (url) async {
final response = await RestClient.getInstance().get<String>(url);
return response.data!;
});
static void init(
{String? stsUrl,
required String ossEndpoint,
required String bucketName,
String Function()? tokenGetter}) {
assert(stsUrl != null || tokenGetter != null);
final tokenGet = tokenGetter ??
() async {
final response = await RestClient.getInstance().get<String>(stsUrl!);
return response.data!;
};
_instance = Client._(ossEndpoint, bucketName, tokenGet);
}

Auth? _auth;
Expand All @@ -42,7 +43,7 @@ class Client {
/// get auth information from sts server
Future<Auth> _getAuth() async {
if (_isNotAuthenticated()) {
final resp = await tokenGetter(stsRequestUrl);
final resp = await tokenGetter!();
final respMap = jsonDecode(resp);
_auth = Auth(respMap['AccessKeyId'], respMap['AccessKeySecret'],
respMap['SecurityToken']);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_oss_aliyun
description: oss aliyun plugin for flutter and upport STS to access OSS temporarily
version: 0.1.1
version: 1.0.0
repository: https://github.com/huhx/flutter_oss_aliyun

environment:
Expand Down
16 changes: 10 additions & 6 deletions test/flutter_oss_aliyun_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import 'package:flutter_oss_aliyun/flutter_oss_aliyun.dart';
void main() {
test("test the put object in Client", () async {
Client.init(
stsUrl: "server url",
stsUrl: "**",
ossEndpoint: "oss-cn-beijing.aliyuncs.com",
bucketName: "***",
bucketName: "**",
);

final resp = await Client().putObject("Hello World".codeUnits, "test.txt");
Expand All @@ -18,10 +18,14 @@ void main() {

test("test the get object in Client", () async {
Client.init(
stsUrl: "server url",
ossEndpoint: "oss-cn-beijing.aliyuncs.com",
bucketName: "***",
);
ossEndpoint: "oss-cn-beijing.aliyuncs.com",
bucketName: "back_name",
tokenGetter: () => '''{
"AccessKeyId": "access id",
"AccessKeySecret": "AccessKeySecret",
"SecurityToken": "security token",
"Expiration": "2022-03-22T11:33:06Z"
}''');

final resp = await Client().getObject("test.txt");

Expand Down

0 comments on commit 8eb61f7

Please sign in to comment.