head_object
async
¶
head_object(
bucket: BucketName,
key: ObjectKey,
*,
config_overrides: Optional[AsyncS3ClientConfig] = None,
if_match: Optional[IfMatch] = None,
if_modified_since: Optional[IfModifiedSince] = None,
if_none_match: Optional[IfNoneMatch] = None,
if_unmodified_since: Optional[IfUnmodifiedSince] = None,
range: Optional[Range] = None,
response_cache_control: Optional[ResponseCacheControl] = None,
response_content_disposition: Optional[ResponseContentDisposition] = None,
response_content_encoding: Optional[ResponseContentEncoding] = None,
response_content_language: Optional[ResponseContentLanguage] = None,
response_content_type: Optional[ResponseContentType] = None,
response_expires: Optional[ResponseExpires] = None,
version_id: Optional[ObjectVersionId] = None,
sse_customer_algorithm: Optional[SSECustomerAlgorithm] = None,
sse_customer_key: Optional[SSECustomerKey] = None,
sse_customer_key_md5: Optional[SSECustomerKeyMD5] = None,
request_payer: Optional[RequestPayer] = None,
part_number: Optional[PartNumber] = None,
expected_bucket_owner: Optional[AccountId] = None,
checksum_mode: Optional[ChecksumMode] = None,
) -> HeadObjectOutput
The HEAD operation retrieves metadata from an object without returning the object itself. This operation is useful if you're interested only in an object's metadata.
A HEAD request has the same options as a GET operation on an object. The response is identical to the GET response except that there is no response body. Because of this, if the HEAD request generates an error, it returns a generic code, such as 400 Bad Request, 403 Forbidden, 404 Not Found, 405 Method Not Allowed, 412 Precondition Failed, or 304 Not Modified. It's not possible to retrieve the exact exception of these error codes.
Request headers are limited to 8 KB in size. For more information, see Common Request Headers.
- Permissions
-
-
General purpose bucket permissions - To use
HEAD, you must have thes3:GetObjectpermission. You need the relevant read object (or version) permission for this operation. For more information, see Actions, resources, and condition keys for Amazon S3 in the Amazon S3 User Guide. For more information about the permissions to S3 API operations by S3 resource types, see Required permissions for Amazon S3 API operations in the Amazon S3 User Guide.If the object you request doesn't exist, the error that Amazon S3 returns depends on whether you also have the
s3:ListBucketpermission.-
If you have the
s3:ListBucketpermission on the bucket, Amazon S3 returns an HTTP status code404 Not Founderror. -
If you don’t have the
s3:ListBucketpermission, Amazon S3 returns an HTTP status code403 Forbiddenerror.
-
-
Directory bucket permissions - To grant access to this API operation on a directory bucket, we recommend that you use the
CreateSessionAPI operation for session-based authorization. Specifically, you grant thes3express:CreateSessionpermission to the directory bucket in a bucket policy or an IAM identity-based policy. Then, you make theCreateSessionAPI call on the bucket to obtain a session token. With the session token in your request header, you can make API requests to this operation. After the session token expires, you make anotherCreateSessionAPI call to generate a new session token for use. Amazon Web Services CLI or SDKs create session and refresh the session token automatically to avoid service interruptions when a session expires. For more information about authorization, seeCreateSession.If you enable
x-amz-checksum-modein the request and the object is encrypted with Amazon Web Services Key Management Service (Amazon Web Services KMS), you must also have thekms:GenerateDataKeyandkms:Decryptpermissions in IAM identity-based policies and KMS key policies for the KMS key to retrieve the checksum of the object.
-
- Encryption
-
Encryption request headers, like
x-amz-server-side-encryption, should not be sent forHEADrequests if your object uses server-side encryption with Key Management Service (KMS) keys (SSE-KMS), dual-layer server-side encryption with Amazon Web Services KMS keys (DSSE-KMS), or server-side encryption with Amazon S3 managed encryption keys (SSE-S3). Thex-amz-server-side-encryptionheader is used when youPUTan object to S3 and want to specify the encryption method. If you include this header in aHEADrequest for an object that uses these types of keys, you’ll get an HTTP400 Bad Requesterror. It's because the encryption method can't be changed when you retrieve the object.If you encrypt an object by using server-side encryption with customer-provided encryption keys (SSE-C) when you store the object in Amazon S3, then when you retrieve the metadata from the object, you must use the following headers to provide the encryption key for the server to be able to retrieve the object's metadata. The headers are:
-
x-amz-server-side-encryption-customer-algorithm -
x-amz-server-side-encryption-customer-key -
x-amz-server-side-encryption-customer-key-MD5
For more information about SSE-C, see Server-Side Encryption (Using Customer-Provided Encryption Keys) in the Amazon S3 User Guide.
Directory bucket - For directory buckets, there are only two supported options for server-side encryption: SSE-S3 and SSE-KMS. SSE-C isn't supported. For more information, see Protecting data with server-side encryption in the Amazon S3 User Guide.
-
- Versioning
-
-
If the current version of the object is a delete marker, Amazon S3 behaves as if the object was deleted and includes
x-amz-delete-marker: truein the response. -
If the specified version is a delete marker, the response returns a
405 Method Not Allowederror and theLast-Modified: timestampresponse header.
-
Directory buckets - Delete marker is not supported for directory buckets.
-
Directory buckets - S3 Versioning isn't enabled and supported for directory buckets. For this API operation, only the
nullvalue of the version ID is supported by directory buckets. You can only specifynullto theversionIdquery parameter in the request.
-
- HTTP Host header syntax
-
Directory buckets - The HTTP Host header syntax is
Bucket-name.s3express-zone-id.region-code.amazonaws.com.For directory buckets, you must make requests for this API operation to the Zonal endpoint. These endpoints support virtual-hosted-style requests in the format
https://amzn-s3-demo-bucket.s3express-zone-id.region-code.amazonaws.com/key-name. Path-style requests are not supported. For more information about endpoints in Availability Zones, see Regional and Zonal endpoints for directory buckets in Availability Zones in the Amazon S3 User Guide. For more information about endpoints in Local Zones, see Concepts for directory buckets in Local Zones in the Amazon S3 User Guide.
The following actions are related to HeadObject:
You must URL encode any signed header values that contain spaces. For example, if your header value is my file.txt, containing two spaces after my, you must URL encode this value to my%20%20file.txt.
Parameters:
-
bucket(BucketName) –The name of the bucket that contains the object.
Directory buckets - When you use this operation with a directory bucket, you must use virtual-hosted-style requests in the format
Bucket-name.s3express-zone-id.region-code.amazonaws.com. Path-style requests are not supported. Directory bucket names must be unique in the chosen Zone (Availability Zone or Local Zone). Bucket names must follow the formatbucket-base-name--zone-id--x-s3(for example,amzn-s3-demo-bucket--usw2-az1--x-s3). For information about bucket naming restrictions, see Directory bucket naming rules in the Amazon S3 User Guide.Access points - When you use this action with an access point for general purpose buckets, you must provide the alias of the access point in place of the bucket name or specify the access point ARN. When you use this action with an access point for directory buckets, you must provide the access point name in place of the bucket name. When using the access point ARN, you must direct requests to the access point hostname. The access point hostname takes the form AccessPointName-AccountId.s3-accesspoint.Region.amazonaws.com. When using this action with an access point through the Amazon Web Services SDKs, you provide the access point ARN in place of the bucket name. For more information about access point ARNs, see Using access points in the Amazon S3 User Guide.
Object Lambda access points are not supported by directory buckets.
S3 on Outposts - When you use this action with S3 on Outposts, you must direct requests to the S3 on Outposts hostname. The S3 on Outposts hostname takes the form
AccessPointName-AccountId.outpostID.s3-outposts.Region.amazonaws.com. When you use this action with S3 on Outposts, the destination bucket must be the Outposts access point ARN or the access point alias. For more information about S3 on Outposts, see What is S3 on Outposts? in the Amazon S3 User Guide. -
if_match(Optional[IfMatch], default:None) –Return the object only if its entity tag (ETag) is the same as the one specified; otherwise, return a 412 (precondition failed) error.
If both of the
If-MatchandIf-Unmodified-Sinceheaders are present in the request as follows:-
If-Matchcondition evaluates totrue, and; -
If-Unmodified-Sincecondition evaluates tofalse;
Then Amazon S3 returns
200 OKand the data requested.For more information about conditional requests, see RFC 7232.
-
-
if_modified_since(Optional[IfModifiedSince], default:None) –Return the object only if it has been modified since the specified time; otherwise, return a 304 (not modified) error.
If both of the
If-None-MatchandIf-Modified-Sinceheaders are present in the request as follows:-
If-None-Matchcondition evaluates tofalse, and; -
If-Modified-Sincecondition evaluates totrue;
Then Amazon S3 returns the
304 Not Modifiedresponse code.For more information about conditional requests, see RFC 7232.
-
-
if_none_match(Optional[IfNoneMatch], default:None) –Return the object only if its entity tag (ETag) is different from the one specified; otherwise, return a 304 (not modified) error.
If both of the
If-None-MatchandIf-Modified-Sinceheaders are present in the request as follows:-
If-None-Matchcondition evaluates tofalse, and; -
If-Modified-Sincecondition evaluates totrue;
Then Amazon S3 returns the
304 Not Modifiedresponse code.For more information about conditional requests, see RFC 7232.
-
-
if_unmodified_since(Optional[IfUnmodifiedSince], default:None) –Return the object only if it has not been modified since the specified time; otherwise, return a 412 (precondition failed) error.
If both of the
If-MatchandIf-Unmodified-Sinceheaders are present in the request as follows:-
If-Matchcondition evaluates totrue, and; -
If-Unmodified-Sincecondition evaluates tofalse;
Then Amazon S3 returns
200 OKand the data requested.For more information about conditional requests, see RFC 7232.
-
-
key(ObjectKey) –The object key.
-
range(Optional[Range], default:None) –HeadObject returns only the metadata for an object. If the Range is satisfiable, only the
ContentLengthis affected in the response. If the Range is not satisfiable, S3 returns a416 - Requested Range Not Satisfiableerror. -
response_cache_control(Optional[ResponseCacheControl], default:None) –Sets the
Cache-Controlheader of the response. -
response_content_disposition(Optional[ResponseContentDisposition], default:None) –Sets the
Content-Dispositionheader of the response. -
response_content_encoding(Optional[ResponseContentEncoding], default:None) –Sets the
Content-Encodingheader of the response. -
response_content_language(Optional[ResponseContentLanguage], default:None) –Sets the
Content-Languageheader of the response. -
response_content_type(Optional[ResponseContentType], default:None) –Sets the
Content-Typeheader of the response. -
response_expires(Optional[ResponseExpires], default:None) –Sets the
Expiresheader of the response. -
version_id(Optional[ObjectVersionId], default:None) –Version ID used to reference a specific version of the object.
For directory buckets in this API operation, only the
nullvalue of the version ID is supported. -
sse_customer_algorithm(Optional[SSECustomerAlgorithm], default:None) –Specifies the algorithm to use when encrypting the object (for example, AES256).
This functionality is not supported for directory buckets.
-
sse_customer_key(Optional[SSECustomerKey], default:None) –Specifies the customer-provided encryption key for Amazon S3 to use in encrypting data. This value is used to store the object and then it is discarded; Amazon S3 does not store the encryption key. The key must be appropriate for use with the algorithm specified in the
x-amz-server-side-encryption-customer-algorithmheader.This functionality is not supported for directory buckets.
-
sse_customer_key_md5(Optional[SSECustomerKeyMD5], default:None) –Specifies the 128-bit MD5 digest of the encryption key according to RFC 1321. Amazon S3 uses this header for a message integrity check to ensure that the encryption key was transmitted without error.
This functionality is not supported for directory buckets.
-
part_number(Optional[PartNumber], default:None) –Part number of the object being read. This is a positive integer between 1 and 10,000. Effectively performs a 'ranged' HEAD request for the part specified. Useful querying about the size of the part and the number of parts in this object.
-
expected_bucket_owner(Optional[AccountId], default:None) –The account ID of the expected bucket owner. If the account ID that you provide does not match the actual owner of the bucket, the request fails with the HTTP status code
403 Forbidden(access denied). -
checksum_mode(Optional[ChecksumMode], default:None) –To retrieve the checksum, this parameter must be enabled.
General purpose buckets - If you enable checksum mode and the object is uploaded with a checksum and encrypted with an Key Management Service (KMS) key, you must have permission to use the
kms:Decryptaction to retrieve the checksum.Directory buckets - If you enable
ChecksumModeand the object is encrypted with Amazon Web Services Key Management Service (Amazon Web Services KMS), you must also have thekms:GenerateDataKeyandkms:Decryptpermissions in IAM identity-based policies and KMS key policies for the KMS key to retrieve the checksum of the object.
Raises:
-
NotFound–The specified content does not exist.
-
UnknownServiceError–The service returned an error code this client does not model.
Examples:
To retrieve metadata of an object without returning the object itself The following example retrieves an object metadata.
Source code in docs_src/capo_s3/_services/async_s3.py
5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 | |