POST 文件到 AWS Mediastore Python 3 没有 SDK,没有 CLI
POST file to AWS Mediastore with Python 3 without SDK, without CLI
我想 POST 使用 Python 和 Signature v4 将一个 mp4 文件上传到 AWS MediaStore。我正在尝试使用 MediaStore 中的 PutObject
操作。
对于这项工作,我无法使用 SDK 或 CLI。
我可以在没有 SDK 或 CLI 的情况下使用 Python 向 MediaStore 发出 GET 请求,但是关于 POST 请求,我不明白我应该如何处理负载。我收到以下错误:
<InvalidSignatureException>
<Message>The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.</Message>
</InvalidSignatureException>
无法正常工作POST 代码
# NON WORKING CODE
import sys, os, base64, datetime, hashlib, hmac
import requests # pip install requests
method = 'PUT'
service = 'mediastore'
host = 'qwerty123.data.mediastore.ap-northeast-1.amazonaws.com'
region = 'ap-northeast-1'
endpoint = 'https://qwerty123.data.mediastore.ap-northeast-1.amazonaws.com'
request_parameters = 'Action=PutObject&Path=dummyfile.mp4&Version=2017-09-01'
cwd = os.getcwd()
def sign(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
def getSignatureKey(key, dateStamp, regionName, serviceName):
kDate = sign(('AWS4' + key).encode('utf-8'), dateStamp)
kRegion = sign(kDate, regionName)
kService = sign(kRegion, serviceName)
kSigning = sign(kService, 'aws4_request')
return kSigning
access_key = "AXXXXXXXXXXXA"
secret_key = "UXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXD"
if access_key is None or secret_key is None:
print('No access key is available.')
sys.exit()
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')
canonical_uri = '/'
canonical_querystring = request_parameters
payload_hash = hashlib.sha256(('').encode('utf-8')).hexdigest()
canonical_headers = 'content-type:video/mp4' + '\n' +'host:' + host + '\n' + 'x-amz-content-sha256:'+payload_hash + '\n' + 'x-amz-date:' + amzdate + '\n' + 'x-amz-storage-class:'+ "TEMPORAL" + '\n'
signed_headers = 'content-type;host;x-amz-content-sha256;x-amz-storage-class;x-amz-date'
canonical_request = method + '\n' + canonical_uri + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + '\n' + payload_hash
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/' + service + '/' + 'aws4_request'
string_to_sign = algorithm + '\n' + amzdate + '\n' + credential_scope + '\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
signing_key = getSignatureKey(secret_key, datestamp, region, service)
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature
headers = {'Content-Type':'video/mp4','x-amz-date':amzdate,'X-Amz-Content-Sha256':payload_hash, 'X-Amz-Storage-Class':'TEMPORAL', 'Authorization':authorization_header}
request_url = endpoint + '?' + canonical_querystring
print('\n * * * * * * * * * * * * BEGIN REQUEST * * * * * * * * * * * * ')
print('Request URL = ' + request_url)
files = {'file': open(cwd + "/" + "dummyfile.mp4", 'rb')}
r = requests.post(request_url, headers=headers, files=files)
print('\n* * * * * * * * * * * * * * RESPONSE * * * * * * * * * * * * *')
print('Response code: %d\n' % r.status_code)
print(r.text)
来自文档:
POST premium/canada/mlaw.avi
Host: aaabbbcccdddee.files.mediastore-us-west-2.com
x-amz-Date: 20170323T120000Z
Authorization: AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20141123/us-west-2/mediastore/aws4_request,SignedHeaders=host;x-amz-date;x-amz-mediastore-version,Signature=9257c16da6b25a715ce900a5b45b03da0447acf430195dcb540091b12966f2a2
Content-Length: 0
x-amz-mediastore-version: 2016-07-11
工作 GET 代码
# WORKING CODE
# NB Apparently the x-amz-content-sha256 header is not necessary
import sys, os, base64, datetime, hashlib, hmac
import requests # pip install requests
method = 'GET'
service = 'mediastore'
host = 'qwerty123.data.mediastore.ap-northeast-1.amazonaws.com'
region = 'ap-northeast-1'
endpoint = 'https://qwerty123.data.mediastore.ap-northeast-1.amazonaws.com'
request_parameters = 'Action=ListItems&Version=2017-09-01'
def sign(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
def getSignatureKey(key, dateStamp, regionName, serviceName):
kDate = sign(('AWS4' + key).encode('utf-8'), dateStamp)
kRegion = sign(kDate, regionName)
kService = sign(kRegion, serviceName)
kSigning = sign(kService, 'aws4_request')
return kSigning
access_key = "AXXXXXXXXXXXXXXA"
secret_key = "UXXXXXXXXXXXXXXXXXXXXXXXXXXXXD"
if access_key is None or secret_key is None:
print('No access key is available.')
sys.exit()
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')
canonical_uri = '/'
canonical_querystring = request_parameters
payload_hash = hashlib.sha256(('').encode('utf-8')).hexdigest()
canonical_headers = 'content-type:application/x-www-form-urlencoded; charset=utf-8' + '\n' +'host:' + host + '\n' + 'x-amz-date:' + amzdate + '\n' #+ 'x-amz-content-sha256:'+payload_hash + '\n'
signed_headers = 'content-type;host;x-amz-date' #x-amz-content-sha256;
canonical_request = method + '\n' + canonical_uri + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + '\n' + payload_hash
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/' + service + '/' + 'aws4_request'
string_to_sign = algorithm + '\n' + amzdate + '\n' + credential_scope + '\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
signing_key = getSignatureKey(secret_key, datestamp, region, service)
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature
headers = {'Content-Type':'application/x-www-form-urlencoded; charset=utf-8','x-amz-date':amzdate, 'Authorization':authorization_header} #'X-Amz-Content-Sha256':payload_hash,
request_url = endpoint + '?' + canonical_querystring
print('\n * * * * * * * * * * * * BEGIN REQUEST * * * * * * * * * * * * ')
print('Request URL = ' + request_url)
r = requests.get(request_url, headers=headers)
print('\n* * * * * * * * * * * * * * RESPONSE * * * * * * * * * * * * *')
print('Response code: %d\n' % r.status_code)
print(r.text)
非常感谢任何帮助。
编辑
我从 GET 请求中删除了 x-amz-content-sha256
header,请求仍然有效。
我尝试对 POST 请求做同样的事情,但没有成功。
您是否尝试将方法从 method = 'PUT'
更改为 method = 'POST'
?我认为这会对您有所帮助,因为文档中使用的方法是 POST
使用此代码进行 SHA 密钥签名:
def sign(key, msg):
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
def getSignatureKey(key, dateStamp, regionName, serviceName):
kDate = sign(("AWS4" + key).encode("utf-8"), dateStamp)
kRegion = sign(kDate, regionName)
kService = sign(kRegion, serviceName)
kSigning = sign(kService, "aws4_request")
return kSigning
来源AWS
常规上传请求:
资料来源:AWS-PDF
POST premium/canada/mlaw.avi
Host: aaabbbcccdddee.files.mediastore-us-west-2.com
x-amz-Date: 20170323T120000Z
Authorization: AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20141123/us-
west-2/mediastore/aws4_request,SignedHeaders=host;x-amz-date;x-amz-mediastore-version,Signature=9257c16da6b25a715ce900a5b45b03da0447acf430195dcb540091b12966f2a2
Content-Length: 0
x-amz-mediastore-version: 2016-07-11
我的脚本中有以下错误:
canonical_uri
必须设置为:/filename.mp4
以下 headers 是必需的:host,contentlength,x-amz-content-sha256,x-amz-date
payload_hash
必须设置为UNSIGNED-PAYLOAD
这是有效的 curl 请求(带有 Ruby 个变量):
`curl -v -X PUT -T #{target_file} \
-H "Content-Lenght: #{file_size}" \
-H "host: #{host}" \
-H "X-Amz-Content-Sha256: #{payload_hash}" \
-H "X-Amz-Date: #{timestamp}" \
-H "Authorization: AWS4-HMAC-SHA256 Credential=#{access_key_id + '/' + credential_scope}, SignedHeaders=#{headers_lst}, Signature=#{signature}" \
"https://qwerty123.data.mediastore.ap-northeast-1.amazonaws.com#{canonical_uri}?#{request_parameters}"`
@TonyJafar:感谢您的帮助
我想 POST 使用 Python 和 Signature v4 将一个 mp4 文件上传到 AWS MediaStore。我正在尝试使用 MediaStore 中的 PutObject
操作。
对于这项工作,我无法使用 SDK 或 CLI。
我可以在没有 SDK 或 CLI 的情况下使用 Python 向 MediaStore 发出 GET 请求,但是关于 POST 请求,我不明白我应该如何处理负载。我收到以下错误:
<InvalidSignatureException>
<Message>The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.</Message>
</InvalidSignatureException>
无法正常工作POST 代码
# NON WORKING CODE
import sys, os, base64, datetime, hashlib, hmac
import requests # pip install requests
method = 'PUT'
service = 'mediastore'
host = 'qwerty123.data.mediastore.ap-northeast-1.amazonaws.com'
region = 'ap-northeast-1'
endpoint = 'https://qwerty123.data.mediastore.ap-northeast-1.amazonaws.com'
request_parameters = 'Action=PutObject&Path=dummyfile.mp4&Version=2017-09-01'
cwd = os.getcwd()
def sign(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
def getSignatureKey(key, dateStamp, regionName, serviceName):
kDate = sign(('AWS4' + key).encode('utf-8'), dateStamp)
kRegion = sign(kDate, regionName)
kService = sign(kRegion, serviceName)
kSigning = sign(kService, 'aws4_request')
return kSigning
access_key = "AXXXXXXXXXXXA"
secret_key = "UXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXD"
if access_key is None or secret_key is None:
print('No access key is available.')
sys.exit()
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')
canonical_uri = '/'
canonical_querystring = request_parameters
payload_hash = hashlib.sha256(('').encode('utf-8')).hexdigest()
canonical_headers = 'content-type:video/mp4' + '\n' +'host:' + host + '\n' + 'x-amz-content-sha256:'+payload_hash + '\n' + 'x-amz-date:' + amzdate + '\n' + 'x-amz-storage-class:'+ "TEMPORAL" + '\n'
signed_headers = 'content-type;host;x-amz-content-sha256;x-amz-storage-class;x-amz-date'
canonical_request = method + '\n' + canonical_uri + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + '\n' + payload_hash
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/' + service + '/' + 'aws4_request'
string_to_sign = algorithm + '\n' + amzdate + '\n' + credential_scope + '\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
signing_key = getSignatureKey(secret_key, datestamp, region, service)
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature
headers = {'Content-Type':'video/mp4','x-amz-date':amzdate,'X-Amz-Content-Sha256':payload_hash, 'X-Amz-Storage-Class':'TEMPORAL', 'Authorization':authorization_header}
request_url = endpoint + '?' + canonical_querystring
print('\n * * * * * * * * * * * * BEGIN REQUEST * * * * * * * * * * * * ')
print('Request URL = ' + request_url)
files = {'file': open(cwd + "/" + "dummyfile.mp4", 'rb')}
r = requests.post(request_url, headers=headers, files=files)
print('\n* * * * * * * * * * * * * * RESPONSE * * * * * * * * * * * * *')
print('Response code: %d\n' % r.status_code)
print(r.text)
来自文档:
POST premium/canada/mlaw.avi
Host: aaabbbcccdddee.files.mediastore-us-west-2.com
x-amz-Date: 20170323T120000Z
Authorization: AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20141123/us-west-2/mediastore/aws4_request,SignedHeaders=host;x-amz-date;x-amz-mediastore-version,Signature=9257c16da6b25a715ce900a5b45b03da0447acf430195dcb540091b12966f2a2
Content-Length: 0
x-amz-mediastore-version: 2016-07-11
工作 GET 代码
# WORKING CODE
# NB Apparently the x-amz-content-sha256 header is not necessary
import sys, os, base64, datetime, hashlib, hmac
import requests # pip install requests
method = 'GET'
service = 'mediastore'
host = 'qwerty123.data.mediastore.ap-northeast-1.amazonaws.com'
region = 'ap-northeast-1'
endpoint = 'https://qwerty123.data.mediastore.ap-northeast-1.amazonaws.com'
request_parameters = 'Action=ListItems&Version=2017-09-01'
def sign(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
def getSignatureKey(key, dateStamp, regionName, serviceName):
kDate = sign(('AWS4' + key).encode('utf-8'), dateStamp)
kRegion = sign(kDate, regionName)
kService = sign(kRegion, serviceName)
kSigning = sign(kService, 'aws4_request')
return kSigning
access_key = "AXXXXXXXXXXXXXXA"
secret_key = "UXXXXXXXXXXXXXXXXXXXXXXXXXXXXD"
if access_key is None or secret_key is None:
print('No access key is available.')
sys.exit()
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')
canonical_uri = '/'
canonical_querystring = request_parameters
payload_hash = hashlib.sha256(('').encode('utf-8')).hexdigest()
canonical_headers = 'content-type:application/x-www-form-urlencoded; charset=utf-8' + '\n' +'host:' + host + '\n' + 'x-amz-date:' + amzdate + '\n' #+ 'x-amz-content-sha256:'+payload_hash + '\n'
signed_headers = 'content-type;host;x-amz-date' #x-amz-content-sha256;
canonical_request = method + '\n' + canonical_uri + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + '\n' + payload_hash
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/' + service + '/' + 'aws4_request'
string_to_sign = algorithm + '\n' + amzdate + '\n' + credential_scope + '\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
signing_key = getSignatureKey(secret_key, datestamp, region, service)
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature
headers = {'Content-Type':'application/x-www-form-urlencoded; charset=utf-8','x-amz-date':amzdate, 'Authorization':authorization_header} #'X-Amz-Content-Sha256':payload_hash,
request_url = endpoint + '?' + canonical_querystring
print('\n * * * * * * * * * * * * BEGIN REQUEST * * * * * * * * * * * * ')
print('Request URL = ' + request_url)
r = requests.get(request_url, headers=headers)
print('\n* * * * * * * * * * * * * * RESPONSE * * * * * * * * * * * * *')
print('Response code: %d\n' % r.status_code)
print(r.text)
非常感谢任何帮助。
编辑
我从 GET 请求中删除了 x-amz-content-sha256
header,请求仍然有效。
我尝试对 POST 请求做同样的事情,但没有成功。
您是否尝试将方法从 method = 'PUT'
更改为 method = 'POST'
?我认为这会对您有所帮助,因为文档中使用的方法是 POST
使用此代码进行 SHA 密钥签名:
def sign(key, msg):
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
def getSignatureKey(key, dateStamp, regionName, serviceName):
kDate = sign(("AWS4" + key).encode("utf-8"), dateStamp)
kRegion = sign(kDate, regionName)
kService = sign(kRegion, serviceName)
kSigning = sign(kService, "aws4_request")
return kSigning
来源AWS
常规上传请求: 资料来源:AWS-PDF
POST premium/canada/mlaw.avi
Host: aaabbbcccdddee.files.mediastore-us-west-2.com
x-amz-Date: 20170323T120000Z
Authorization: AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20141123/us-
west-2/mediastore/aws4_request,SignedHeaders=host;x-amz-date;x-amz-mediastore-version,Signature=9257c16da6b25a715ce900a5b45b03da0447acf430195dcb540091b12966f2a2
Content-Length: 0
x-amz-mediastore-version: 2016-07-11
我的脚本中有以下错误:
canonical_uri
必须设置为:/filename.mp4
以下 headers 是必需的:host,contentlength,x-amz-content-sha256,x-amz-date
payload_hash
必须设置为UNSIGNED-PAYLOAD
这是有效的 curl 请求(带有 Ruby 个变量):
`curl -v -X PUT -T #{target_file} \
-H "Content-Lenght: #{file_size}" \
-H "host: #{host}" \
-H "X-Amz-Content-Sha256: #{payload_hash}" \
-H "X-Amz-Date: #{timestamp}" \
-H "Authorization: AWS4-HMAC-SHA256 Credential=#{access_key_id + '/' + credential_scope}, SignedHeaders=#{headers_lst}, Signature=#{signature}" \
"https://qwerty123.data.mediastore.ap-northeast-1.amazonaws.com#{canonical_uri}?#{request_parameters}"`
@TonyJafar:感谢您的帮助