Query object metadata using OSS SDK for Python 2.0
This topic describes how to query metadata of objects in a versioning-enabled bucket.
Notes
-
The sample code in this topic uses the region ID
cn-hangzhouof the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you want to access resources in the bucket by using other Alibaba Cloud services in the same region in which the bucket is located, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see OSS regions and endpoints. -
To query object metadata, you must have the
oss:GetObjectpermission. For more information, see Grant custom policy to RAM users.
Sample code
Query all metadata of an object by using the HeadObject method
You can use the following code to query all metadata of a specific version of the object by using the HeadObject method.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command line parameter parser and describe the purpose of the script. The example describes how to query the headers of an object.
parser = argparse.ArgumentParser(description="head object sample")
# Specify the --region parameter to indicate the region in which the bucket is located. This parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the --bucket parameter, which specifies the name of the bucket. This command line parameter is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the --endpoint parameter to indicate the endpoint of the region in which the bucket is located. This parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Specify the --key parameter, which specifies the name of the object. This command line parameter is required.
parser.add_argument('--key', help='The name of the object.', required=True)
# Specify the --source_version_id parameter, which specifies the version ID of the source object. This command line parameter is required.
parser.add_argument('--version_id', help='The version ID of the object.', required=True)
def main():
# Parse the command-line arguments.
args = parser.parse_args()
# Obtain access credentials from environment variables for authentication.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configuration to create a cfg object and specify the credential provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Set the region attribute of the cfg object to the region provided in the command line.
cfg.region = args.region
# If a custom endpoint is provided, update the endpoint attribute of the cfg object with the provided endpoint.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Use the preceding settings to initialize the OSSClient instance.
client = oss.Client(cfg)
# Send a request to query the headers of the object.
result = client.head_object(oss.HeadObjectRequest(
bucket=args.bucket, # Specify the name of the bucket
key=args.key, # Specify the name of the object
version_id=args.version_id, # Specify the version ID of the object
))
# Display various metadata information of the operation results.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' content length: {result.content_length},'
f' content type: {result.content_type},'
f' etag: {result.etag},'
f' last modified: {result.last_modified},'
f' content md5: {result.content_md5},'
f' cache control: {result.cache_control},'
f' content disposition: {result.content_disposition},'
f' content encoding: {result.content_encoding},'
f' expires: {result.expires},'
f' hash crc64: {result.hash_crc64},'
f' storage class: {result.storage_class},'
f' object type: {result.object_type},'
f' version id: {result.version_id},'
f' tagging count: {result.tagging_count},'
f' server side encryption: {result.server_side_encryption},'
f' server side data encryption: {result.server_side_data_encryption},'
f' server side encryption key id: {result.server_side_encryption_key_id},'
f' next append position: {result.next_append_position},'
f' expiration: {result.expiration},'
f' restore: {result.restore},'
f' process status: {result.process_status},'
f' request charged: {result.request_charged},'
f' allow origin: {result.allow_origin},'
f' allow methods: {result.allow_methods},'
f' allow age: {result.allow_age},'
f' allow headers: {result.allow_headers},'
f' expose headers: {result.expose_headers},'
)
# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
main() # Specify the entry point of the script. The control flow starts here.
Use the GetObjectMeta method to query the partial metadata of an object
You can use the GetObjectMeta method to query only partial object metadata, including the returned content length (ContentLength), entity tag (ETag), last modified time (LastModified), last access time (LastAccessTime), version ID (VersionId), and CRC-64 hash (HashCRC64).
You can use the following code to query partial metadata for a specific object version with the GetObjectMeta method.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command line parameter parser and describe the purpose of the script. The example describes how to query the metadata of an object.
parser = argparse.ArgumentParser(description="get object meta sample")
# Specify the --region parameter to indicate the region in which the bucket is located. This parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the --bucket parameter, which specifies the name of the bucket. This command line parameter is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the --endpoint parameter to indicate the endpoint of the region in which the bucket is located. This parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Specify the --key parameter, which specifies the name of the object. This command line parameter is required.
parser.add_argument('--key', help='The name of the object.', required=True)
# Specify the --source_version_id parameter, which specifies the version ID of the source object. This command line parameter is required.
parser.add_argument('--version_id', help='The version id of the object.', required=True)
def main():
# Parse the command-line arguments.
args = parser.parse_args()
# Obtain access credentials from environment variables for authentication.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configuration to create a cfg object and specify the credential provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Set the region attribute of the cfg object to the region provided in the command line.
cfg.region = args.region
# If a custom endpoint is provided, update the endpoint attribute of the cfg object with the provided endpoint.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Use the preceding settings to initialize the OSSClient instance.
client = oss.Client(cfg)
# Send a request to query the metadata of the object.
result = client.get_object_meta(oss.GetObjectMetaRequest(
bucket=args.bucket, # Specify the name of the bucket
key=args.key, # Specify the name of the object
version_id=args.version_id, # Specify the version ID of the object
))
# Display the status code, request ID, content length, ETag, last modified time, last access time, version ID, and hash CRC64 of the operation result to confirm the request status.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' content length: {result.content_length},'
f' etag: {result.etag},'
f' last modified: {result.last_modified},'
f' last access time: {result.last_access_time},'
f' version id: {result.version_id},'
f' hash crc64: {result.hash_crc64},'
)
# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
main() # Specify the entry point of the script. The control flow starts here.