[Cloudian # 8] Try setting the bucket versioning with Python (boto3)

Introduction

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

This time, I will use Python (boto3) to set the versioning of the object storage bucket.

Bucket versioning settings

Enable the versioning function and check its operation. It is assumed that the bucket "pythonbucket2" has already been created and exists.

1. Enable bucket versioning / put_bucket_versioning ()

Pass the bucket name to change the versioning state and the state to change (here ‘Enabled’) to the argument of put_bucket_versioning () and execute it to enable the versioning function.

In the following example, the status of the versioning function of the bucket "pythonbucket2" is changed to Enabled ('Status': ‘Enabled’).

test1.py


import boto3

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


#Bucket name:Enable versioning feature for pythonbucket2
client.put_bucket_versioning(
    Bucket='pythonbucket2',
    VersioningConfiguration={'Status': 'Enabled'}
)
{'ResponseMetadata': {'RequestId': '9dad3e00-0e30-1dbc-a754-06bdfcde1d5e',
  'HostId': '',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'date': 'Sun, 13 Dec 2020 22:30:33 GMT',
   'x-amz-request-id': '9dad3e00-0e30-1dbc-a754-06bdfcde1d5e',
   'content-length': '0',
   'server': 'CloudianS3'},
  'RetryAttempts': 0}}

** Caution ** Once the bucket versioning feature is enabled, it cannot be disabled thereafter. If you want to suspend the versioning function, pass "Suspended" to the Status of VersioningConfiguration and execute put_bucket_versioning () as described later.

2. Check the versioning status of the bucket / get_bucket_versioning ()

Execute it by passing the bucket name to check the versioning status to the argument of get_bucket_versioning ().

In the following example, the versioning state of the bucket "pythonbucket2" is acquired.

test1.py


import boto3

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


#Bucket name:Get versioning state of pythonbucket2
client.get_bucket_versioning(Bucket='pythonbucket2')
 {'ResponseMetadata': {'RequestId': '9dad3e02-0e30-1dbc-a754-06bdfcde1d5e',
  'HostId': '',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'date': 'Sun, 13 Dec 2020 22:30:36 GMT',
   'x-amz-request-id': '9dad3e02-0e30-1dbc-a754-06bdfcde1d5e',
   'content-type': 'application/xml;charset=UTF-8',
   'content-length': '161',
   'server': 'CloudianS3'},
  'RetryAttempts': 0},
 'Status': 'Enabled'}

3-7-1.png

3. Upload the file to a versioned bucket

Try uploading the file multiple times with the same key to the versioned bucket using upload_file () from S3 Transfers above.

In the following example, the file is uploaded to the bucket "pythonbucket2" with the versioning function enabled four times in total with "10mb.dat" set as the key.

upload1.py


import boto3

from boto3.s3.transfer import S3Transfer
from boto3.s3.transfer import TransferConfig


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

config = TransferConfig(
    multipart_threshold = 8 * 1024 * 1024,
    max_concurrency = 10,
    multipart_chunksize = 8388608,
    num_download_attempts = 10,
    max_io_queue = 100
)


#Creating an S3Transfer object
transfer = S3Transfer(client, config)


#【First time】
transfer.upload_file('fileup/10mb.dat', 'pythonbucket2', '10mb.dat')


#【Second time】
transfer.upload_file('fileup/10mb.dat', 'pythonbucket2', '10mb.dat',
    extra_args={
	    'ACL': 'public-read',
	    'Metadata': {
	                    'Purpose': 'boto3 demo 1',
	                    'Engineer': 'yamahiro',
	                    'Company': 'nw'
	                },
	    'ServerSideEncryption': 'AES256'
	    }
    )


#【Third time】
transfer.upload_file('fileup/10mb.dat', 'pythonbucket2', '10mb.dat',
    extra_args={
                'ACL': 'public-read',
                'Metadata': {
                    'Purpose': 'boto3 demo 2',
                    'Engineer': 'miki',
                    'Company': 'nw'
                },
                	'ServerSideEncryption': 'AES256'
                }
    )
    
    
