COS
對象存儲(Cloud Object Storage,COS)是騰訊雲提供的一種存儲海量文件的分布式存儲服務,
用戶可通過網絡隨時存儲和查看數據。騰訊雲 COS 使所有用戶都能使用具備高擴展性、低成本、可靠和安全的數據存儲服務。
創建存儲桶
存儲桶(Bucket)是對象的載體,可理解為存放對象的“容器”。用戶可以通過騰訊雲控制台、API、SDK 等多種方式管理存儲桶以及配置屬性。
查看SDK文檔
小程序sdk:
使用示例
小程序端
uploadFile:function(){ var onlineImageList = []; var that = this; // 去某個地方獲取一個臨時密鑰 var cos = new COS({ getAuthorization: function (options, callback) { // 服務端 JS 和 PHP 示例:https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/ // 服務端其他語言參考 COS STS SDK :https://github.com/tencentyun/qcloud-cos-sts-sdk // STS 詳細文檔指引看:https://cloud.tencent.com/document/product/436/14048 wx.request({ url: 'http://127.0.0.1:8000/api/credential/', data: { // 可從 options 取需要的參數 }, success: function (result) { var data = result.data; var credentials = data.credentials; callback({ TmpSecretId: credentials.tmpSecretId, TmpSecretKey: credentials.tmpSecretKey, XCosSecurityToken: credentials.sessionToken, ExpiredTime: data.expiredTime, }); } }); } }); for(var index in this.data.imageList){ var filePath = this.data.imageList[index]; cos.postObject({ Bucket: 'xiaopu-1259051832', Region: 'ap-beijing', Key: index + "uuu.png", FilePath: filePath, onProgress: function (info) { console.log('進度條', JSON.stringify(info)); } }, function (err, data) { console.log(data.Location) onlineImageList.push(data.Location); }); } },
服務端示例:https://github.com/tencentyun/qcloud-cos-sts-sdk/edit/master
如python端


https://github.com/tencentyun/qcloud-cos-sts-sdk/tree/master/python
python端使用
class CredentialView(APIView): def get(self,request,*args,**kwargs): from sts.sts import Sts from django.conf import settings config = { # 臨時密鑰有效時長,單位是秒 'duration_seconds': 1800, # 固定密鑰 id 'secret_id': os.environ.get("secretId"), # 固定密鑰 key 'secret_key': os.environ.get("secretKey"), # 設置網絡代理 # 'proxy': { # 'http': 'xx', # 'https': 'xx' # }, # 換成你的 bucket 'bucket': 'xiaopu-1259051832', # 換成 bucket 所在地區 'region': 'ap-beijing', # 這里改成允許的路徑前綴,可以根據自己網站的用戶登錄態判斷允許上傳的具體路徑 # 例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全風險, 請謹慎評估使用) 'allow_prefix': '*', # 密鑰的權限列表。簡單上傳和分片需要以下的權限,其他權限列表請看 https://cloud.tencent.com/document/product/436/31923 'allow_actions': [ # 簡單上傳 "name/cos:PutObject", # 表單上傳、小程序上傳 "name/cos:PostObject", ], } sts = Sts(config) response = sts.get_credential() return Response(response)
文件上傳成功