[Cloudian # 2] Try to display the object storage bucket in Python (boto3)

Introduction

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).

View Created Buckets / list_buckets ()

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.

1. Display without processing

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.

2. Narrow down and display information about buckets (each bucket name and creation date and time)

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

3. Narrow down the information about the bucket and display it, and format the creation date and time (each bucket name and creation date and time)

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.

4. Easy-to-read information about buckets (each bucket name and creation date and time)

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)

5. Summary

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

[Cloudian # 2] Try to display the object storage bucket in Python (boto3)
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
[Cloudian # 6] Try deleting the object stored in the bucket with Python (boto3)
[Cloudian # 3] Try to create a new object storage bucket with Python (boto3)
[Cloudian # 5] Try to list the objects stored in the bucket with Python (boto3)
[Cloudian # 1] Try to access object storage with AWS SDK for Python (boto3)
[Cloudian # 8] Try setting the bucket versioning with Python (boto3)
[Cloudian # 10] Try to generate a signed URL for object publishing in Python (boto3)
I want to display the progress in Python!
Python OpenCV tried to display the image in text.
[Python] Change the Cache-Control of the object uploaded to Cloud Storage
In the python command python points to python3.8
Try to calculate Trace in Python
Cython to try in the shortest
To make sure that the specified key is in the specified bucket in Boto 3
How to know the internal structure of an object in Python
How to display bytes in the same way in Java and Python
Try logging in to qiita with Python
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Display Python 3 in the browser with MAMP
How to display multiplication table in python
Python amateurs try to summarize the list ①
Try hitting the YouTube API in Python
How to display Hello world in python
Try to extract specific data from JSON format data in object storage Cloudian/S3
How to use the C library in Python
First steps to try Google CloudVision in Python
Try to implement Oni Maitsuji Miserable in python
Try to calculate a statistical problem in Python
3.14 π day, so try to output in Python
Try auto to automatically price Enums in Python 3.6
Try to solve the Python class inheritance problem
[Python] How to change the date format (display format)
Try to solve the man-machine chart with Python
Try using the BitFlyer Ligntning API in Python
Draw graphs in Julia ... Leave the graphs to Python
[Python] Display the Altair legend in the plot area
The trick to write flatten concisely in python
Try to calculate RPN in Python (for beginners)
How to get the files in the [Python] folder
Difference in object ID due to import in Python
Try implementing the Monte Carlo method in Python
Try using the DropBox Core API in Python
Try to display the Fibonacci sequence in various languages in the name of algorithm practice
Location information data display in Python --Try plotting with the map display library (folium)-
Try to display the railway data of national land numerical information in 3D
Convert the cURL API to a Python script (using IBM Cloud object storage)
How to retrieve the nth largest value in Python
I tried to graph the packages installed in Python
How to get the variable name itself in python
Try to make a Python module in C language
How to get the number of digits in Python
[Python] Try to read the cool answer to the FizzBuzz problem
How to know the current directory in Python in Blender
Try to improve your own intro quiz in Python
Try to solve the internship assignment problem with Python
Convert the image in .zip to PDF with Python
Object oriented in python
Try gRPC in Python
[IBM Cloud] Place the ISO file in the object storage