#【4th】
transfer.upload_file('fileup/10mb.dat', 'pythonbucket2', '10mb.dat',
    extra_args={'ServerSideEncryption': 'AES256'}
)      
  • Add user-defined metadata when uploading extra_args = {Metadata': {'name':'value', .....}}

  • Specify encryption (AES256) when uploading 'ServerSideEncryption': 'AES256'

4. List versioned objects / list_object_versions ()

To list the versioned (not currently the latest version) objects (files), run list_object_versions ().

Display without processing

In the following example, the bucket name in the argument of list_object_versions () Only (Bucket ='pythonbucket2') is set and all return values ​​are displayed.

client.list_object_versions(Bucket='pythonbucket2')
{'ResponseMetadata': {'RequestId': '9dad3e3e-0e30-1dbc-a754-06bdfcde1d5e',
  'HostId': '',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'date': 'Sun, 13 Dec 2020 22:35:45 GMT',
   'x-amz-request-id': '9dad3e3e-0e30-1dbc-a754-06bdfcde1d5e',
   'x-gmt-policyid': 'b46db4b3ebb2180b046ad1065c9702e1',
   'x-amz-bucket-region': 'region1',
   'content-type': 'application/xml;charset=UTF-8',
   'content-length': '1674',
   'server': 'CloudianS3'},
  'RetryAttempts': 0},
 'IsTruncated': False,
 'KeyMarker': '',
 'VersionIdMarker': '',
 'Versions': [{'ETag': '"669fdad9e309b552f1e9cf7b489c1f73-2"',
   'Size': 10485760,
   'StorageClass': 'STANDARD',
   'Key': '10mb.dat',
   'VersionId': 'fe14c26d-16ea-60bf-a754-06bdfcde1d5e',
   'IsLatest': True,
   'LastModified': datetime.datetime(2020, 12, 13, 22, 31, 14, 996000, tzinfo=tzutc()),
   'Owner': {'ID': '27b8e84694ca0b529d5379049564ebe1'}},
  {'ETag': '"669fdad9e309b552f1e9cf7b489c1f73-2"',
   'Size': 10485760,
   'StorageClass': 'STANDARD',
   'Key': '10mb.dat',
   'VersionId': 'fe14c26d-18b5-d1ef-a754-06bdfcde1d5e',
   'IsLatest': False,
   'LastModified': datetime.datetime(2020, 12, 13, 22, 31, 11, 985000, tzinfo=tzutc()),
   'Owner': {'ID': '27b8e84694ca0b529d5379049564ebe1'}},
  {'ETag': '"669fdad9e309b552f1e9cf7b489c1f73-2"',
   'Size': 10485760,
   'StorageClass': 'STANDARD',
   'Key': '10mb.dat',
   'VersionId': 'fe14c26d-1a6a-119f-a754-06bdfcde1d5e',
   'IsLatest': False,
   'LastModified': datetime.datetime(2020, 12, 13, 22, 31, 9, 126000, tzinfo=tzutc()),
   'Owner': {'ID': '27b8e84694ca0b529d5379049564ebe1'}},
  {'ETag': '"669fdad9e309b552f1e9cf7b489c1f73-2"',
   'Size': 10485760,
   'StorageClass': 'STANDARD',
   'Key': '10mb.dat',
   'VersionId': 'fe14c26d-1e9b-382f-a754-06bdfcde1d5e',
   'IsLatest': False,
   'LastModified': datetime.datetime(2020, 12, 13, 22, 31, 2, 93000, tzinfo=tzutc()),
   'Owner': {'ID': '27b8e84694ca0b529d5379049564ebe1'}}],
 'Name': 'pythonbucket2',
 'Prefix': '',
 'MaxKeys': 1000,
 'EncodingType': 'url'}

Since list_object_versions () returns so much information, the following examples will narrow down the output.

Narrow down to display information about versioned objects

In the following example, only the bucket name (Bucket ='pythonbucket2') is set in the argument of list_object_versions (), and the output is narrowed down to the key, version ID, and last modification date and time.

for version in client.list_object_versions(Bucket='pythonbucket2')['Versions']:
    print(version['Key'],version['VersionId'], version['LastModified'].strftime("%Y/%m/%d %H:%M:%S"))
10mb.dat fe14c26d-16ea-60bf-a754-06bdfcde1d5e 2020/12/13 22:31:14
10mb.dat fe14c26d-18b5-d1ef-a754-06bdfcde1d5e 2020/12/13 22:31:11
10mb.dat fe14c26d-1a6a-119f-a754-06bdfcde1d5e 2020/12/13 22:31:09
10mb.dat fe14c26d-1e9b-382f-a754-06bdfcde1d5e 2020/12/13 22:31:02 

