Feature Overview
This document provides an overview of APIs and SDK code samples for querying object metadata.
API | Operation | Description |
Querying object metadata | Queries the metadata of an object |
Querying object metadata
Note
This API is used to query object metadata.
Method prototype
head_object(Bucket, Key, **kwargs)
Sample Request
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport logging# Under normal circumstances, use the INFO log level. To locate issues, change it to DEBUG, and the SDK will print communication information with the server.logging.basicConfig(level=logging.INFO, stream=sys.stdout)# 1. Set user properties, including secret_id, secret_key, region, etc. Appid has been removed from CosConfig, please include Appid in the Bucket parameter. Bucket is composed of BucketName-Appid.secret_id = os.environ['COS_SECRET_ID'] # User <1>SecretId</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.secret_key = os.environ['COS_SECRET_KEY'] # User <1>SecretKey</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket.For a list of all regions supported by COS, visit https://cloud.tencent.com/document/product/436/6224token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, see https://cloud.tencent.com/document/product/436/14048.scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This is optional and is https by default.config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)client = CosS3Client(config)response = client.head_object(Bucket='examplebucket-1250000000',Key='exampleobject')print(response)
Sample request with all parameters
response = client.head_object(Bucket='examplebucket-1250000000',Key='exampleobject',VersionId='string',IfModifiedSince='Wed, 28 Oct 2020 20:30:00 GMT',)
Description
Parameter name | ParameterDescription | Local Disk Types | Required |
Bucket | Bucket name in the format of BucketName-APPID | String | Required |
Key | ObjectKey is the unique identifier of the object in the bucket. For example, in the object's access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, the ObjectKey is doc/pic.jpg | String | Required |
VersionId | Version ID of the object if versioning is enabled | String | Not required |
IfModifiedSince | Returns the object metadata only if the object is modified after the time specified in GMT format | String | Not required |
Response description
The response contains object metadata in dict format:
{'ETag': '"9a4802d5c99dafe1c04da0a8e7e166bf"','Last-Modified': 'Wed, 28 Oct 2014 20:30:00 GMT','Cache-Control': 'max-age=1000000','Content-Type': 'application/octet-stream','Content-Disposition': 'attachment; filename="filename.jpg"','Content-Encoding': 'gzip','Content-Language': 'zh-cn','Content-Length': '16807','Expires': 'Wed, 28 Oct 2019 20:30:00 GMT','Connection': 'keep-alive','Accept-Ranges': 'bytes','Server': 'tencent-cos','Date': 'Wed, 28 Oct 2019 20:20:00 GMT','x-cos-meta-test': 'test','x-cos-version-id': 'MTg0NDUxODMzMTMwMDM2Njc1ODA','x-cos-hash-crc64ecma': '1220413902487941276','x-cos-request-id': 'NTg3NzQ3ZmVfYmRjMzVfMzE5N182NzczMQ=='}
Parameter name | ParameterDescription | Local Disk Types |
ETag | For multipart uploads, this value does not represent the MD5 checksum of the object content, but is used only to verify the uniqueness of the object. | String |
Last-Modified | Time the object was last modified | String |
Cache-Control | Cache policy. Standard HTTP header | String |
Content-Type | Content type. Standard HTTP header | String |
Content-Disposition | File name. Standard HTTP header | String |
Content-Encoding | Encoding format. Standard HTTP header | String |
Content-Language | Language type. HTTP standard header | String |
Content-Length | Object size | String |
Expires | Cache expiration time. HTTP standard header | String |
Connection | Connection method. Standard HTTP header | String |
Accept-Ranges | Range request flag. Standard HTTP header | String |
Server | Server flag. Standard HTTP header | String |
Date | Request date. Standard HTTP header | String |
x-cos-meta-* | User-defined object metadata. It must start with x-cos-meta; otherwise, it will be ignored | String |
x-cos-hash-crc64ecma | CRC64 checksum of the file | String |
x-cos-version-id | Version ID of the object if versioning is enabled | String |
x-cos-request-id | Request ID | String |