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).
You can generate a public URL that can be used to share objects stored in Cloudian by calling generate_presigned_url ().
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)
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'
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.
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'
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.
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:
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