Get file metadata (Node.js SDK)

Updated at:

By default, calling the HeadObject operation on a bucket with versioning enabled retrieves the metadata of only the current version of a file. To retrieve the metadata of a specific version, specify the file's version ID (versionId).

Note

If the current version of a file is a delete marker, a 404 Not Found error is returned. If you specify a versionId in the request, the metadata of the specified version is returned. To retrieve the versionId of a file, see List files (Node.js SDK).

Sample code

The following code retrieves the metadata for a specified version of the `exampleobject.txt` file from the `exampledir` folder in the `examplebucket` bucket.

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

const client = new OSS({
  // Set yourRegion to the region where your bucket is located. For example, for China (Hangzhou), set Region to oss-cn-hangzhou.
  region: 'yourRegion',
  // Get access credentials from environment variables. Before running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Enter the bucket name, for example, examplebucket.
  bucket: 'examplebucket',
});

async function headInfo() {
  // Enter the full path of the file, for example, exampledir/exampleobject.txt. The full path cannot include the bucket name.
  const name = 'exampledir/exampleobject.txt'
  // Enter the version ID of the file to get the metadata of the specified version.
  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 for retrieving file metadata, see HeadObject and GetObjectMeta.