国产亚洲欧美人成在线,免费视频爱爱太爽了无码,日本免费一区二区三区高清视频 ,国产真实伦对白精彩视频

歡迎您光臨深圳塔燈網(wǎng)絡(luò)科技有限公司!
電話圖標(biāo) 余先生:13699882642

網(wǎng)站百科

為您解碼網(wǎng)站建設(shè)的點點滴滴

flutter使用騰訊云COS對象服務(wù)

發(fā)表日期:2019-01 文章編輯:小燈 瀏覽次數(shù):3593

官方文檔:請求簽名, Get Object

轉(zhuǎn)載請注明出處: https://www.jianshu.com/p/49fe86909d18

Overview

本文使用dart按照騰訊COS文檔生成簽名,并使用簽名下載COS文件對象到本地(使用flutter_download包)

載入crypto以及flutter_downloader包

pubspec.yaml文件中加入依賴(flutter_downloader文檔,crypto文檔)

Android: flutter_downloader 需要配置權(quán)限,請參考其文檔。

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

實現(xiàn)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); } 

更多

可根據(jù)自己需求,使用buildHeaders生成帶簽名的請求頭,進(jìn)行https請求。


本頁內(nèi)容由塔燈網(wǎng)絡(luò)科技有限公司通過網(wǎng)絡(luò)收集編輯所得,所有資料僅供用戶學(xué)習(xí)參考,本站不擁有所有權(quán),如您認(rèn)為本網(wǎng)頁中由涉嫌抄襲的內(nèi)容,請及時與我們聯(lián)系,并提供相關(guān)證據(jù),工作人員會在5工作日內(nèi)聯(lián)系您,一經(jīng)查實,本站立刻刪除侵權(quán)內(nèi)容。本文鏈接:http://jstctz.cn/17553.html
相關(guān)APP開發(fā)