zoukankan      html  css  js  c++  java
  • 腾讯oss对象存储服务

    from qcloud_cos import CosConfig
    from qcloud_cos import CosS3Client
    import requests
    
    
    class TencentOSS:
        def __init__(self):
            self.temp_url = 'https://lqn-1259364486.cos.ap-beijing.myqcloud.com/'
            self.init_client()
    
        def init_client(self):
            secret_id = '*******'  # 替换为用户的 secretId
            secret_key = '*********'  # 替换为用户的 secretKey
            region = '*****'  # 对象存储的位置信息
            endpoint = ''  # 自由域名修改 如果需要修改自由域名 参数指定 Endpoint=endpoint
            config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key)
            # 2. 获取客户端对象
            self.client = CosS3Client(config)
    
        def check_file(self, oss_file_path):
            """校验单个文件名称是否已经存在桶中"""
            response = self.client.list_objects(
                Bucket='*****',
                Prefix=oss_file_path
            )
            content = response.get('Contents')
            if content:
                return True
            else:
                return False
    
        def upload_file_locality(self, file_pth, file_name):
            """
            本地文件上传
            file_pth: 本地文件的路径
            file_name: 文件的名称
            """
            oss_file_name = f'images/{file_name}'
            result = self.check_file(oss_file_name)
            if result:
                print('文件已经存在桶中')
            else:
                with open(file_pth, 'rb') as fp:
                     self.client.put_object(
                        Bucket='********',  # oss 桶的名称
                        Body=fp,
                        Key=oss_file_name,  # 此处是上传到oss 的路径,上传到这个桶的images 文件夹下
                        StorageClass='image/jpeg',  # 上传数据类行
                        EnableMD5=False  # 是否使用md5加密
                    )
    
                file_url = self.temp_url+oss_file_name
                return file_url
    
        def upload_file_url(self, file_url, file_name):
            """
            url 链接上传
            file_url : 文件的url地址
            file_name : 文件的名称
            """
            oss_file_name = f'images/{file_name}'
            result = self.check_file(oss_file_name)
            if result:
                print('文件已经存在桶中')
            else:
                file_resp = requests.get(file_url)
                self.client.put_object(
                    Bucket='l********',  # oss 桶的名称
                    Body=file_resp.content,
                    Key=oss_file_name,  # 此处是上传到oss 的路径,上传到这个桶的images 文件夹下
                    StorageClass='image/jpeg',  # 上传数据类行
                    EnableMD5=False  # 是否使用md5加密
                )
                file_url = self.temp_url + oss_file_name  # 可以访问的url
                return file_url
    
        def upload_big_file(self, file_path, file_name):
            """大文件上传-----比如视频啥的"""
            oss_file_name = f'images/{file_name}'
            result = self.check_file(oss_file_name)
            if result:
                print('文件已经存在桶中')
            else:
                self.client.upload_file(
                    Bucket='********',
                    LocalFilePath=file_path,
                    Key=oss_file_name,
                    PartSize=1,
                    MAXThread=10,
                    EnableMD5=False
                )
                file_url = self.temp_url + oss_file_name  # 可以访问的url
                return file_url
    
    
    if __name__ == '__main__':
        file_path = '1.jpg'  # 本地文件路径
        file_name = '3.jpg'  # 文件名称
        file_url = 'https://lqn-1259364486.cos.ap-beijing.myqcloud.com/images/1.jpg'  # 文件的链接地址
        TencentOSS().upload_big_file(file_path, file_name)

    腾讯oss 对象服务,用于存储大量的数据库不好存储数据,比如图片,视频之类。

    数据访问地址可以使用自有域名,或者有对象服务提供域名

  • 相关阅读:
    证券市场主体
    证券投资基金
    1.监控系统的重要性
    1.五种世界顶级思维-20190303
    【四校联考】【比赛题解】FJ NOIP 四校联考 2017 Round 7
    【学长出题】【比赛题解】17-09-29
    【codeforces】【比赛题解】#854 CF Round #433 (Div.2)
    【codeforces】【比赛题解】#851 CF Round #432 (Div.2)
    【算法学习】三分法
    【codeforces】【比赛题解】#849 CF Round #431 (Div.2)
  • 原文地址:https://www.cnblogs.com/lqn404/p/13840370.html
Copyright © 2011-2022 走看看