Checking whether an object exists

Last updated: 2023-09-13 10:34:59

Feature Overview

This document provides an overview of the API and sample code for quickly checking whether an object exists in a bucket. The sample code actually calls the HEAD Object COS API and is a simplified version of the API.
In addition to checking whether an object exists, the primary function of HEAD Object is to return object metadata. To view the SDK interface with the complete functionality of HEAD Object, please refer to Query Object Metadata.
API
Operation
Description
Querying object metadata
Queries the metadata of an object

Note

To check whether an object exists in a bucket, you can implement this by encapsulating the cos.headObject method.

Sample Code


function doesObjectExist() {
cos.headObject({
Bucket: 'examplebucket-1250000000', /* Enter your own bucket, required field */
Region: 'COS_REGION', /* The region where the bucket is located, such as ap-beijing, required field */
Key: '1.jpg', /* The object key stored in the bucket (e.g., 1.jpg, a/b/test.txt), required field */
}, function(err, data) {
if (data) {
console.log('Object exists');
} else if (err.statusCode == 404) {
console.log('Object does not exist');
} else if (err.statusCode == 403) {
console.log('No read permission for this object');
}
});
}

Description

Parameter
ParameterDescription
Local Disk Types
Required
Bucket
Bucket name in the format of BucketName-APPID.
String
Required
Region
Bucket region. For the enumerated values, see Regions and Access Endpoints.
String
Required
Key
Object key (object name), the unique identifier of an object in a bucket. For more information, see Object Overview.
String
Required
IfModifiedSince
Returns object metadata if the object is modified after the specified time; otherwise, 304 is returned
String
Not required

Callback function description

function(err, data) { ... }
Parameter
ParameterDescription
Local Disk Types
err
Error code, which is returned when an error (network error or service error) occurs. If the request is successful, this parameter is empty. For more information, see Error Codes.
Object
- statusCode
HTTP status code, such as 200, 403, and 404
Number
- headers
Returned headers
Object
data
Object returned when the request is successful. If the request fails, this parameter is empty.
Object
- statusCode
HTTP status code returned by the request, such as 200 and 304. If no modification is made after the specified time, 304 will be returned.
Number
- headers
Returned headers
Object
- x-cos-object-type
Indicates whether an object is appendable. Enumerated values: normal, appendable. The default value normal is not displayed if returned
String
- x-cos-storage-class
The storage class of the object, with enumeration values: STANDARD, STANDARD_IA, ARCHIVE, DEEP_ARCHIVE, etc. The default value is STANDARD and is not displayed in the response. For more information on storage classes, see Storage Class Overview.
String
- x-cos-meta-*
User-defined metadata
String
- NotModified
Indicates whether an object is unmodified after the specified time.
Boolean
- ETag
Returns the MD5 checksum value of the file. The ETag value can be used to verify whether the object has been damaged during the upload process.
For example, "09cba091df696af91549de27b8e7d0f6", Note: The ETag value here is enclosed in double quotes
String
- VersionId
Returns the version ID for versioning-enabled buckets. For buckets which have never had versioning enabled, this parameter is not returned
String