zoukankan      html  css  js  c++  java
  • 腾讯云存储

    安装 SDK

    安装 SDK 有三种安装方式:pip 安装、手动安装和离线安装。

    python

    pip install -U cos-python-sdk-v5

    对象存储手册

    https://cloud.tencent.com/document/product/436/35151#.E6.89.B9.E9.87.8F.E4.B8.8A.E4.BC.A0.EF.BC.88.E6.9C.AC.E5.9C.B0.E6.96.87.E4.BB.B6.E5.A4.B9.E4.B8.8A.E4.BC.A0.EF.BC.89

    该示例展示通过组合 SDK 的基本接口,完成批量上传本地文件夹到 COS。

    # -*- coding=utf-8
    from qcloud_cos import CosConfig
    from qcloud_cos import CosS3Client
    from qcloud_cos import CosServiceError
    from qcloud_cos import CosClientError
    from qcloud_cos.cos_threadpool import SimpleThreadPool
    import sys
    import os
    import logging
    # 设置用户属性, 包括secret_id, secret_key, region
    # APPID 已在配置中移除,请在参数 Bucket 中带上 APPID。Bucket 由 BucketName-APPID 组成
    secret_id = ''     # 替换为用户的 secret_id
    secret_key = ''     # 替换为用户的 secret_key
    region = 'ap-guangzhou'    # 替换为用户的 region
    token = None               # 使用临时密钥需要传入 Token,默认为空,可不填
    config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token)  # 获取配置对象
    client = CosS3Client(config)
    uploadDir = '/root/logs'
    bucket = 'examplebucket-125000000'
    g = os.walk(uploadDir)
    # 创建上传的线程池
    pool = SimpleThreadPool()
    for path, dir_list, file_list in g:
       for file_name in file_list:
           srcKey = os.path.join(path, file_name)
           cosObjectKey = srcKey.strip('/')
           # 判断COS上文件是否存在
           exists = False
           try:
               response = client.head_object(Bucket=bucket, Key=cosObjectKey)
               exists = True
           except CosServiceError as e:
               if e.get_status_code() == 404:
                   exists = False
               else:
                   print("Error happened, reupload it.")
           if not exists:
               print("File %s not exists in cos, upload it", srcKey)
               pool.add_task(client.upload_file, bucket, cosObjectKey, srcKey)
    
    pool.wait_completion()
    result = pool.get_result()
    if not result['success_all']:
       print("Not all files upload sucessed. you should retry")

  • 相关阅读:
    fedora 27
    Python3 字符串操作
    python3 使用matplotlib画图问题
    Python3 移动文件——合集
    MySQL开放外部链接
    python之csv操作问题
    Question
    17-12-21
    python之List排序
    ubuntu版的sublime-text3输入中文问题
  • 原文地址:https://www.cnblogs.com/fw-qql/p/15109611.html
Copyright © 2011-2022 走看看