8

I'm just a new with boto3 and i can not understand how i can get URL link for file that i have just uploaded to s3 amazon.

Please clarify.

Thank you

import boto3

s3 = boto3.resource('s3')

data = open ('file.xlsx', 'rb')
s3.Bucket ('dimxxx1').put_object (Key='file.xlsx', Body=data)
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
J_log
  • 223
  • 1
  • 3
  • 12
  • Does this answer your question? [Upload to Amazon S3 using Boto3 and return public url](https://stackoverflow.com/questions/33809592/upload-to-amazon-s3-using-boto3-and-return-public-url) – smac89 Oct 12 '21 at 21:21

4 Answers4

8

First, the better way to upload would be:

import boto3
s3 = boto3.resource('s3')
s3.Bucket('dimxxx1').upload_file('/tmp/file.xlsx', 'file.xlsx')

To obtain the URL, you can construct it from the basic elements:

http://s3-REGION-.amazonaws.com/BUCKET-NAME/KEY

For example:

http://s3-ap-southeast-2.amazonaws.com/dimxxx1/file.xlsx
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
7

in python3,

url = f"https://{bucket}.s3.{region}.amazonaws.com/{folder}{file_name}"
alan_blk
  • 163
  • 1
  • 6
4

Here is a fix for this issue to enable you get the URL of S3 file as suggested by this link. You basically have to generate a pre-signed URL for each S3 object you wish to provide access to. See the code below:

import boto3

# Get the service client.
s3 = boto3.client('s3')

# 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>'
    }
)

Change the <bucket-name> and <key-name> to your S3 bucket and the name of the file that was uploaded.

Dalton Whyte
  • 657
  • 1
  • 8
  • 15
  • thanks for your solutions. now i have a link but when i copy and past link to another browser which is not log in amazon a couldn't download file. receive error access denied. how it can be solved? – J_log Nov 06 '18 at 16:07
  • I'd warn here that pre-signed URLs have an expiration timer (max is one week) so if these need to be links that are stateful, do not use pre-signed URLs. https://stackoverflow.com/questions/24014306/aws-s3-pre-signed-url-without-expiry-date – BLang Sep 17 '19 at 18:03
1

This is proper sequence with python formatter:

url = f"https://s3.{region}.amazonaws.com/{bucket}/{folder}...N/{file_name}"
Atishay
  • 41
  • 9