python 腾讯云cos对象存储上传脚本

葫芦的运维日志

下一篇 搜索 上一篇

浏览量 4705

2019/10/18 23:50


每日上传腾讯云cos本地数据日志:

#!/usr/bin/env python
# coding=utf-8
# author: wz
# mail: 277215243@qq.com
# datetime:2019/10/18 1:24 PM
# web: https://www.bthlt.com
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
from qcloud_cos import CosServiceError
from qcloud_cos import CosClientError
import socket
import sys
import os
import logging
import datetime

# 腾讯云COSV5Python SDK, 目前可以支持Python2.6与Python2.7以及Python3.x

# pip安装指南:pip install -U cos-python-sdk-v5

# cos最新可用地域,参照https://www.qcloud.com/document/product/436/6224

logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 设置用户属性, 包括secret_id, secret_key, region
# appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成
secret_id = 'id**********'  # 替换为用户的secret_id
secret_key = 'key*********'  # 替换为用户的secret_key
region = 'ap-shanghai'  # 替换为用户的region
token = None  # 使用临时密钥需要传入Token,默认为空,可不填
scheme = 'http'
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)  # 获取配置对象
client = CosS3Client(config)
host_name = socket.gethostname()

today = datetime.date.today()
today_str = today.strftime('%Y-%m-%d')

files=os.listdir('/data/task')
for file in files:
    if today_str in file and 'txt' in file:
         
        response = client.upload_file(
            Bucket='nginx-****',
            LocalFilePath='/data/task/{0}'.format(file),
            Key=file,         
            PartSize=10,
            MAXThread=10
        )
        #Key 文件名
        print response

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2021 王梓打赏联系方式 陕ICP备17005322号-1