Created
December 3, 2018 21:22
-
-
Save avaitla/925588ef907039e88c91ae613f9dc78b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests, boto3, base64, hashlib | |
access_key = "..." | |
secret_key = "..." | |
bucket = "..." | |
s3 = boto3.client('s3', | |
aws_access_key_id=access_key, | |
aws_secret_access_key=secret_key) | |
test_encrypt_key = os.urandom(32) | |
test_uuid_file = str(uuid.uuid4()) | |
def get_url(filename): | |
return s3.generate_presigned_url( | |
ClientMethod='get_object', | |
Params={ | |
'Bucket': bucket, | |
'Key': filename, | |
'SSECustomerAlgorithm': 'AES256', | |
} | |
) | |
def post_url(filename): | |
return s3.generate_presigned_url( | |
ClientMethod='put_object', | |
Params={ | |
'Bucket': bucket, | |
'Key': filename, | |
'SSECustomerAlgorithm': 'AES256', | |
} | |
) | |
def test_uploading_with_signed_urls(): | |
encoded_key = base64.b64encode(test_encrypt_key) | |
md5_key = base64.b64encode(hashlib.md5(test_encrypt_key).digest()) | |
post_address = post_url(test_uuid_file) | |
print("POST: %s" % (post_address)) | |
resp = requests.put(post_address, data="hello world", headers={ | |
"x-amz-server-side-encryption-customer-algorithm": "AES256", | |
"x-amz-server-side-encryption-customer-key": encoded_key, | |
"x-amz-server-side-encryption-customer-key-MD5": md5_key | |
}) | |
resp.raise_for_status() | |
get_address = get_url(test_uuid_file) | |
print("GET: %s" % (get_address)) | |
resp = requests.get(get_address, headers={ | |
"x-amz-server-side-encryption-customer-algorithm": "AES256", | |
"x-amz-server-side-encryption-customer-key": encoded_key, | |
"x-amz-server-side-encryption-customer-key-MD5": md5_key | |
}) | |
resp.raise_for_status() | |
print("Got Payload: %s" % resp.content) | |
if __name__ == "__main__": | |
test_uploading_with_signed_urls() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you. This works to download the file. Let's say that I want to generate an URL for an jpeg image which I want to share and it expires in certain time. How would I do that?
In a nutshell how to get the URL from this method