欢迎您光临深圳塔灯网络科技有限公司!
电话图标 余先生:13699882642

网站百科

为您解码网站建设的点点滴滴

flutter使用腾讯云COS对象服务

发表日期:2019-01 文章编辑:小灯 浏览次数:3252

官方文档:请求签名, Get Object

转载请注明出处: https://www.jianshu.com/p/49fe86909d18

Overview

本文使用dart按照腾讯COS文档生成签名,并使用签名下载COS文件对象到本地(使用flutter_download包)

载入crypto以及flutter_downloader包

pubspec.yaml文件中加入依赖(flutter_downloader文档,crypto文档)

Android: flutter_downloader 需要配置权限,请参考其文档。

dependencies: flutter: sdk: flutter ........ crypto: ^2.0.6 flutter_downloader: ^1.1.3 

实现cos签名以及下载文件

import 'dart:convert';import 'package:crypto/crypto.dart'; import 'package:flutter_downloader/flutter_downloader.dart';class TencentCos { static bool debug = true; /// auth signature expired time in seconds static final int signExpireTimeInSeconds = '10';static final String secretId = 'tencentSerertId'; static final String secretKey = 'tencentSecretKey'; static final String bucketHost = 'yourBucketHost'; // xxx-xxxxxx.cos.ap-chengdu.myqcloud.comstatic TencentCos _cos;TencentCos._();static TencentCos get() { if (_cos == null) { _cos = TencentCos._(); }return _cos; } /// download $fileName and save to $saveDir(absolute path) Future<String> downloadFile(String fileName, String saveDir) async { var url = '/$fileName'; final taskId = await FlutterDownloader.enqueue( headers: buildHeaders(url), url: 'https://$bucketHost$url', savedDir: saveDir, fileName: fileName, showNotification: false, // show download progress in status bar (for Android) openFileFromNotification: false, // click on notification to open downloaded file (for Android) ); return taskId; }Map<String, String> buildHeaders(String url) { Map<String, String> headers = Map(); headers['HOST'] = bucketHost; headers['Authorization'] = _auth('get', url, headers: headers); if(debug) { print(headers); } return headers; }String _auth(String httpMethod, String httpUrl, {Map<String, String> headers, Map<String, String> params}) { headers = headers ?? Map(); params = params ?? Map();int currentTimestamp = DateTime.now().millisecondsSinceEpoch ~/ 1000; var keyTime = '$currentTimestamp;${currentTimestamp + signExpireTimeInSeconds}'; headers = headers.map((key, value) => MapEntry(key.toLowerCase(), value)); params = params.map((key, value) => MapEntry(key.toLowerCase(), value)); List<String> headerKeys = headers.keys.toList(); headerKeys.sort(); var headerList = headerKeys.join(';'); var httpHeaders = headerKeys .map((item) => '$item=${Uri.encodeFull(headers[item])}') .join('&');List<String> paramKeys = params.keys.toList(); paramKeys.sort(); var urlParamList = paramKeys.join(';'); var httpParameters = paramKeys .map((item) => '$item=${Uri.encodeFull(params[item])}') .join('&');var signKey = new Hmac(sha1, utf8.encode(secretKey)) .convert(utf8.encode(keyTime)); String httpString = '${httpMethod.toLowerCase()}\n$httpUrl\n$httpParameters\n$httpHeaders\n'; var httpStringData = sha1.convert(utf8.encode(httpString)); String stringToSign = 'sha1\n$keyTime\n$httpStringData\n'; var signature = new Hmac(sha1, utf8.encode(signKey.toString())).convert(utf8.encode(stringToSign));String auth = 'q-sign-algorithm=sha1&q-ak=$secretId&q-sign-time=$keyTime&q-key-time=$keyTime&q-header-list=$headerList&q-url-param-list=$urlParamList&q-signature=$signature'; return auth; } } 

使用示例

static final TencentCos _cos = TencentCos.get(); Future<String> _findLocalPath() async { final directory = widget.platform == TargetPlatform.android ? await getExternalStorageDirectory() : await getApplicationDocumentsDirectory(); return directory.path; }Future _requestDownload() async { ///$yourLocalSaveDir is relative pathvar _localPath = (await _findLocalPath()) + '$yourLocalSaveDir';var taskId = await _cos.downloadFile('$yourFileNameInCos', _localPath); } 

更多

可根据自己需求,使用buildHeaders生成带签名的请求头,进行https请求。


本页内容由塔灯网络科技有限公司通过网络收集编辑所得,所有资料仅供用户学习参考,本站不拥有所有权,如您认为本网页中由涉嫌抄袭的内容,请及时与我们联系,并提供相关证据,工作人员会在5工作日内联系您,一经查实,本站立刻删除侵权内容。本文链接:http://dengtar.com/17553.html
相关APP开发
 八年  行业经验

多一份参考,总有益处

联系深圳网站公司塔灯网络,免费获得网站建设方案及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:余经理:13699882642

Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.