[Cloudian # 9] Try to display the metadata of the object in Python (boto3)

Introduction

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

This time, I will use Python (boto3) to display the metadata of objects in the object storage.

Object metadata display / head_object ()

The metadata given to the object at the time of upload is displayed by head_object ().

1. Latest version of object metadata

In the following example, the metadata given to the latest version object of the key "10mb.dat" stored in the bucket "pythonbucket2" is displayed.

test1.py


import boto3

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


#Bucket name:pythonbucket2fix object metadata display
ret = client.head_object(Bucket='pythonbucket2fix', Key='10mb.dat')

print(ret)
{'ResponseMetadata': {'RequestId': '9dad3fc3-0e30-1dbc-a754-06bdfcde1d5e',
  'HostId': '',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'date': 'Sun, 13 Dec 2020 22:43:10 GMT',
   'x-amz-request-id': '9dad3fc3-0e30-1dbc-a754-06bdfcde1d5e',
   'last-modified': 'Sun, 13 Dec 2020 22:40:57 GMT',
   'etag': '"669fdad9e309b552f1e9cf7b489c1f73-2"',
   'content-type': 'binary/octet-stream',
   'x-amz-server-side-encryption': 'AES256',
   'x-amz-version-id': 'fe14c26b-bba0-6edf-a754-06bdfcde1d5e',
   'accept-ranges': 'bytes',
   'content-length': '10485760',
   'server': 'CloudianS3'},
  'RetryAttempts': 0},
 'AcceptRanges': 'bytes',
 'LastModified': datetime.datetime(2020, 12, 13, 22, 40, 57, tzinfo=tzutc()),
 'ContentLength': 10485760,
 'ETag': '"669fdad9e309b552f1e9cf7b489c1f73-2"',
 'VersionId': 'fe14c26b-bba0-6edf-a754-06bdfcde1d5e',
 'ContentType': 'binary/octet-stream',
 'ServerSideEncryption': 'AES256',
 'Metadata': {}}

If the versioning function of the stored bucket is enabled and head_object () is executed without specifying the version ID, the metadata given to the latest version of the object stored in that bucket. Returns.

2. Metadata for past versions of the object

In the following example, it is assigned to the object with version ID "fe14c26b-bed0-c73f-a754-06bdfcde1d5e" of the key "10mb.dat" stored in the bucket "pythonbucket2fix" with the versioning function enabled. I'm displaying the metadata.

test1.py


import boto3

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


#Bucket name:pythonbucket2fix object metadata display
ret = client.head_object(Bucket='pythonbucket2fix', Key='10mb.dat', VersionId='fe14c26b-bed0-c73f-a754-06bdfcde1d5e')

print(ret)
{'ResponseMetadata': {'RequestId': '9dad3fdf-0e30-1dbc-a754-06bdfcde1d5e',
  'HostId': '',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'date': 'Sun, 13 Dec 2020 22:43:41 GMT',
   'x-amz-request-id': '9dad3fdf-0e30-1dbc-a754-06bdfcde1d5e',
   'x-amz-meta-engineer': 'yamahiro',
   'x-amz-meta-company': 'nw',
   'x-amz-meta-purpose': 'boto3 demo 1',
   'last-modified': 'Sun, 13 Dec 2020 22:40:52 GMT',
   'etag': '"669fdad9e309b552f1e9cf7b489c1f73-2"',
   'content-type': 'binary/octet-stream',
   'x-amz-server-side-encryption': 'AES256',
   'x-amz-version-id': 'fe14c26b-bed0-c73f-a754-06bdfcde1d5e',
   'accept-ranges': 'bytes',
   'content-length': '10485760',
   'server': 'CloudianS3'},
  'RetryAttempts': 0},
 'AcceptRanges': 'bytes',
 'LastModified': datetime.datetime(2020, 12, 13, 22, 40, 52, tzinfo=tzutc()),
 'ContentLength': 10485760,
 'ETag': '"669fdad9e309b552f1e9cf7b489c1f73-2"',
 'VersionId': 'fe14c26b-bed0-c73f-a754-06bdfcde1d5e',
 'ContentType': 'binary/octet-stream',
 'ServerSideEncryption': 'AES256',
 'Metadata': {'Engineer': 'yamahiro',
  'Company': 'nw',
  'Purpose': 'boto3 demo 1'}}

Note: Select and display only metadata

The following example displays only the object's metadata by specifying the version ID.

print("~Select and display only metadata~")
ret = client.head_object(Bucket='pythonbucket2fix',
                Key='10mb.dat',
                VersionId='fe14c26b-bba0-6edf-a754-06bdfcde1d5e'
            )['Metadata']
print(ret)
ret = client.head_object(Bucket='pythonbucket2fix',
                Key='10mb.dat',
                VersionId='fe14c26b-bd5e-4b7f-a754-06bdfcde1d5e'
            )['Metadata']
print(ret)
ret = client.head_object(Bucket='pythonbucket2fix',
                Key='10mb.dat',
                VersionId='fe14c26b-bed0-c73f-a754-06bdfcde1d5e'
            )['Metadata']
print(ret)
ret = client.head_object(Bucket='pythonbucket2fix',
                Key='10mb.dat',
                VersionId='fe14c26b-c075-286f-a754-06bdfcde1d5e'
            )['Metadata']
