ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

腾讯云存储

2021-08-06 17:35:37  阅读:308  来源: 互联网

标签:存储 exists E4 cos qcloud secret 腾讯 import


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

 

标签:存储,exists,E4,cos,qcloud,secret,腾讯,import
来源: https://www.cnblogs.com/fw-qql/p/15109611.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有