[Cloudian # 10] Try to generate a signed URL for object publishing in Python (boto3)

Introduction

Cloudian is fully compatible with S3, so it's convenient to use the AWS SDK! Last time tried to display the metadata of the object in Python (boto3).

This time, I will try to generate a signed URL for publishing the object on the web with Python (boto3).

Generate presigned URL / generate_presigned_url ()

You can generate a public URL that can be used to share objects stored in Cloudian by calling generate_presigned_url ().

1. Generate a pre-signed URL with a default lifetime (3,600 seconds)

In the following example, "get_object" is set in the client method of generate_presigned_url () to generate a pre-signed URL that can get the object of the key "HyperStoreInstallGuide_v-7.2.1.pdf" stored in the bucket "pythonbucket3". I have.

If you generate a URL without specifying a lifetime, the URL will default to 3,600 seconds (1 hour).

test1.py


import boto3

client = boto3.client(
    's3',
    endpoint_url='https://xxx.yyy.com'
)


#Generate a signed URL for publishing the object on the web
client.generate_presigned_url(
    'get_object',
    Params={
        'Bucket': 'pythonbucket3',
        'Key': 'HyperStoreInstallGuide_v-7.2.1.pdf'
    }
)
'http://s3-region1.admin-tech.tokyo/pythonbucket3/HyperStoreInstallGuide_v-7.2.1.pdf?AWSAccessKeyId=4dfba0a142971dbde38b&Signature=tPwQma3II9quv%2BadJ2%2F5A%2B6NOzQ%3D&Expires=1607903186'

If you open the URL generated in this example, you can get the specified object as shown in the figure below. (Since it is a PDF that can be displayed in a browser, it is open in the browser)

3-9-1.png

2. Generate a pre-signed URL with an expiration date of 2 days (7,200 seconds)

The following example sets the argument of generate_presigned_url () to a 2-day expiration date (ExpiresIn = 7200) to generate a pre-signed URL.

test1.py


import boto3

client = boto3.client(
    's3',
    endpoint_url='https://xxx.yyy.com'
)


#Generate a signed URL for publishing the object on the web
client.generate_presigned_url(
    'get_object',
    Params={
        'Bucket': 'pythonbucket3',
        'Key': 'HyperStoreInstallGuide_v-7.2.1.pdf',
        },
    ExpiresIn=7200
)
'http://s3-region1.admin-tech.tokyo/pythonbucket3/HyperStoreInstallGuide_v-7.2.1.pdf?AWSAccessKeyId=4dfba0a142971dbde38b&Signature=3xU0rXwS4hjlsUZRykIqL4N2RCM%3D&Expires=1607906791'

3. Generate a pre-signed URL with an expiration date of 2 minutes (120 seconds)

In the following example, the argument of generate_presigned_url () is set to a very short expiration date (in this example, "ExpiresIn = 120"), and a pre-signed URL is generated to check its behavior.

Generate a pre-signed URL with a 2-minute expiration date

test1.py


import boto3

client = boto3.client(
    's3',
    endpoint_url='https://xxx.yyy.com'
)


#Generate a signed URL for publishing the object on the web
client.generate_presigned_url(
    'get_object',
    Params={
        'Bucket': 'pythonbucket3',
        'Key': 'HyperStoreInstallGuide_v-7.2.1.pdf',
        },
    ExpiresIn=120
)
'http://s3-region1.admin-tech.tokyo/pythonbucket3/HyperStoreInstallGuide_v-7.2.1.pdf?AWSAccessKeyId=4dfba0a142971dbde38b&Signature=nacVbc2OkUz22AcHhXVJ%2FQ9dMXo%3D&Expires=1607899714'

Access the generated URL within 2 minutes

The PDF is open in your web browser because the object pointed to by the presigned URL is in PDF format that you can browse in your web browser.

3-9-1.png

Access the generated URL after 2 minutes or more

Access to the object is denied with an error similar to the following: Since you specified "ExpiresIn = 120" when you generated the pre-signed URL, access that is more than 2 minutes old will be rejected as follows:

3-9-3.png

Summary

I tried to generate a signed URL for publishing the object WEB with Python (boto3).

Next time, I would like to operate various object storage/Cloudian with Python.

Recommended Posts

[Cloudian # 10] Try to generate a signed URL for object publishing in Python (boto3)
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
[Cloudian # 2] Try to display the object storage bucket in Python (boto3)
[Cloudian # 1] Try to access object storage with AWS SDK for Python (boto3)
[Cloudian # 5] Try to list the objects stored in the bucket with Python (boto3)
Try to calculate a statistical problem in Python
[Cloudian # 7] Try deleting the bucket in Python (boto3)
How to generate a Python object from JSON
Try to calculate RPN in Python (for beginners)
Try to make a Python module in C language
Try searching for a million character profile in Python
A simple way to avoid multiple for loops in Python
How to define multiple variables in a python for statement
Just try to receive a webhook in ngrok and python
Try to calculate Trace in Python
Generate S3 signed URL with boto
Try to get a list of breaking news threads in Python.
[Introduction to Python] How to use the in operator in a for statement?
[For beginners] How to register a library created in Python in PyPI
Generate a first class collection in Python
How to generate a new loggroup in CloudWatch using python within Lambda
Try logging in to qiita with Python
Generate AWS-S3 signed (time-limited) URLs in Python
How to make a model for object detection using YOLO in 3 hours
Try drawing a simple animation in Python
Create a JSON object mapper in Python
How to get a stacktrace in python
Module to generate word N-gram in Python
Try to set up a Vim test environment quite seriously (for Python)
Get a token for conoha in python
Generate a class from a string in Python
Try a functional programming pipe in Python
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
I searched for the skills needed to become a web engineer in Python
Searching for an efficient way to write a Dockerfile in Python with poetry
Try to implement Oni Maitsuji Miserable in python
3.14 π day, so try to output in Python
How to clear tuples in a list (Python)
How to generate permutations in Python and C ++
To execute a Python enumerate function in JavaScript
How to embed a variable in a python string
I want to create a window in Python
How to create a JSON file in Python
Try to draw a life curve with python
Try to make a "cryptanalysis" cipher with Python
A clever way to time processing in Python
Steps to develop a web application in Python
Try to automatically generate Python documents with Sphinx
To add a module to python put in Julialang
How to notify a Discord channel in Python
Try to make a dihedral group with Python
Create a datetime object from a string in Python (Python 3.3)
Difference in object ID due to import in Python
[Python] How to draw a histogram in Matplotlib
Issue the Amazon CloudFront Signed URL in Python
Put out a shortened URL string in Python
Insert an object inside a string in Python
Perform a Twitter search from Python and try to generate sentences with Markov chains.
[For beginners] Web scraping with Python "Access the URL in the page to get the contents"
Tool to make mask image for ETC in Python
[For beginners] How to use say command in python!