python - 使用不带 SDK 和 CLI 的 Python 3 将文件发布到 AWS Mediastore

标签 python amazon-web-services mediastore aws-mediastore aws-mediaservices

我想使用 Python 和签名 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 key 签名:

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

Troubleshooting

常规上传请求: 来源: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

关于python - 使用不带 SDK 和 CLI 的 Python 3 将文件发布到 AWS Mediastore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56142601/

相关文章:

python - 不应打开任何文件时 PyTorch 的数据加载器 "too many open files"错误

python - 将 Plone 2 root 重定向到新域

android - AWS cognito 与 AppAuth 集成

android - 如何显示歌曲时长

android - 按歌曲获取封面图片

python - 如何在Python3中加速 "for loop"

python - AWS CDK将CreationPolicy添加到EC2实例

amazon-web-services - 云信息 : Autoscaling persist volume after instance stop and add tags

android - 如何使用 MediaStore.Images.Thumbnails.query?

python - 如何按句子编号、句子(按 '|' 分割)在 CSV 中写入文件?