Todos los productos
Search
Centro de documentos

Object Storage Service:Eliminación de archivos (SDK de Browser.js)

Última actualización: Sep 24, 2026

Puede eliminar uno o varios archivos (objetos) que ya no necesite.

Aviso

Los objetos eliminados no se pueden recuperar. Proceda con precaución.

Notas de uso

  • Cuando utilice herramientas de empaquetado como Webpack y Browserify, instale el SDK de OSS para Browser.js mediante la ejecución del comando npm install ali-oss.

  • Si desea acceder a un bucket de OSS desde un navegador, pero no hay reglas de CORS configuradas para ese bucket, el navegador rechazará la solicitud. Por lo tanto, debe configurar las reglas de CORS en el bucket si necesita acceder a él desde un navegador. Para obtener más información, consulte Installation.

  • En la mayoría de los casos, el SDK de OSS para Browser.js se utiliza dentro de navegadores. Para evitar que su par de AccessKey quede expuesto, se recomienda usar credenciales de acceso temporales obtenidas de Security Token Service (STS) para acceder a OSS.

    Las credenciales de acceso temporales constan de un par de AccessKey y un token de seguridad. El par de AccessKey incluye un AccessKey ID y un AccessKey secret. Para obtener más información sobre cómo conseguir credenciales de acceso temporales, consulte Use STS for temporary access authorization.

Eliminación de un solo archivo

A continuación se muestra el código para eliminar un solo archivo:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <title>Document</title>
</head>

<body>
  <button id="delete">Delete</button> 
  <!--Import the SDK file.-->
  <script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.18.0.min.js"></script>
  <script type="text/javascript">
    const client = new OSS({
      // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
      region: 'yourRegion',
      authorizationV4: true,
      // The temporary AccessKey pair (AccessKey ID and AccessKey secret) obtained from STS.
      accessKeyId: 'yourAccessKeyId',
      accessKeySecret: 'yourAccessKeySecret',
      // The security token (SecurityToken) obtained from STS.
      stsToken: 'yourSecurityToken',
      // Specify the bucket name. For example, examplebucket.
      bucket: "examplebucket",
    });

    const deleteSingle = document.getElementById("delete");   

    // Delete a single file.
    deleteSingle.addEventListener("click", async () => {
      // Specify the name of the object to delete. The object name must be the full path of the object, not including the bucket name. For example, exampledir/exampleobject.txt.
      let result = await client.delete('exampledir/exampleobject.txt');
      console.log(result);
    });

  </script>
</body>

</html>

Eliminación de varios archivos

A continuación se muestra el código para eliminar varios archivos:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
  </head>

  <body>
    <button id="deleteAll">Delete multiple files</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 region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
        region: 'yourRegion',
        authorizationV4: true,
        // The temporary AccessKey pair (AccessKey ID and AccessKey secret) obtained from STS.
        accessKeyId: 'yourAccessKeyId',
        accessKeySecret: 'yourAccessKeySecret',
        // The security token (SecurityToken) obtained from STS.
        stsToken: 'yourSecurityToken',
        // Specify the bucket name. For example, examplebucket.
        bucket: "examplebucket",
      });

      const deleteAll = document.getElementById("deleteAll");

      // Delete multiple files.
      deleteAll.addEventListener("click", async () => {
        // Specify the names of the objects to delete. The object names must be the full paths of the objects, not including the bucket name.
        let result = await client.deleteMulti([
          "example.txt",
          "exampleobject.txt",
          "newexampleobject.txt",
        ]);
        console.log(result);

        result = await client.deleteMulti(
          ["example.txt", "exampleobject.txt", "newexampleobject.txt"],
          {
            // Use the quiet parameter to specify whether to return a list of all deleted files. If quiet is set to true, OSS does not return a message body. If quiet is set to false, OSS returns a list of all deleted files.
            quiet: true,
          }
        );
      });
    </script>
  </body>
</html>

Referencias

  • Eliminación de un solo archivo

    • Para consultar el código de ejemplo completo, visite el ejemplo en GitHub.

    • Para obtener más información sobre la operación de API, consulte DeleteObject.

  • Eliminación de varios archivos