[Cloudian # 1] Try to access object storage with AWS SDK for Python (boto3)

Introduction

Cloudian is an object storage device with AWS S3 compatible API.

Since it is an S3 compatible API, it can be operated using the AWS SDK, but since there are many articles that assume connecting to S3, I will write a method to connect to an object storage other than S3.

This time, we will check that you can get the bucket list (bucket list) of object storage using AWS SDK for Python (boto3).

1. Install AWS SDK for Python (boto3)

In order to work with S3 files, you need to import the AWS SDK for Python (boto3) in Python. boto3 can be easily installed with the pip command

$ pip install boto3

2. Setting credential information

Set the credential information (access key and secret key) to use boto3 from Python. Easy to set up by installing the AWS CLI

AWS CLI installation

Install with pip command

$ pip install awscli

Configure settings

Run the aws configure command --Enter Cloudian credential information (access key and secret key) --Default region name is an empty enter --Default output format is json

$ aws configure

AWS Access Key ID: xxxxxxxxxxxxxxxxx
AWS Secret Access Key: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
Default region name: 
Default output format: json

This completes the setting of credential information. You can check the settings in "~/.aws/credentials" and "~/.aws/config".

$ cat ~/.aws/credentials

[default]  
aws_access_key_id = xxxxxxxxxxxxxxxxx
aws_secret_access_key = yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy


$ cat ~/.aws/config

[default]
output=json

boto3 import

In order to work with S3 files, you need to import boto3 in Python.

import boto3

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

Set the following as arguments of boto3.client.

--Set the AWS service type in the first argument. Set's3'here.

--In the second argument, set "endopoint_url ='Object storage S3 endpoint'" to specify the object storage S3 endpoint instead of the AWS endpoint.

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

I have set ‘s3’ as the AWS service type and endpoint_url ='http://xxx.yyy.com' as the URL of the S3 endpoint.

The endpoint_url setting overrides the AWS S3 endpoint, which is the default reference, with the object storage S3 endpoint.

Reference information) If you want to set the credential information in the Python program (if you do not set it in the AWS CLI/Configure settings) You can also connect by passing the credential information (access key and secret key) to the boto3.client parameter.

 client = boto3.client(
    's3',
    endpoint_url='https://xxx.yyy.com',
    
    # Hard coded strings as credentials, not recommended.
    aws_access_key_id='4dfba0a142971dbde38b',
    aws_secret_access_key='FdgZNJ7udliVpbc9HE5M8sgAz5nvzn1y6uCHl7YQ'
)

Note) You can try it immediately without AWS CLI/Configure settings, but it is not recommended to write the credentials programmatically (for verification purposes).

_

Reference information) About creating S3 objects The AWS SDKs, including boto3, provide a 1: 1 "low-level API" for the S3 API and an object-oriented "high-level API" for more advanced operations.

・ Client API (low level API): S3Client (boto3.client) It has a one-to-one correspondence with AWS REST API.

・ Resource API (high level API): S3Resource (boto3.resource) AWS resources can be handled in an object-oriented manner.

This sample uses the low-level API S3Client (boto3.client) object.

3. Operation check

test.py


import boto3

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

#Get a list of buckets
client.list_buckets()

You can execute the above code to get a list of buckets set in object storage/Cloudian.

$ python test1.py 

{'ResponseMetadata': {'RequestId': '9dad38b5-0e30-1dbc-a754-06bdfcde1d5e',
  'HostId': '',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'date': 'Sun, 13 Dec 2020 21:57:43 GMT',
   'x-amz-request-id': '9dad38b5-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'}}

4. Summary

The boto3 S3Client object created by the Python program has many methods that can perform various operations (create/delete buckets, upload/download data, etc.) to the object storage/Cloudian.

Detailed information on the methods available in S3Client (boto3.client) https://boto3.amazonaws.com/v1/documentation/api/latest/index.html

Next time wants to use this S3Client object to operate Cloudian, which is an object storage, in various ways.

Recommended Posts

[Cloudian # 1] Try to access object storage with AWS SDK for Python (boto3)
[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 # 10] Try to generate a signed URL for object publishing in Python (boto3)
boto3 (AWS SDK for Python) Note
[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)
How to access data with object ['key'] for your own Python class
Try using S3 object upload and download with AWS SDK for Go v2
Authentication information used by Boto3 (AWS SDK for Python)
[Cloudian # 8] Try setting the bucket versioning with Python (boto3)
Fleet provisioning with AWS IoT SDK for Python v2
AWS SDK for Python (Boto3) development in Visual Studio 2017
Use AWS SDK for Python (boto) under Proxy environment
Try to display various information useful for debugging with python
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
How to upload files to Cloud Storage with Firebase's python SDK
Python code for writing CSV data to DSX object storage
Try to operate Facebook with Python
Upload files to Aspera that comes with IBM Cloud Object Storage (ICOS) using SDK (Python version)
Try to reproduce color film with Python
Try logging in to qiita with Python
Memo to ask for KPI with python
I want to play with aws with python
Connect to s3 with AWS Lambda Python
[Continued] Try PLC register access with Python
[For beginners] Try web scraping with Python
Use Resource API rather than Client API in AWS SDK for Python (Boto3)
Copy data from Amazon S3 to Google Cloud Storage with Python (boto)
[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
Try to automatically generate Python documents with Sphinx
Try to calculate RPN in Python (for beginners)
Manage AWS nicely with the Python library Boto
Python mock to try AWS IoT Device Shadow
Try to make a dihedral group with Python
Call Polly from the AWS SDK for Python
Try to detect fish with python + OpenCV2.4 (unfinished)
Get an Access Token for your service account with the Firebase Admin Python SDK
[For beginners] Web scraping with Python "Access the URL in the page to get the contents"
Try to solve the programming challenge book with python3
[First API] Try to get Qiita articles with Python
Try to solve the internship assignment problem with Python
Yum command to access MySQL with Python 3 on Linux
Try to operate DB with Python and visualize with d3
[Python] Local → Procedure for uploading files to S3 (boto3)
Create a Layer for AWS Lambda Python with Docker
I want to AWS Lambda with Python on Mac!
For those who want to write Python with vim
Try to automate pdf format report creation with Python
Try writing JSON format data to object storage Cloudian/S3
ODBC access to SQL Server from Linux with Python
AWS CDK with Python
[Python] Change the Cache-Control of the object uploaded to Cloud Storage
Update Python for Raspberry Pi to 3.7 or later with pyenv
Run AWS IoT Device SDK for Python on Raspberry Pi
Try Amazon Simple Workflow Service (SWF) with Python and boto3
How to specify Cache-Control for blob storage in Azure Storage in Python
Try to bring up a subwindow with PyQt5 and Python
Create a Twitter BOT with the GoogleAppEngine SDK for Python