启动分段上传到S3版本4方法不允许err.code 405

时间:2018-06-21 15:48:07

标签: python rest amazon-s3 multipart

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

如果任何人都可以编辑此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

0 个答案:

没有答案