Skip to content

Commit

Permalink
support api:
Browse files Browse the repository at this point in the history
* getService
* listObjects
* listObjectVersions
* headBucket
* putObject
* getObject
* headObject
* deleteObject
* deleteMultipleObject
* putObjectTagging
* deleteObjectTagging
* getObjectTagging
  • Loading branch information
meetleev committed Oct 12, 2022
1 parent 5bf1409 commit 4ebda83
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 64 deletions.
3 changes: 0 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:example/test_page.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -30,5 +29,3 @@ class MyApp extends StatelessWidget {
);
}
}


15 changes: 10 additions & 5 deletions example/lib/test_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class _TestPageState extends State<TestPage> {
headers: {
CosHeaders.xCosMeta('test'): convert.jsonEncode(meta),
CosHeaders.xCosTagging: meta.keys
.map((key) => '${Uri.encodeComponent(key)}=${Uri.encodeComponent(meta[key].toString())}')
.map((key) =>
'${Uri.encodeComponent(key)}=${Uri.encodeComponent(meta[key].toString())}')
.toList()
.join('&')
},
Expand All @@ -74,13 +75,15 @@ class _TestPageState extends State<TestPage> {
print('onReceiveProgress---$cur:$total');
});
if (null != res.data) {
var utf8 = convert.utf8.decode(res.data!.objectData, allowMalformed: true);
var utf8 =
convert.utf8.decode(res.data!.objectData, allowMalformed: true);
print('utf8----$utf8');
}
print('_getObject res ==> $res');
}

Future<void> _deleteObjectTagging({String objectKey = _defaultObjectKey}) async {
Future<void> _deleteObjectTagging(
{String objectKey = _defaultObjectKey}) async {
await _ensureCosClientNonNull();
var res = await _client!.deleteObjectTagging(
region: _region,
Expand Down Expand Up @@ -121,7 +124,8 @@ class _TestPageState extends State<TestPage> {
print('_deleteObject res ==> $res');
}

Future<void> _deleteMultipleObject({List<CosObjectVersionParams>? deletes}) async {
Future<void> _deleteMultipleObject(
{List<CosObjectVersionParams>? deletes}) async {
await _ensureCosClientNonNull();
var res = await _client!.deleteMultipleObject(
region: _region,
Expand Down Expand Up @@ -196,7 +200,8 @@ class _TestPageState extends State<TestPage> {
var url = 'http://127.0.0.1:8080/getQCloudCosCredential';
var req = await client.postUrl(Uri.parse(url));
req.headers.set(HttpHeaders.contentTypeHeader, 'application/json');
req.add(convert.utf8.encode(convert.jsonEncode(['cos:DeleteMultipleObjects'])));
req.add(
convert.utf8.encode(convert.jsonEncode(['cos:DeleteMultipleObjects'])));
var res = await req.close();
if (kDebugMode) {
print('_requestTempCredential-res---${res.statusCode}---');
Expand Down
19 changes: 14 additions & 5 deletions lib/src/cos_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import 'cos_client.dart';
import 'logger.dart';

List<String> handleHeaderOrParams(Map<String, String?> params) {
params = params.map(
(key, value) => MapEntry(Uri.encodeComponent(key.toLowerCase()), Uri.encodeComponent(value ?? '')));
params = params.map((key, value) => MapEntry(
Uri.encodeComponent(key.toLowerCase()),
Uri.encodeComponent(value ?? '')));
var keys = params.keys.toList()..sort();
var values = keys.map((e) => '$e=${params[e]}').toList().join('&');
return [keys.join(';'), values];
Expand Down Expand Up @@ -71,13 +72,21 @@ class CosS3Auth {
var headList = sHttpArr[0];
var httpHeaders = sHttpArr[1];
String pathName = !key.startsWith('/') ? '/$key' : key;
String formatString = [method.toLowerCase(), pathName, httpParams, httpHeaders, ''].join('\n');
String formatString = [
method.toLowerCase(),
pathName,
httpParams,
httpHeaders,
''
].join('\n');
Log.d('Auth formatString---$formatString');
String signHttpString = hex.encode(sha1.convert(formatString.codeUnits).bytes);
String signHttpString =
hex.encode(sha1.convert(formatString.codeUnits).bytes);
String stringToSign = ['sha1', qKeyTime, signHttpString, ''].join('\n');
Log.d('Auth stringToSign----$stringToSign');
String signature = hmacSha1(signKey, stringToSign);
String authorization = 'q-sign-algorithm=sha1&q-ak=$secretId&q-sign-time=$qKeyTime'
String authorization =
'q-sign-algorithm=sha1&q-ak=$secretId&q-sign-time=$qKeyTime'
'&q-key-time=$qKeyTime&q-header-list=$headList'
'&q-url-param-list=$urlParamList&q-signature=$signature';
Log.d('Auth authorization---$authorization');
Expand Down
Loading

0 comments on commit 4ebda83

Please sign in to comment.