17

In Boto, I used to generate a signed URL using the below function.

import boto
conn = boto.connect_s3()
bucket = conn.get_bucket(bucket_name, validate=True)
key = bucket.get_key(key)
signed_url = key.generate_url(expires_in=3600)

How do I do the exact same thing in boto3?
I searched through boto3 GitHub codebase but could not find a single reference to generate_url.
Has the function name changed?

meadhikari
  • 971
  • 3
  • 13
  • 27
  • 1
    Possible duplicate of [how to generate url from boto3 in amazon web services](https://stackoverflow.com/questions/33549254/how-to-generate-url-from-boto3-in-amazon-web-services) – Red Boy Jun 27 '18 at 08:39

2 Answers2

40

From Generating Presigned URLs:

import boto3
import requests
from botocore import client


# Get the service client.
s3 = boto3.client('s3', config=client.Config(signature_version='s3v4'))

# Generate the URL to get 'key-name' from 'bucket-name'
url = s3.generate_presigned_url(
    ClientMethod='get_object',
    Params={
        'Bucket': 'bucket-name',
        'Key': 'key-name'
    },
    ExpiresIn=3600 # one hour in seconds, increase if needed
)

# Use the URL to perform the GET operation. You can use any method you like
# to send the GET, but we will use requests here to keep things simple.
response = requests.get(url)

Function reference: generate_presigned_url()

Jossef Harush Kadouri
  • 32,361
  • 10
  • 130
  • 129
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • 3
    I get error `InvalidRequestThe authorization mechanism you have provided is not supported` when trying to access the url generated in normal browser – Aseem Apr 30 '19 at 05:22
  • @Aseem Please create a new question with the details. – John Rotenstein May 01 '19 at 01:24
  • 2
    Hi.Where should I add my `access_key` ,`secret_key` in this code? I am new to this.They're required I suppose. – TheGuardener Jun 16 '20 at 05:40
  • 3
    @TheGuardener You should _never_ add your Access Key and Secret Key in the code. If the code is running on an Amazon EC2 instance, simply assign an IAM Role to the instance. If the code is running on your own computer, use the [AWS Command-Line Interface (CLI)](http://aws.amazon.com/cli/) `aws configure` command to store the credentials in a configuration file. In both cases, the code will automatically find the credentials. – John Rotenstein Jun 16 '20 at 05:43
  • Thank you so much John. If I am hosting my application on Heroku,where should I store my credentials? Should I upload them to Heroku? If so,how to access them via boto3? – TheGuardener Jun 16 '20 at 05:51
  • Never mind.Found it here.https://devcenter.heroku.com/articles/s3#credentials – TheGuardener Jun 16 '20 at 05:57
7

I get error InvalidRequestThe authorization mechanism you have provided is not supported when trying to access the url generated in normal browser – Aseem Apr 30 '19 at 5:22

As there isn't much info i am assuming you are getting signature version issue, if not maybe it will help someone else ! :P

For this you can import Config from botocore:-

from botocore.client import Config

and then get the client using this config and providing signature version as 's3v4'

s3 = boto3.client('s3', config=Config(signature_version='s3v4'))
Taazar
  • 1,545
  • 18
  • 27
Aakash Jaswal
  • 71
  • 1
  • 1