python 读取最新阿里云RDS数据库备份并下载

首先需要安装阿里云的SDK库

阿里云SDK核心库PIP安装命令pip install aliyun-python-sdk-core

阿里云关系型数据库  PIP安装命令:pip install aliyun-python-sdk-rds

官方api参考OpenAPI Explorer

#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkrds.request.v20140815.DescribeBackupsRequest import DescribeBackupsRequest
import json
import re
import requests

#RDS用户ACESS KEY

IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>")

request = DescribeBackupsRequest()
request.set_accept_format('json')
request.set_DBInstanceId("DBInstanceId") #RDS数据库实例名
response = client.do_action_with_exception(request)
response=str(response, encoding='utf-8')
response = json.loads(response)
url =response["Items"]["Backup"][0]["BackupDownloadURL"]#获取URL链接
print (url)
filename = (url[url.rindex('/')+1:url.rindex('?')])
print ("downloading with urllib")
#f=requests.get(url)

path = "E:\\filepath\"
print (path)
#f=requests.get(url)
#下载文件
#with open(filename,"wb") as code:
#    code.write(f.content)
urllib.request.urlretrieve(url,path+filename )

如果出现类似报错

 则导入ssl模块,然后在urllib.request.urlretrieve前使用ssl._create_unverified_context关闭认证

import ssl
ssl._create_default_https_context = ssl._create_unverified_context
 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值