python实现upyun存储解压zip文件

3/24/2023 pythonupyun

# 使用python发送又拍云云存储的解压

upyun内介绍的有点抽象,可能会看不懂啥的,这里我们用它的demo示例代码来对zip文件进行解压。

# 条件

# 注意

upyun目前仅支持解压zip文件,因此云存储内的指定文件必须是zip格式
upyun原文档 (opens new window)

# 代码

import base64,hashlib,hmac
import datetime,requests,json

bucket = ''         # 存储服务名称
operator = ''       # 操作员
password = hashlib.md5('PASSWORD'.encode()).hexdigest() # PASSWORD是操作员密码
date = datetime.datetime.utcnow().strftime("%a,%d %b %Y %H:%M:%S GMT")
uri = '/pretreatment'   # 这里的uri不用换
method = 'POST'
strings = method + '&' + uri + '&' + date

def make_tasks():
    p = json.dumps([{"sources":"path/to/file.zip", "save_as":"/depress/path"}]) # path/to/file.zip是存储库内的zip文件 /depress/path是解压后保存的路径
    return base64.b64encode(p.encode()).decode()
tasks = make_tasks()

def hmac_sha1(key, msg):
    h = hmac.new(key.encode(), msg.encode(), digestmod = hashlib.sha1).digest()
    return "UPYUN " + operator + ':' + base64.b64encode(h).decode()

auth = hmac_sha1(password, strings)
headers = {"authorization":auth, "date":date}
data = {"service":bucket, "notify_url":"NOTIFY_URL", "tasks":tasks, "app_name":"depress"}   # 又拍云的解压任务通过POST方法回调给notify_url
url = 'http://p0.api.upyun.com' + uri 
r = requests.post(url,headers=headers, data=data)
print(r.text)
print(r.headers)
print(r.status_code)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

notify_url 可以再参考upyun的原文档 (opens new window)

# last

该文章写于2023年3月24日,仅供参考.

最近更新: 11/24/2024, 10:03:25 AM