Cloudian is fully compatible with S3, so it is convenient to use the AWS SDK.
Last time set the AWS SDK credential and tried to access the object storage with Python (boto3) for the time being.
This time, I would like to display the created bucket in various patterns with Python (boto3).
Returns information for all buckets owned by the user authenticated with the access key and secret key. The return value is returned as a Python dict type.
In the example below, list_buckets () is simply called to display all return values.
test1.py
import boto3
client = boto3.client(
's3',
endpoint_url='https://xxx.yyy.com'
)
#Get a list of buckets
response = client.list_buckets()
print(response)
{'ResponseMetadata': {'RequestId': '9dad38b7-0e30-1dbc-a754-06bdfcde1d5e',
'HostId': '',
'HTTPStatusCode': 200,
'HTTPHeaders': {'date': 'Sun, 13 Dec 2020 21:58:07 GMT',
'x-amz-request-id': '9dad38b7-0e30-1dbc-a754-06bdfcde1d5e',
'content-type': 'application/xml;charset=UTF-8',
'content-length': '519',
'server': 'CloudianS3'},
'RetryAttempts': 0},
'Buckets': [{'Name': 'bucket1',
'CreationDate': datetime.datetime(2020, 12, 1, 3, 2, 25, 876000, tzinfo=tzutc())},
{'Name': 'pythonbucket2',
'CreationDate': datetime.datetime(2020, 12, 13, 19, 51, 20, 267000, tzinfo=tzutc())},
{'Name': 'pythonbucket3',
'CreationDate': datetime.datetime(2020, 12, 13, 21, 41, 8, 495000, tzinfo=tzutc())}],
'Owner': {'DisplayName': '', 'ID': '27b8e84694ca0b529d5379049564ebe1'}}
Since list_buckets () returns a great deal of information, we will narrow down the output and display it in the following examples.
The value of the dictionary key "Buckets" is extracted from the dictionary type return value returned by list_buckets (), and further narrowed down to "Name" (bucket name) and "Creation Date" (creation date) and displayed.
test2.py
import boto3
client = boto3.client(
's3',
endpoint_url='https://xxx.yyy.com'
)
#Get a list of buckets
for bucket in client.list_buckets()['Buckets']:
print(bucket['Name'], bucket['CreationDate'])
bucket1 2020-12-01 03:02:25.876000+00:00
pythonbucket2 2020-12-13 19:51:20.267000+00:00
pythonbucket3 2020-12-13 21:41:08.495000+00:00
list_buckets () returns information about created buckets in a dictionary (dict) type, so you can programmatically extract only the data that your program needs.
Extract only the bucket name of string type and the creation date of datetime type
It narrows down to the bucket name and its creation date, and formats the displayed creation date.
test3.py
import boto3
client = boto3.client(
's3',
endpoint_url='https://xxx.yyy.com'
)
#Get a list of buckets
for bucket in client.list_buckets()['Buckets']:
print(bucket['Name'],bucket['CreationDate'].strftime("%Y/%m/%d %H:%M:%S"))
bucket1 2020/12/01 03:02:25
pythonbucket2 2020/12/13 19:51:20
pythonbucket3 2020/12/13 21:41:08
The CreationDate returned by the datetime type is formatted and output for easy viewing using strftime () included in the Python standard module.
It is displayed in Japan time with +9 hours added to the output bucket creation date.
test4.py
import boto3
#Additional modules
import datetime
client = boto3.client(
's3',
endpoint_url='https://xxx.yyy.com'
)
#Get a list of buckets
for bucket in client.list_buckets()['Buckets']:
print('%s (%s)' % (bucket['Name'], (bucket['CreationDate'] + datetime.timedelta(hours=9)).strftime("%Y/%m/%d %H:%M:%S")))
bucket1 (2020/12/01 12:02:25)
pythonbucket2 (2020/12/14 04:51:20)
pythonbucket3 (2020/12/14 06:41:08)
I tried to display the created bucket in various patterns with Python (boto3).
Next time, I would like to operate object storage/Cloudian in various ways with Python.
Recommended Posts