Puede configurar OSS para que envíe un callback a su servidor de aplicaciones después de cargar un archivo (objeto). Para ello, incluya los parámetros del callback en la solicitud de carga.
Puede configurar opciones de callback como callbackSNI. Para obtener más información, consulte Callback.
Notas de uso
Si utiliza herramientas de empaquetado como Webpack o Browserify, instale el SDK de OSS para Browser.js mediante el comando npm install ali-oss.
Si desea acceder a un bucket de OSS desde un navegador y no hay reglas de CORS configuradas, el navegador rechazará la solicitud. Por lo tanto, configure las reglas de CORS en el bucket para permitir el acceso desde el navegador. Para obtener más información, consulte Installation.
-
El SDK de OSS para Browser.js se utiliza principalmente en navegadores. Para evitar que su par de AccessKey quede expuesto, use credenciales de acceso temporal obtenidas mediante Security Token Service (STS) para acceder a OSS.
Las credenciales de acceso temporal incluyen un par de AccessKey y un token de seguridad. El par de AccessKey consta de un AccessKey ID y un AccessKey secret. Para obtener más información sobre cómo obtener estas credenciales, consulte Use STS for temporary access authorization.
Código de ejemplo
A continuación se muestra un ejemplo de código para implementar un callback de carga:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<button id="submit">Upload Callback</button>
<!--Import the SDK file-->
<script
type="text/javascript"
src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.16.0.min.js"
></script>
<script type="text/javascript">
const client = new OSS({
// Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set yourRegion to oss-cn-hangzhou.
region: "yourRegion",
authorizationV4: true,
// The temporary AccessKey pair (AccessKey ID and AccessKey secret) obtained from the Security Token Service (STS).
accessKeyId: "yourAccessKeyId",
accessKeySecret: "yourAccessKeySecret",
// The security token (SecurityToken) obtained from the STS service.
stsToken: "yourSecurityToken",
// Specify the bucket name. For example, examplebucket.
bucket: "examplebucket",
});
const options = {
callback: {
// The server address for the callback request. The address must be an Internet URL.
url: "http://examplebucket.aliyuncs.com:23450",
// The value of Host in the callback request header. This is the Host value configured on your server.
// host: 'yourHost',
// The value of the request body for the callback.
body: "bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&mimeType=${mimeType}&imageInfo.height=${imageInfo.height}&imageInfo.width=${imageInfo.width}&imageInfo.format=${imageInfo.format}&var1=${x:var1}&var2=${x:var2}",
// The Content-Type of the callback request.
// contentType: 'application/x-www-form-urlencoded',
// Specifies whether OSS sends a Server Name Indication (SNI) to the origin URL specified by callbackUrl when the client initiates a callback request.
callbackSNI: true,
// The custom parameters for the callback request.
customValue: {
var1: "value1",
var2: "value2",
},
},
};
// Get the DOM.
const submit = document.getElementById("submit");
// Upload callback.
submit.addEventListener("click", () => {
client
.put("example.txt", new Blob(["Hello World"]), options)
.then((r) => console.log(r));
});
</script>
</body>
</html>
Referencias
Para consultar la operación de API del callback de carga, revise Callback.