[Cloudian # 3] Try to create a new object storage bucket with Python (boto3)

Introduction

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

This time, I would like to create a new object storage bucket in Python (boto3).

Create new bucket / create_bucket ()

Create a new bucket in Object Storage/Cloudian.

1. Simple bucket creation example (all attributes are default)

In the following example, only the bucket name (Bucket = ‘pythonbucket1’) to be created is set in the argument of create_bucket (), and all attribute values ​​are created using the default.

test1.py


import boto3

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

#Create a new bucket
client.create_bucket(Bucket='pythonbucket1')
{'ResponseMetadata': {'RequestId': '9dad4484-0e30-1dbc-a754-06bdfcde1d5e',
  'HostId': '3d5c05376530a2eb49e3e90576f83c5b',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'date': 'Mon, 14 Dec 2020 01:51:29 GMT',
   'x-amz-request-id': '9dad4484-0e30-1dbc-a754-06bdfcde1d5e',
   'location': 'http://pythonbucket1.s3-region1.admin-tech.tokyo',
   'x-amz-id-2': '3d5c05376530a2eb49e3e90576f83c5b',
   'content-length': '0',
   'server': 'CloudianS3'},
  'RetryAttempts': 0},
 'Location': 'http://pythonbucket1.s3-region1.admin-tech.tokyo'}

Since list_buckets () returns a great deal of information, we will narrow down the output and display it in the following examples.

2. Example of creating a bucket with specified attributes ①

In the following example, the bucket is created by setting the bucket name (Bucket ='pythonbucket1') to be created and the ACL (ACL ='public-read') of that bucket in the argument of create_bucket ().

test2.py


import boto3

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

#Create a new bucket
client.create_bucket(
    ACL='public-read',
    Bucket='pythonbucket1'
)
{'ResponseMetadata': {'RequestId': '9dad4487-0e30-1dbc-a754-06bdfcde1d5e',
  'HostId': '3d5c05376530a2eb49e3e90576f83c5b',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'date': 'Mon, 14 Dec 2020 01:51:30 GMT',
   'x-amz-request-id': '9dad4487-0e30-1dbc-a754-06bdfcde1d5e',
   'location': 'http://pythonbucket1.s3-region1.admin-tech.tokyo',
   'x-amz-id-2': '3d5c05376530a2eb49e3e90576f83c5b',
   'content-length': '0',
   'server': 'CloudianS3'},
  'RetryAttempts': 0},
 'Location': 'http://pythonbucket1.s3-region1.admin-tech.tokyo'}

3. Bucket creation example with specified attributes ②

In the following example, the bucket name (Bucket ='bucket2') and bucket ACL (ACL ='private') to be created in the argument of create_bucket (), and the location constraint (CreateBucketConfiguration = {'LocationConstraint':'region1'). }) Is set to create a bucket.

test3.py


import boto3

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

#Create a new bucket
client.create_bucket(
    ACL='private',
    Bucket='pythonbucket1',
    CreateBucketConfiguration={
        'LocationConstraint': 'region1'
    }
)
{'ResponseMetadata': {'RequestId': '9dad448a-0e30-1dbc-a754-06bdfcde1d5e',
  'HostId': '3d5c05376530a2eb49e3e90576f83c5b',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'date': 'Mon, 14 Dec 2020 01:51:31 GMT',
   'x-amz-request-id': '9dad448a-0e30-1dbc-a754-06bdfcde1d5e',
   'location': 'http://pythonbucket1.s3-region1.admin-tech.tokyo',
   'x-amz-id-2': '3d5c05376530a2eb49e3e90576f83c5b',
   'content-length': '0',
   'server': 'CloudianS3'},
  'RetryAttempts': 0},
 'Location': 'http://pythonbucket1.s3-region1.admin-tech.tokyo'}

The CreationDate returned by the datetime type is formatted and output for easy viewing using strftime () included in the Python standard module.

4. Summary

I tried to create a new bucket with various patterns in Python (boto3).

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

Recommended Posts

[Cloudian # 3] Try to create a new object storage bucket with 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 # 6] Try deleting the object stored in the bucket with Python (boto3)
[Cloudian # 5] Try to list the objects stored in the bucket with 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)
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
Try to create a python environment with Visual Studio Code & WSL
[Cloudian # 7] Try deleting the bucket in Python (boto3)
Try to draw a life curve with python
Try to make a "cryptanalysis" cipher with Python
Steps to create a Twitter bot with python
Try to create a new command on linux
Create a new page in confluence with Python
Try to make a dihedral group with Python
Try to make a command standby tool with python
Try to dynamically create a Checkbutton with Python's Tkinter
[Python] How to create a 2D histogram with Matplotlib
Create a directory with python
Try to create a waveform (audio spectrum) that moves according to the sound with python
Create a Mastodon bot with a function to automatically reply with Python
Probably the easiest way to create a pdf with Python3
Try to bring up a subwindow with PyQt5 and Python
Create a message corresponding to localization with python translation string
Create folders from '01' to '12' with python
Try to operate Facebook with Python
Create a virtual environment with Python!
5 Ways to Create a Python Chatbot
Try to extract a character string from an image with Python3
Rails users try to create a simple blog engine with Django
I tried to create a list of prime numbers with python
How to create a heatmap with an arbitrary domain in Python
Try adding a wall to your IFC file with IfcOpenShell python
Try to create a Qiita article with REST API [Environmental preparation]
Create a Python function decorator with Class
Try logging in to qiita with Python
Create a new Python numerical calculation project
Build a blockchain with Python ① Create a class
Create a dummy image with Python + PIL.
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Create a JSON object mapper in Python
Create a word frequency counter with Python 3.4
Try HTML scraping with a Python library
Try drawing a map with python + cartopy 0.18.0
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
Try to make a capture software with as high accuracy as possible with python (2)
Create a tool to automatically furigana with html using Mecab from Python3
Steps to create a Python virtual environment with VS Code on Windows
[Outlook] I tried to automatically create a daily report email with Python
Try to solve a set problem of high school math with Python
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
Copy data from Amazon S3 to Google Cloud Storage with Python (boto)
3. Natural language processing with Python 1-2. How to create a corpus: Aozora Bunko
Create a frame with transparent background with tkinter [Python]
[Python] List Comprehension Various ways to create a list
Try to solve the traveling salesman problem with a genetic algorithm (Python code)
Edit Excel from Python to create a PivotTable
How to read a CSV file with Python 2/3
Send a message to LINE with Python (LINE Notify)