腾讯云cos对象存储使用

时间:2020-05-18
本文章向大家介绍腾讯云cos对象存储使用,主要包括腾讯云cos对象存储使用使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

COS

对象存储(Cloud Object Storage,COS)是腾讯云提供的一种存储海量文件的分布式存储服务,

用户可通过网络随时存储和查看数据。腾讯云 COS 使所有用户都能使用具备高扩展性、低成本、可靠和安全的数据存储服务。

创建存储桶

存储桶(Bucket)是对象的载体,可理解为存放对象的“容器”。用户可以通过腾讯云控制台、API、SDK 等多种方式管理存储桶以及配置属性。

查看SDK文档

小程序sdk:

使用示例

小程序端

 // 去某个地方获取一个临时密钥
    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,
            });
          }
        });
      }
    });
  服务端示例: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)

文件上传成功

原文地址:https://www.cnblogs.com/xiao-apple36/p/12912595.html