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.
The metadata given to the object at the time of upload is displayed by head_object ().
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.
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'}}
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'}
{}
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'}
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