Exclua um ou mais arquivos (Objects) que não são mais necessários.
Objects excluídos não podem ser recuperados. Prossiga com cautela.
Notas de uso
Ao usar ferramentas de empacotamento como Webpack e Browserify, instale o OSS SDK for Browser.js executando o comando npm install ali-oss.
Se você acessar um bucket do OSS pelo navegador sem que regras de CORS estejam configuradas, o navegador rejeitará a requisição. Por isso, configure as regras de CORS para o bucket antes de acessá-lo pelo navegador. Para obter mais informações, consulte Installation.
-
Na maioria dos casos, o OSS SDK for Browser.js é usado em navegadores. Para evitar a exposição do seu par de AccessKey, recomendamos usar credenciais de acesso temporárias obtidas pelo Security Token service (STS) para acessar o OSS.
As credenciais de acesso temporárias consistem em um par de AccessKey e um token de segurança. O par de AccessKey é composto por um AccessKey ID e um AccessKey secret. Para obter mais informações sobre como obter credenciais de acesso temporárias, consulte Use STS for temporary access authorization.
Excluir um único arquivo
O código a seguir exclui um único arquivo:
<!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>
Excluir vários arquivos
O código a seguir exclui vários arquivos:
<!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>
Referências
-
Excluir um único arquivo
Para ver o código de exemplo completo, consulte GitHub example.
Para obter mais informações sobre a operação de API, consulte DeleteObject.
-
Excluir vários arquivos
Para ver o código de exemplo completo, consulte GitHub example.
Para obter mais informações sobre a operação de API, consulte DeleteMultipleObjects.