功能
- 从腾讯云存储上获取下载链接以及文件大小
- 将链接及大小更新到csv文件中
# -*- coding=utf-8
from concurrent.futures import ThreadPoolExecutor
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
from qcloud_cos.cos_exception import CosClientError, CosServiceError
import logging
import csv
# 找it要腾讯云的账号下的密钥
secret_id = ''
secret_key = ''
bucket = 'mech-soft-1316409819'
# 全球加速域名访问
region = None
token = None
domain = 'mech-soft-1316409819.cos.accelerate.myqcloud.com'
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Domain=domain)
client = CosS3Client(config)
def get_cos_file_download_url(file_key,original_file_name):
url = client.get_presigned_download_url(
Bucket=bucket,
Key=file_key,
Expired=864000000
)
logging.info("Get cos file download url: key:{0}, original file name:{1}, url:{2}.".format(file_key,original_file_name,url))
return url
def file_size(file_key):
try:
response = client.head_object(Bucket=bucket,Key=file_key)
return int(response["Content-Length"])
except Exception as e:
return 0
def format_file_size(size):
if size < 1024*1024:
return "{}KB".format(str(format(size/1024, '.2f')))
elif size < 1024*1024*1024:
return "{}MB".format(str(format(size/1024/1024, '.2f')))
return "{}GB".format(str(format(size/1024/1024/1024, '.2f')))
if __name__ == '__main__':
links = []
file_path = 'C:/Users/mech-mind-339/Desktop/robots.csv'
with open(file_path) as f:
reader = csv.reader(f)
header_row = next(reader)
for row in reader:
cos_file = "community_downloads/robot_models/{0}/{1}.mrob".format(row[0],row[2])
download_file_name = "{}.mrob".format(row[2])
size = format_file_size(file_size(cos_file))
link = get_cos_file_download_url(cos_file,download_file_name)
print(link)
links.append([link,size])
with open(file_path) as f:
data = [row for row in csv.DictReader(f)]
for i in range(len(links)):
data[i]['链接'] = links[i][0]
data[i]['文件大小'] = links[i][1]
csv_header = data[0].keys()
with open(file_path, 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=csv_header)
writer.writeheader()
writer.writerows(data)
文档更新时间: 2023-04-06 08:38 作者:admin