python3实现gitlab备份文件上传腾讯云COS

时间:2024-02-16 15:45:01
# -*- coding: utf-8 -*- # /usr/bin/env python3 # file_name : upload.py # 依赖安装:pip3 install -U cos-python-sdk-v5 import os import sys import time import datetime import logging from qcloud_cos import CosConfig from qcloud_cos import CosS3Client from qcloud_cos.cos_exception import CosClientError, CosServiceError logging.basicConfig(level=logging.INFO, stream=sys.stdout) secret_id = 'SecretId' secret_key = 'SecretKey' region = 'ap-guangzhou' token = None config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) client = CosS3Client(config) def upload_cos(file): current = os.getcwd() # 线程上传 for i in range(0, 10): try: client.upload_file( Bucket='backup-1145114', Key=f'gitlab/{file}', LocalFilePath=current + '/' + file, ) break except CosClientError or CosServiceError as e: print(e) def get_files(): # 切换到gitlab备份目录 os.chdir('/opt/gitlab/backups') current_dir = os.getcwd() file_list = os.listdir(current_dir) # 脚本文件排除上传 if 'upload.py' in file_list: file_list.remove('upload.py') # print(file_list) current_day = datetime.datetime.now().strftime("%Y_%m_%d") for file in file_list: if current_day in file: print('file exist ===>', file) # window下用 \\ ,linux下用 / print(current_dir + "/" + file) upload_cos(file) if __name__ == '__main__': start_time = int(time.time() * 1000) get_files() end_Time = int(time.time() * 1000) allCostTime = end_Time - start_time print(f"上传耗时:{allCostTime}ms")