Cuando se carga un objeto, Object Storage Service (OSS) puede iniciar un proceso de devolución de llamada para el servidor de aplicaciones. Para configurar las devoluciones de llamada de carga, solo necesita agregar los parámetros de devolución de llamada obligatorios a la solicitud de carga que se envía a OSS.
El envío de callbackSNI por parte de OSS depende de cómo se construyan los parámetros de devolución de llamada. Para obtener más información, consulte Callback.
Nota de uso
Antes de configurar una devolución de llamada de carga, familiarícese con esta función. Para obtener más información, consulte Upload callback.
Ejemplos
A continuación se muestra un ejemplo de código para configurar devoluciones de llamada de carga. En este caso, se carga un archivo local llamado examplefile.txt a un bucket denominado examplebucket. Una vez que termina la carga del archivo local, el nombre del objeto en OSS es exampleobject.txt.
const OSS = require("ali-oss");
var path = require("path");
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,
authorizationV4: true,
// Specify the name of the bucket.
bucket: "examplebucket",
});
const options = {
callback: {
// Specify the address of the callback server that receives the callback request. Example: http://oss-demo.aliyuncs.com:23450.
url: "http://oss-demo.aliyuncs.com:23450",
// (Optional) Specify the Host field included in the callback request header.
//host: 'yourCallbackHost',
// Specify the body of the callback request.
body: "bucket=${bucket}&object=${object}&var1=${x:var1}&var2=${x:var2}",
// Specify Content-Type in the callback request.
contentType: "application/x-www-form-urlencoded",
// Specifies whether OSS sends Server Name Indication (SNI) to the origin address specified by callbackUrl when a callback request is initiated from the client.
callbackSNI: true,
// Configure custom parameters for the callback request.
customValue: {
var1: "value1",
var2: "value2",
},
},
};
async function put() {
try {
// Specify the full paths of the object and the local file. Do not include the bucket name in the full path of the object.
// By default, if you do not specify the path of the local file, the file is uploaded from the local path of the project to which the sample program belongs.
let result = await client.put(
"exampleobject.txt",
path.normalize("/localpath/examplefile.txt"),
options
);
console.log(result);
} catch (e) {
console.log(e);
}
}
put();
Referencias
Consulte el código de ejemplo completo para configurar devoluciones de llamada de carga en GitHub.
Para obtener más información sobre la operación de API que permite configurar devoluciones de llamada de carga, consulte Callback.
Si desea conocer las causas y soluciones de los errores en las devoluciones de llamada de carga, consulte Fault 203.
Para más detalles sobre la configuración de parámetros y las notas de uso de la función de devolución de llamada, consulte Callback.