All Products
Search
Document Center

Object Storage Service:Query object metadata by using OSS SDK for Node.js

Last Updated:Feb 01, 2024

By default, when you call the HeadObject operation on an Object Storage Service (OSS) object in a versioning-enabled bucket, the metadata of only the current version of the object is returned. You can query the metadata of a specific version of an object by specifying the version ID of the object.

Note

If the current version of the object is a delete marker, OSS returns 404 Not Found. If you specify a version ID in the request, OSS returns the metadata of the specified version of the object. For more information about how to query the version ID of an object, see List objects.

Examples

The following sample code provides an example on how to query the metadata of a specific version of the exampleobject.txt object in the exampledir directory of the examplebucket bucket:

const OSS = require('ali-oss');

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. Example: examplebucket. 
  bucket: 'examplebucket',
});

async function headInfo() {
  // Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
  const name = 'exampledir/exampleobject.txt'
  // Specify the version ID of the object to query the metadata of the specified version of the object. 
  const versionId = 'CAEQMxiBgICAof2D0BYiIDJhMGE3N2M1YTI1NDQzOGY5NTkyNTI3MGYyMzJm****'
  const options = {
    versionId
  };
  const result = await client.head(name, options);
  console.log(result);
}

headInfo();

References

For more information about the API operations that you can call to query the metadata of an object, see HeadObject and GetObjectMeta.