Este tópico descreve como configurar e consultar metadados de objeto usando o SDK do Object Storage Service (OSS) para Harmony.
Notas de uso
Para obter mais informações sobre regiões e endpoints, consulte Regiões e endpoints.
Para definir metadados de objeto, é necessária a permissão
oss:PutObject. Para consultar metadados de objeto, é necessária a permissãooss:GetObject. Para mais informações, consulte Conceder permissões personalizadas a um usuário do RAM.
Configure 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 leitura pública 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();
Consulta de metadados de objeto
Use o método HeadObject para recuperar todos os metadados de um objeto
O exemplo de código a seguir ilustra 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();
Use o GetObjectMeta para obter metadados parciais do objeto
Use o método GetObjectMeta para consultar apenas metadados parciais do objeto, incluindo o comprimento do conteúdo retornado (ContentLength), a tag de entidade (ETag), a hora da última modificação (LastModified), a hora do último acesso (LastAccessTime), o ID da versão (VersionId) e o hash CRC-64 (HashCRC64).
O exemplo de código a seguir apresenta 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();