|
|
import os
|
|
|
import oss2
|
|
|
import requests
|
|
|
|
|
|
# 填写RAM用户的访问密钥(AccessKey ID和AccessKey Secret)。
|
|
|
accessKeyId = 'LTAI5tE4tgpGcKWhbZg6C4bh'
|
|
|
accessKeySecret = 'oizcTOZ8izbGUouboC00RcmGE8vBQ1'
|
|
|
|
|
|
# endpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
|
|
|
endpoint = 'http://oss-cn-beijing.aliyuncs.com'
|
|
|
# 填写Bucket名称。
|
|
|
bucketName = 'hzkc'
|
|
|
|
|
|
# 使用代码嵌入的RAM用户的访问密钥配置访问凭证。
|
|
|
auth = oss2.Auth(accessKeyId, accessKeySecret)
|
|
|
bucket = oss2.Bucket(auth, endpoint, bucketName)
|
|
|
|
|
|
# 遍历 Images/Upload前缀下所有文件
|
|
|
# 指定前缀
|
|
|
prefix = 'Images/Upload/'
|
|
|
|
|
|
|
|
|
# 获取图片宽高
|
|
|
def down_small_img(v_url, directory):
|
|
|
v_url = v_url.replace("https://hzkc.oss-cn-beijing.aliyuncs.com", 'http://image.hzkjai.com')
|
|
|
t_url = v_url + '?x-oss-process=image/info'
|
|
|
# 发送GET请求
|
|
|
response = requests.get(t_url)
|
|
|
# 解析JSON数据
|
|
|
data = response.json()
|
|
|
width = data['ImageWidth']['value']
|
|
|
height = data['ImageHeight']['value']
|
|
|
action = 'w'
|
|
|
if width < height:
|
|
|
action = 'h'
|
|
|
if int(width) > 1024 or int(height) > 1024:
|
|
|
t_url = v_url + '?x-oss-process=image/resize,' + action + '_1024,limit_0'
|
|
|
else:
|
|
|
t_url = v_url
|
|
|
|
|
|
# 从URL中提取文件名
|
|
|
filename = v_url.split('/')[-1]
|
|
|
# windows版本的wget
|
|
|
os.system('wget ' + t_url + ' -O ' + directory + '/' + filename)
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
Directory = r'D:\backup\Temp'
|
|
|
# 使用ObjectIterator查找具有指定前缀的对象
|
|
|
for obj in oss2.ObjectIterator(bucket, prefix=prefix):
|
|
|
oss_file_path = obj.key
|
|
|
if '.' not in oss_file_path:
|
|
|
continue
|
|
|
img_url = "http://hzkc.oss-cn-beijing.aliyuncs.com/" + obj.key
|
|
|
down_small_img(img_url, Directory)
|