这里写自定义目录标题
import os
import tempfile
import sys
from qcloud_cos import CosConfig, CosS3Client
from qcloud_cos.cos_threadpool import SimpleThreadPool
import psutil
import time
local_folder_path = r'E:\work\output'
cos_folder_path = 'game/version/v100000001/'
total_files = 0
uploaded_byte = 0
uploaded_sum = 0
upload_success = []
def upload_percentage(consumed_bytes, total_bytes):
global uploaded_byte
global uploaded_sum
uploaded_byte += consumed_bytes
rate = int(100 * (float(uploaded_byte) / float(total_files)))
if(rate >= 100):
rate = 99
if(uploaded_sum != rate):
print('\r{0}%;'.format(rate))
uploaded_sum = rate
printUploadFile(rate)
get_network_speed()
sys.stdout.flush()
def printUploadFile(num):
global upload_success
l = len(upload_success)
c = int(l * num / 100)
for i in range(c):
if not upload_success[i]['log']:
print(upload_success[i]['url'])
upload_success[i]['log'] = True
def count_files_recursively(folder_path):
file_count = 0
try:
for root, dirs, files in os.walk(folder_path):
file_count += len(files)
return file_count
except Exception as e:
print("Error:", e)
return None
def upload_directory(local_folder_path, cos_folder_path = ''):
global total_files
global upload_success
secret_id = '**********************'
secret_key = '**********************'
region = 'region'
bucket_name = 'bucket_name'
token = None
domain = None
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Domain=domain)
client = CosS3Client(config)
pool = SimpleThreadPool()
total_files = sum(os.path.getsize(os.path.join(root, file)) for root, dirs, files in os.walk(local_folder_path) for file in files if file != 'desktop.ini')
for root, dirs, files in os.walk(local_folder_path):
for file in files:
if file != 'desktop.ini':
local_file_path = os.path.join(root, file)
remote_file_path = os.path.join(cos_folder_path, os.path.relpath(local_file_path, local_folder_path))
remote_file_path = remote_file_path.replace("\\", "/")
pool.add_task(client.upload_file,Bucket = bucket_name,Key = remote_file_path,LocalFilePath = local_file_path,progress_callback=upload_percentage)
upload_success.append({'url':local_file_path,'log':False})
for dir in dirs:
remote_dir_path = os.path.join(cos_folder_path, os.path.relpath(os.path.join(root, dir), local_folder_path))
empty_file_path = os.path.join(tempfile.gettempdir(), 'empty_file')
empty_file_path = empty_file_path.replace("\\", "/")
with open(empty_file_path, 'wb'):
pass
pool.add_task(client.upload_file,Bucket = bucket_name,Key = remote_dir_path + '/',local_file_path = empty_file_path)
os.remove(empty_file_path)
pool.wait_completion()
result = pool.get_result()
if not result['success_all']:
print("Not all files upload successed. you should retry")
else:
print("100%")
printUploadFile(100)
print('All uploaded')
def get_network_speed(interval=1):
net_io = psutil.net_io_counters()
bytes_sent = net_io.bytes_sent
time.sleep(interval)
new_net_io = psutil.net_io_counters()
new_bytes_sent = new_net_io.bytes_sent
sent_speed = (new_bytes_sent - bytes_sent) / interval
mbps = (sent_speed * 8) / 1000000
mbps = "{:.2f}".format(mbps)
if __name__ == "__main__":
upload_directory(local_folder_path,cos_folder_path)