print(ret)
~Select and display only metadata~
{}
{'Engineer': 'miki', 'Company': 'nw', 'Purpose': 'boto3 demo 2'}
{'Engineer': 'yamahiro', 'Company': 'nw', 'Purpose': 'boto3 demo 1'}
{}

Note: Difference between get_object and head_object

You can get the same information with the "get_object" method as shown below, but "head_object" can read only the metadata without returning the object itself.

ret = client.get_object(Bucket='pythonbucket2fix',
                Key='10mb.dat',
                VersionId='fe14c26b-bed0-c73f-a754-06bdfcde1d5e'
            )

print(ret)
{'ResponseMetadata': {'RequestId': '9dad3fed-0e30-1dbc-a754-06bdfcde1d5e', 'HostId': '', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sun, 13 Dec 2020 22:45:43 GMT', 'x-amz-request-id': '9dad3fed-0e30-1dbc-a754-06bdfcde1d5e', 'x-amz-version-id': 'fe14c26b-bed0-c73f-a754-06bdfcde1d5e', 'accept-ranges': 'bytes', 'etag': '"669fdad9e309b552f1e9cf7b489c1f73-2"', 'content-type': 'binary/octet-stream', 'x-amz-meta-engineer': 'yamahiro', 'x-amz-meta-company': 'nw', 'x-amz-meta-purpose': 'boto3 demo 1', 'x-amz-server-side-encryption': 'AES256', 'last-modified': 'Sun, 13 Dec 2020 22:40:52 GMT', 'content-length': '10485760', 'server': 'CloudianS3'}, 'RetryAttempts': 0}, 'AcceptRanges': 'bytes', 'LastModified': datetime.datetime(2020, 12, 13, 22, 40, 52, tzinfo=tzutc()), 'ContentLength': 10485760, 'ETag': '"669fdad9e309b552f1e9cf7b489c1f73-2"', 'VersionId': 'fe14c26b-bed0-c73f-a754-06bdfcde1d5e', 'ContentType': 'binary/octet-stream', 'ServerSideEncryption': 'AES256', 'Metadata': {'Engineer': 'yamahiro', 'Company': 'nw', 'Purpose': 'boto3 demo 1'}, 'Body': <botocore.response.StreamingBody object at 0x11b134588>}

If you want to read only the metadata, it is recommended to use "head_object".

ret = client.get_object(Bucket='pythonbucket2fix',
                Key='10mb.dat',
                VersionId='fe14c26b-bed0-c73f-a754-06bdfcde1d5e'
            )['Metadata']

print(ret)
{'Engineer': 'yamahiro', 'Company': 'nw', 'Purpose': 'boto3 demo 1'}

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 # 9] Try to display the metadata of the object in Python (boto3)
[Cloudian # 2] Try to display the object storage bucket in Python (boto3)
[Cloudian # 6] Try deleting the object stored in the bucket with Python (boto3)
[Cloudian # 7] Try deleting the bucket in Python (boto3)
[Cloudian # 5] Try to list the objects stored in the bucket with Python (boto3)
[Cloudian # 10] Try to generate a signed URL for object publishing in Python (boto3)
How to know the internal structure of an object in Python
I want to display the progress in Python!
[Cloudian # 3] Try to create a new object storage bucket with Python (boto3)
[Cloudian # 1] Try to access object storage with AWS SDK for Python (boto3)
How to get the number of digits in Python
[Cloudian # 8] Try setting the bucket versioning with Python (boto3)
Try to display the Fibonacci sequence in various languages in the name of algorithm practice
Try to display the railway data of national land numerical information in 3D
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Python OpenCV tried to display the image in text.
[Python] Change the Cache-Control of the object uploaded to Cloud Storage
Try scraping the data of COVID-19 in Tokyo with Python
Try to get the function list of Python> os package
Make the display of Python module exceptions easier to understand
Try to automate the operation of network devices with Python
In the python command python points to python3.8
Waveform display of audio in Python
Try to calculate Trace in Python
The meaning of ".object" in Django
Cython to try in the shortest
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
How to determine the existence of a selenium element in Python
Try to get a list of breaking news threads in Python.
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
[Python] Try to graph from the image of Ring Fit [OCR]
Try transcribing the probability mass function of the binomial distribution in Python
How to check the memory size of a variable in Python
First python ② Try to write code while examining the features of python
Output the contents of ~ .xlsx in the folder to HTML with Python
Feel free to change the label of the legend in Seaborn in python
I wrote the code to write the code of Brainf * ck in python
How to check the memory size of a dictionary in Python
Try to model the cumulative return of rollovers in futures trading
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
Check the behavior of destructor in Python
Try using the Kraken API in Python
Display a list of alphabets in Python 3
Display Python 3 in the browser with MAMP
[Python3] Rewrite the code object of the function
The result of installing python in Anaconda
How to display multiplication table in python
[Python] Try pydash of the Python version of lodash
The basics of running NoxPlayer in Python
Python amateurs try to summarize the list ①
In search of the fastest FizzBuzz in Python
Try hitting the YouTube API in Python
How to display Hello world in python
[Python] How to specify the window display position and size of matplotlib
Python Note: When you want to know the attributes of an object
Try to image the elevation data of the Geographical Survey Institute with Python
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
[Introduction to Python] Thorough explanation of the character string type used in Python!