Todos os produtos
Search
Central de documentação

Object Storage Service:Gerenciar metadados de objeto (Harmony SDK)

Última atualização: Jul 03, 2026

Este tópico descreve como configurar e consultar metadados de objeto usando o SDK do Object Storage Service (OSS) para Harmony.

Observações de uso

Configurar metadados de objeto ao fazer upload de um objeto

O exemplo de código a seguir demonstra como definir a lista de controle de acesso (ACL) do objeto como public-read durante o upload:

import Client, { EObjectAcl, RequestError } from '@aliyun/oss';

// Create an OSSClient instance.
const client = new Client({
  // Specify the AccessKey ID obtained from Security Token Service (STS).
  accessKeyId: 'yourAccessKeyId',
  // Specify the AccessKey secret obtained from STS.
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the security token obtained from STS.
  securityToken: 'yourSecurityToken',
  // 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: 'oss-cn-hangzhou',
});

const bucket = 'yourBucketName'; // Specify the name of the bucket.

const key='yourObjectName '; // Specify the name of the object.

const putObject = async () => {
  try {
    // Use the putObject method to upload data to object in the specified bucket and pass parameters.
    const res = await client.putObject({
      bucket, // Specify the name of the bucket.
      key, // Specify the name of the object.
      data: 'hello world' // Specify the data that you want to upload. In this example, a simple string is uploaded.
      objectAcl: EObjectAcl.PRIVATE
    });

    // Display the result of the object upload operation.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Capture exceptions during the request.
    if (err instanceof RequestError) {
      // If known types of errors exist, display information, such as the error code, error message, request ID, HTTP status code, and EC.
      console.log('code: ', err.code);
      console.log('message: ', err.message);
      console.log('requestId: ', err.requestId);
      console.log('status: ', err.status);
      console.log('ec: ', err.ec);
    } else {
      // Display other unknown types of errors.
      console.log('unknown error: ', err);
    }
  }
}

// Call the putObject function to perform the object upload operation.
putObject();

Consultar metadados de objeto

Usar o método HeadObject para recuperar todos os metadados de um objeto

O exemplo de código a seguir mostra como consultar todos os metadados de um objeto com o método HeadObject:

import Client, { RequestError } from '@aliyun/oss';

// Create an OSSClient instance.
const client = new Client({
  // Specify the AccessKey ID obtained from STS.
  accessKeyId: 'yourAccessKeyId',
  // Specify the AccessKey secret obtained from STS.
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the security token obtained from STS.
  securityToken: 'yourSecurityToken',
  // 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: 'oss-cn-hangzhou',
});

// Specify the name of the bucket.
const bucket = 'yourBucketName';
// Specify the name of the object.
const key = 'yourObjectName';

/**
 * Query the metadata of the object. 
 * Use the HeadObject method to query the metadata of the object without downloading the object. 
 */
const headObject = async () => {
  try {
    // Use the HeadObject method to query the metadata of the object.
    const res = await client.headObject({
      bucket, // Specify the name of the bucket.
      key, // Specify the name of the object.
    });

    // Display the object metadata.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Capture exceptions during the request.
    if (err instanceof RequestError) {
      // If known types of errors exist, display information, such as the error code, error message, request ID, HTTP status code, and EC.
      console.log('code: ', err.code); // The error code.
      console.log('message: ', err.message); // The error message.
      console.log('requestId: ', err.requestId); // The request ID.
      console.log('status: ', err.status); // The HTTP status code.
      console.log('ec: ', err.ec); // The EC.
    } else {
      // Display other unknown types of errors.
      console.log('unknown error: ', err);
    }
  }
};

// Call the headObject function to query object metadata.
headObject();

Usar GetObjectMeta para obter metadados parciais do objeto

Nota

Use o método GetObjectMeta para consultar apenas parte dos metadados do objeto, incluindo comprimento do conteúdo retornado (ContentLength), tag de entidade (ETag), hora da última modificação (LastModified), hora do último acesso (LastAccessTime), ID da versão (VersionId) e hash CRC-64 (HashCRC64).

O exemplo de código a seguir mostra como consultar metadados parciais de um objeto com o método GetObjectMeta:

import Client, { RequestError } from '@aliyun/oss';

// Create an OSSClient instance.
const client = new Client({
  // Specify the AccessKey ID obtained from STS.
  accessKeyId: 'yourAccessKeyId',
  // Specify the AccessKey secret obtained from STS.
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the security token obtained from STS.
  securityToken: 'yourSecurityToken',
  // 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: 'oss-cn-hangzhou',
});

// Specify the name of the bucket.
const bucket = 'yourBucketName';
// Specify the name of the object.
const key = 'yourObjectName';

/**
 * Query the metadata of the object. 
 * Use the getObjectMeta method to query the metadata of the object, including the HTTP headers. 
 */
const getObjectMeta = async () => {
  try {
    // Use the getObjectMeta method to query the metadata of the object.
    const res = await client.getObjectMeta({
      bucket, // Specify the name of the bucket.
      key, // Specify the name of the object.
    });

    // Display the object metadata.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Capture exceptions during the request.
    if (err instanceof RequestError) {
      // If known types of errors exist, display information, such as the error code, error message, request ID, HTTP status code, and EC.
      console.log('code: ', err.code); // The error code.
      console.log('message: ', err.message); // The error message.
      console.log('requestId: ', err.requestId); // The request ID.
      console.log('status: ', err.status); // The HTTP status code.
      console.log('ec: ', err.ec); // The EC.
    } else {
      // Display other unknown types of errors.
      console.log('unknown error: ', err);
    }
  }
};

// Call the getObjectMeta function to query object metadata.
getObjectMeta();