You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.5 KiB

1 year ago
import time
import oss2
# pip install oss2
import hashlib
import requests
from Util import PgUtil
# 填写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 download_image(image_url, file_path):
while True:
try:
# 发送GET请求
response = requests.get(image_url)
# 检查请求是否成功
if response.status_code == 200:
# 打开一个文件用于写入
with open(file_path, 'wb') as f:
# 将响应的内容写入文件中
f.write(response.content)
break
else:
pass
except Exception as err:
time.sleep(3)
q = []
# 使用ObjectIterator查找具有指定前缀的对象
for obj in oss2.ObjectIterator(bucket, prefix=prefix):
oss_file_path = obj.key
if '.' not in oss_file_path:
continue
local_file_path = 'D:/Temp/' + oss_file_path.replace(prefix, '')
img_url = "http://hzkc.oss-cn-beijing.aliyuncs.com/" + obj.key
download_image(img_url, local_file_path)
with open(local_file_path, 'rb') as fp:
modelData = fp.read()
file_md5 = hashlib.md5(modelData).hexdigest()
# print(file_md5)
sql = "select image_md5,image_url as c from t_hy_image_md5 where image_md5='%s'" % file_md5
list = PgUtil.execute_query(sql)
count = len(list)
if count == 0:
sql = "insert into t_hy_image_md5(image_md5,image_url) values('%s','%s')" % (file_md5, img_url)
PgUtil.execute_modify(sql)
else:
print("img_url=" + img_url + "===>" + list[0][1])
# 1、删除远程文件
if img_url != list[0][1]:
bucket.delete_object(obj.key)
# 2、更改t_hy_task source_img_url
sql = "update t_hy_task set source_img_url='%s' where source_img_url='%s'" % (list[0][1], img_url)
PgUtil.execute_modify(sql)