4.7. Swift 获取临时 URL 对象
临时 URL 使用加密 HMAC-SHA1 签名,其中包含以下元素:
- Request 方法的值,实例"GET"
- 自 epoch (即 Unix 时间)以来的过期时间(以秒为单位)
- 从 "v1" 开始的请求路径
以上项目在它们之间附加了换行符,并使用 SHA-1 哈希算法(之前发布的 Temp URL 键)生成 HMAC。
要演示上述的 python 脚本的示例如下:
示例
import hmac
from hashlib import sha1
from time import time
method = 'GET'
host = 'https://objectstore.example.com'
duration_in_seconds = 300 # Duration for which the url is valid
expires = int(time() + duration_in_seconds)
path = '/v1/your-bucket/your-object'
key = 'secret'
hmac_body = '%s\n%s\n%s' % (method, expires, path)
hmac_body = hmac.new(key, hmac_body, sha1).hexdigest()
sig = hmac.new(key, hmac_body, sha1).hexdigest()
rest_uri = "{host}{path}?temp_url_sig={sig}&temp_url_expires={expires}".format(
host=host, path=path, sig=sig, expires=expires)
print rest_uri
输出示例
https://objectstore.example.com/v1/your-bucket/your-object?temp_url_sig=ff4657876227fc6025f04fcf1e82818266d022c6&temp_url_expires=1423200992