Os objetos no Object Storage Service (OSS) contêm uma chave, dados e metadados do objeto. Os metadados do objeto descrevem as propriedades de um objeto e incluem cabeçalhos HTTP padrão, além de metadados definidos pelo usuário. Defina cabeçalhos HTTP padrão para personalizar políticas de solicitação HTTP, como a política de cache do objeto ou uma política de download forçado. Defina também metadados personalizados para identificar a finalidade ou as propriedades de um objeto.
Especifique metadados de objetos durante um upload
Ao usar a operação put, putStream ou multipartUpload, especifique o parâmetro meta e defina os metadados do objeto:
const OSS = require('ali-oss');
const path = require('path');
const client = new OSS({
// Obtain access credentials from environment variables. Before you run this code, make sure that 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,
// Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
// Use Signature V4.
authorizationV4: true,
// Set yourBucketName to the name of the bucket.
bucket: 'yourBucketName',
// Set yourEndpoint to the Internet endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
});
async function put() {
try {
// Set metadata as the meta property of the options object.
let result = await client.put('object-name', path.normalize('D:\\localpath\\examplefile.txt'), {
meta: {
year: '2025', // The metadata value must be a string.
people: 'mary'
}
});
console.log(result);
} catch (e) {
console.log(e);
}
}
put();
Modifique os metadados de um objeto existente
Além disso, atualize os metadados do objeto usando a operação putMeta:
const OSS = require('ali-oss');
const client = new OSS({
// Obtain access credentials from environment variables. Before you run this code, make sure that 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,
// Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
// Use Signature V4.
authorizationV4: true,
// Set yourBucketName to the name of the bucket.
bucket: 'yourBucketName',
// Set yourEndpoint to the Internet endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
});
async function putMeta() {
try {
let meta = { year: 2025, people: 'jack' };
let result = await client.putMeta('object-name', meta);
console.log(result);
} catch (e) {
console.log(e);
}
}
putMeta();
Obtenha os metadados do objeto
Use a operação head para recuperar os metadados do objeto.
const OSS = require('ali-oss');
const client = new OSS({
// Obtain access credentials from environment variables. Before you run this code, make sure that 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,
// Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
// Use Signature V4.
authorizationV4: true,
// Set yourBucketName to the name of the bucket.
bucket: 'yourBucketName',
// Set yourEndpoint to the Internet endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
});
async function headInfo() {
// Specify the full path of the object, for example, exampledir/exampleobject.txt. The full path cannot contain the bucket name.
const result = await client.head("exampledir/exampleobject.txt");
console.log(result);
}
headInfo();