Skip to content

Instantly share code, notes, and snippets.

@a5ync
Created December 1, 2022 07:16
Show Gist options
  • Select an option

  • Save a5ync/03617eb6e80e76b0eb7f4c6ca1e42404 to your computer and use it in GitHub Desktop.

Select an option

Save a5ync/03617eb6e80e76b0eb7f4c6ca1e42404 to your computer and use it in GitHub Desktop.
s3 signed url without sdk (python3)
#!/usr/bin/env python
import base64, hmac, os, hashlib, sys, time, urllib.parse
TWENTY_YEARS_IN_SECONDS = 60 * 60 * 24 * 365 * 20
aws_access_key_id = 'AAA'
aws_secret_access_key = 'BBB'
aws_region='us-west-2'
bucket = 'bucket_name'
key='another-test.md'
seconds_alive = 1800
resource = urllib.parse.quote(f"/{bucket}/{key}")
expires = int(time.time()) + seconds_alive
# see https://docs.aws.amazon.com/AmazonS3/latest/userguide/RESTAuthentication.html#RESTAuthenticationStringToSign
raw_value = "GET\n\n\n{0}\n{1}".format(expires, resource).encode("ascii")
key=f"{aws_secret_access_key}".encode("ascii")
signature = base64.b64encode(hmac.new(key, raw_value, hashlib.sha1).digest())
print ("https://s3.{0}.amazonaws.com{1}?AWSAccessKeyId={2}&Expires={3}&Signature={4}".format(aws_region,
resource, urllib.parse.quote(aws_access_key_id), expires, urllib.parse.quote(signature)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment