Skip to content

Commit

Permalink
Python 3 encoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleyg committed Jul 28, 2014
1 parent 0f6799f commit fac80e0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion s3direct/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_signing_fields(self):
self.client.login(username='admin', password='admin')
data = {'upload_to': 'foo', 'name': 'image.jpg', 'type': 'image/jpg'}
response = self.client.post(reverse('s3direct'), data)
response_dict = json.loads(response.content)
response_dict = json.loads(response.content.decode())
self.assertTrue(u'signature' in response_dict)
self.assertTrue(u'policy' in response_dict)
self.assertDictContainsSubset(FOO_RESPONSE, response_dict)
18 changes: 9 additions & 9 deletions s3direct/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

def create_upload_data(content_type, source_filename, upload_to):
access_key = settings.AWS_ACCESS_KEY_ID
secret_access_key = settings.AWS_SECRET_ACCESS_KEY.encode('utf-8')
secret_access_key = settings.AWS_SECRET_ACCESS_KEY
bucket = settings.AWS_STORAGE_BUCKET_NAME
endpoint = settings.S3DIRECT_ENDPOINT

Expand All @@ -31,11 +31,11 @@ def create_upload_data(content_type, source_filename, upload_to):
]
})

policy = policy_object.replace('\n', '').replace('\r', '').encode('utf-8')
policy = b64encode(
policy_object.replace('\n', '').replace('\r', '').encode())

signature = hmac.new(
bytes(secret_access_key),
b64encode(bytes(policy)),
hashlib.sha1).digest()
secret_access_key.encode(), policy, hashlib.sha1).digest()

signature_b64 = b64encode(signature)

Expand All @@ -45,12 +45,12 @@ def create_upload_data(content_type, source_filename, upload_to):
else:
filename = '${filename}'

key = "%s/%s" % (upload_to, filename)
bucket_url = "https://%s/%s" % (endpoint, bucket)
key = '%s/%s' % (upload_to, filename)
bucket_url = 'https://%s/%s' % (endpoint, bucket)

return {
"policy": str(policy),
"signature": str(signature_b64),
"policy": policy.decode(),
"signature": signature_b64.decode(),
"key": key,
"AWSAccessKeyId": access_key,
"form_action": bucket_url,
Expand Down

0 comments on commit fac80e0

Please sign in to comment.