• 周六. 8月 20th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

腾讯云存储

admin

11月 28, 2021

安装 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")

发表回复

您的电子邮箱地址不会被公开。