Tyler Tyler - 1 year ago 165
Python Question

Bucket POST must contain a field named 'key'

I am trying to upload an image to s3. I continue getting this error:

Bucket POST must contain a field named 'key'. If it is specified, please check the order of the fields.

here is my code I am sending to s3:

content_type = "image/JPEG"
key = 'uploads/filename.jpg'
acl = 'public-read'
bucket = None

params_raw = create_upload_data(content_type,key,acl,bucket)

params = { 'policy': params_raw['policy'],'acl':acl,'signature':params_raw['signature'],'key':params_raw['key'],'Content-Type':params_raw['Content-Type'],'AWSAccessKeyId':params_raw['AWSAccessKeyId'],'success_action_status':params_raw['success_action_status'],'file': binary_data}
r = requests.post(params_raw['form_action'],data=params,files={'file':binary_data})
return JsonResponse({'request':str(r.text)},safe=False)

I've tried moving the param 'key' to the front of params but that still produces the same error. What am I doing wrong?

Answer Source

Python dicts are unordered. When serializing them, there is no way to predict the order of the key/values pairs.

Try again with an OrderedDict.

Edit: As of Python 3.6, dicts are ordered by default, this is an implementation detail, not a new standard. Keep using OrderedDict, it is more explicit, more compatible and future proof.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download