• 腾讯云cos对象存储使用


    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)

    文件上传成功

  • 相关阅读:
    POJ 3080 Blue Jeans
    POJ 1961 Period
    POJ 2185 Milking Grid
    POJ 2752 Seek the Name, Seek the Fame
    斗地主算法的设计与实现--项目介绍&如何定义和构造一张牌
    MyEclipse 免安装版制作
    网络子系统48_ip协议数据帧的发送
    Oracle sql语句执行顺序
    当OOP语言RAII特性发展到functional形式的极致
    FusionCharts属性大全
  • 原文地址:https://www.cnblogs.com/xiao-apple36/p/12912595.html
Copyright © 2020-2023  润新知