3-7-4.png

5. Pause bucket versioning/put_bucket_versioning ()

Pass the bucket name to change the versioning state and the state to change (here ‘Suspended’) to the argument of put_bucket_versioning (), and execute it to suspend the versioning function.

In the following example, the versioning function of the bucket "python bucket 1" is suspended (VersioningConfiguration = {'Status':'Suspended'}).

client.put_bucket_versioning( Bucket='pythonbucket2', VersioningConfiguration={'Status': 'Suspended'} )
{'ResponseMetadata': {'RequestId': '9dad3e02-0e30-1dbc-a754-06bdfcde1d5e',
  'HostId': '',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'date': 'Sun, 13 Dec 2020 22:30:36 GMT',
   'x-amz-request-id': '9dad3e02-0e30-1dbc-a754-06bdfcde1d5e',
   'content-type': 'application/xml;charset=UTF-8',
   'content-length': '161',
   'server': 'CloudianS3'},
  'RetryAttempts': 0},
 'Status': 'Enabled'}

After executing the above put_bucket_versioning (), the status of the versioning function of the bucket "pythonbucket1" is displayed, and it is confirmed that the versioning function is paused.

client.get_bucket_versioning(Bucket='pythonbucket2')
{'ResponseMetadata': {'RequestId': '9dad3e44-0e30-1dbc-a754-06bdfcde1d5e',
  'HostId': '',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'date': 'Sun, 13 Dec 2020 22:36:23 GMT',
   'x-amz-request-id': '9dad3e44-0e30-1dbc-a754-06bdfcde1d5e',
   'content-type': 'application/xml;charset=UTF-8',
   'content-length': '163',
   'server': 'CloudianS3'},
  'RetryAttempts': 0},
 'Status': 'Suspended'}

3-7-5.png

Summary

I tried to set the versioning of the bucket with Python (boto3).

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

Recommended Posts

[Cloudian # 8] Try setting the bucket versioning with Python (boto3)
[Cloudian # 7] Try deleting the bucket in 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 # 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 # 1] Try to access object storage with AWS SDK for Python (boto3)
Try to solve the man-machine chart with Python
Manage AWS nicely with the Python library Boto
Try scraping with Python.
Try to solve the programming challenge book with python3
Try translating with Python while maintaining the PDF layout
Try to solve the internship assignment problem with Python
Try touching the micro: bit with VS Code + Python
Try Python output with Haxe 3.2
S3 operation with python boto3
Try running Python with Try Jupyter
Call the API with python3.
Try face recognition with Python
Try scraping the data of COVID-19 in Tokyo with Python
Try hitting the Twitter API quickly and easily with Python
Try Amazon Simple Workflow Service (SWF) with Python and boto3
Try to automate the operation of network devices with Python
Try to decipher the garbled attachment file name with Python
Try scraping with Python + Beautiful Soup
Extract the xz file with python
[Automation with python! ] Part 1: Setting file
Try the Python LINE Pay SDK
Try to operate Facebook with Python
Try singular value decomposition with Python
Get the weather with Python requests
Get the weather with Python requests 2
Find the Levenshtein Distance with python
Hit the Etherpad-lite API with Python
Install the Python plugin with Netbeans 8.0.2
Try face recognition with python + OpenCV
I liked the tweet with python. ..
Master the type with Python [Python 3.9 compatible]
Try using the Python Cmd module
Try frequency control simulation with Python
Try blurring the image with opencv2
Try to solve the shortest path with Python + NetworkX + social data
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Try to image the elevation data of the Geographical Survey Institute with Python
Make the Python console covered with UNKO
Try logging in to qiita with Python
Try using the Wunderlist API in Python
[Python] Set the graph range with matplotlib
Try mathematical formulas using Σ with python
WebUI test with Python2.6 + Selenium 2.44.0 --profile setting
Try using the Kraken API in Python
Behind the flyer: Using Docker with Python
Try working with binary data in Python
Try the Variational-Quantum-Eigensolver (VQE) algorithm with Blueqat
Check the existence of the file with python
Try using the camera with Python's OpenCV
[Python] Get the variable name with str
[Python] Round up with just the operator
Display Python 3 in the browser with MAMP
Search the maze with the python A * algorithm
Try using Python with Google Cloud Functions