首页 > 解决方案 > 启动分段上传到 S3 版本 4 方法不允许 err.code 405

问题描述

我已经尝试了一切。它适用于 GET AND PUT 请求。但是使用 POST(启动多部分请求)会引发各种错误,例如 405 Method not allowed 或 Signature Mismatch 错误。任何帮助表示赞赏和欢迎。如果有人有任何想法,请提出一些提示。

如果有人可以编辑此 python 代码以使其正常工作,我将不胜感激。提前致谢。

import sys, os, base64, datetime, hashlib, hmac
import requests # pip install requests


method = 'POST'
service = 's3'
host = 'freemedianews.s3.amazonaws.com'
region = 'ap-southeast-1'
endpoint = 'https://freemedianews.s3-ap-southeast-1.amazonaws.com'
request_parameters = ''


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 = '*******'
secret_key = '*******'
if access_key is None or secret_key is None:
    print 'No access key is available.'
    sys.exit()


t = datetime.datetime.utcnow()
amzdate = '20180621T151517Z'
datestamp = '20180621' # Date w/o time, used in credential scope


request.html


canonical_uri = '/a/message/1200/1200.png'
content_type = "multipart/form-data"



request_parameters variable.
canonical_querystring = request_parameters



payload_hash = hashlib.sha256('').hexdigest()
canonical_headers ='content-type:' + content_type + '\n' + 'host:' + host + '\n' + 'x-amz-content-sha256:' + payload_hash + '\n' + 'x-amz-date:' + amzdate + '\n'


signed_headers = 'content-type;host;x-amz-content-sha256;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'
credential_scope_final = access_key  + "/"  + datestamp + "/" + region + '/' + service + '/' + 'aws4_request'
string_to_sign = algorithm  + '\n' +  amzdate + '\n' + credential_scope + '\n' +  hashlib.sha256(canonical_request).hexdigest()




signing_key = getSignatureKey(secret_key, datestamp, region, service)

print "signing_key -----" + signing_key



signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
print "signature -----" + signature

authorization_header = algorithm + ' ' + 'Credential='  + credential_scope_final + ', ' +  'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature



headers = {'Content-Type': content_type, 'Host': host, 'X-Amz-Content-Sha256': payload_hash, 'X-Amz-Date':amzdate, 'Authorization':authorization_header}
print authorization_header + "---this"


request_url = endpoint + canonical_uri 





r = requests.post(request_url, data=request_parameters, headers=headers)
print r

print '\nRESPONSE++++++++++++++++++++++++++++++++++++'
print 'Response code: %d\n' % r.status_code
print r.text

标签: pythonrestamazon-s3multipart

解决方案


推荐阅读