CDB:CDB有API可是CDB的查詢備份下載沒有對應的SDK,此時只能經過API來進行獲取,騰訊雲API的簽名很複雜,要進行:構造參數字典->對dict排序->拼接sign->對sign編碼->拼接完成最終url->完成調用,簽名方法,查詢備份APIpython
cos-python-sdk-v5==1.5.2 requests==2.19.1 tencentcloud-sdk-python==3.0.15 urllib3==1.23
github地址git
# auth:kaliarch # func:將騰訊雲cdb備份文件上傳至cos制定的bucket內 # python version:python3+ # cos version:v5 # https://console.cloud.tencent.com/cos5/bucket # 騰訊雲公共信息配置段 [common] # 騰訊雲 secretid secret_id = AKIDMdjegcmoGxxxxxxxxxxxxxxxxxxxx # 騰訊雲 secretkey secret_key = d5MRL4VoxyvlQvxxxxxxxxxxxxxx # 騰訊雲cos信息配置段 [cosinfo] # cos所在地域 cos_region = ap-chengdu # 騰訊雲bucket名字(cos v5 bucket名稱組成:bucket+appid) bucket_name = xuel-test-bucket-125396xxxx # 騰訊雲cdb信息配置段 [cdbinfo] # cdb實例id cdb_instanceid = cdb-rqaxxxxx # cdb所在地域 cdb_region = ap-shanghai # cdb 日誌備份類型,coldbackup(冷備),binlog(二進制日誌)和slowlog_day(慢查詢日誌) cdb_bak_type = coldbackup # 日誌文件信息配置段 [loginfo] #日誌文件目錄名稱 logdir_name = rds_to_cos #日誌文件名稱 logfile_name = rdsbak_to_cos.log
#構建字典 keydict = { 'Action': self.cdb_action, 'Timestamp': str(int(time.time())), 'Nonce': str(int(random.random() * 1000)), 'Region': self.cdb_region, 'SecretId': self.secret_id, # 'SignatureMethod': SignatureMethod, 'cdbInstanceId': self.cdb_instanceid, 'type': self.cdb_bak_type } #字典排序 sorted(zip(keydict.keys(), keydict.values())) #字符串拼接 sign_str_init = '' for value in sortlist: sign_str_init += value[0] + '=' + value[1] + '&' sign_str = 'GET' + self.cdb_api_url + sign_str_init[:-1] return sign_str, sign_str_init #獲取簽名串並編碼 secretkey = self.secret_key signature = bytes(sign_str, encoding='utf-8') secretkey = bytes(secretkey, encoding='utf-8') my_sign = hmac.new(secretkey, signature, hashlib.sha1).digest() my_sign = base64.b64encode(my_sign) parse.quote(my_sign) #獲取最終url result_url = 'https://' + self.cdb_api_url + sign_str + '&Signature=' + result_sign
單獨運行此模塊能夠獲得如下信息:
github
#根據文件大小自動選擇簡單上傳或分塊上傳,分塊上傳具有斷點續傳功能 with open(filename, 'wb') as localfile: localfile.write(requests.request('get', url).content) # 進行上傳 response = cos_client.upload_file( Bucket=self.bucket_name, LocalFilePath=filename, Key=filename, PartSize=partsize, MAXThread=maxthread ) # 刪除本地文件 if os.path.exists(filename): os.remove(filename)
#建立目錄 def create_dir(self): _LOGDIR = os.path.join(os.path.dirname(__file__), self.logdir_name) _TIME = time.strftime('%Y-%m-%d', time.gmtime()) + '-' _LOGNAME = _TIME + self.logfile_name LOGFILENAME = os.path.join(_LOGDIR, _LOGNAME) if not os.path.exists(_LOGDIR): os.mkdir(_LOGDIR) return LOGFILENAME #定義日誌文件 def create_logger(self, logfilename): logger = logging.getLogger() logger.setLevel(logging.INFO) handler = logging.FileHandler(logfilename) handler.setLevel(logging.INFO) formater = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formater) logger.addHandler(handler) return logger
獲取CDB下載連接
完成上傳查看COS文件
api