Este tópico explica como excluir um único objeto, vários objetos ou objetos com um prefixo específico de um bucket com versionamento.
Precauções
Este tópico utiliza o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para obter detalhes sobre as regiões e endpoints compatíveis, consulte Regions and endpoints.
Este tópico demonstra a criação de uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como usar um domínio personalizado ou autenticar-se com credenciais do Security Token Service (STS), consulte Create an OssClient.
A exclusão de um objeto requer a permissão
oss:DeleteObject. Para mais informações, consulte Grant custom permissions to a RAM user.
Comportamentos de exclusão em buckets com versionamento
-
Exclusão temporária (sem especificar um ID de versão):
Se você executar uma operação de exclusão sem especificar um ID de versão, o OSS não exclui a versão atual do objeto. Em vez disso, insere um marcador de exclusão para o objeto. Ao recuperar o objeto, o OSS detecta que a versão atual é um marcador de exclusão e retorna
404 Not Found. A resposta também inclui o cabeçalhox-oss-delete-marker: truee ox-oss-version-iddo novo marcador de exclusão. -
Exclusão permanente (ao especificar um ID de versão):
Ao executar uma operação de exclusão e especificar um ID de versão, o OSS exclui permanentemente a versão indicada com base no parâmetro
versionIddo objetoparams. Para excluir uma versão cujo ID é "null", adicione ao objetoparamsa configuraçãoparams['versionId'] = "null". O OSS trata a string "null" como o ID de versão e exclui a versão do objeto correspondente.
Excluir um único objeto
Os exemplos a seguir mostram como excluir permanentemente ou temporariamente um único objeto.
-
Exclusão permanente
O código a seguir mostra como excluir permanentemente um objeto especificando seu ID de versão:
<?php if (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php'; } if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php'; } use OSS\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; // 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. $provider = new EnvironmentVariableCredentialsProvider(); // The endpoint of the China (Hangzhou) region is used in this example. Replace it with your actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; $bucket= "<yourBucketName>"; // Specify the full path of the object. Do not include the bucket name in the full path. Example: example/test.txt. $object = "<yourObjectName>"; $versionId = "<yourObjectVersionId>"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); try{ // Delete the object with the specified version ID. $ossClient->deleteObject($bucket, $object, array(OssClient::OSS_VERSION_ID => $versionId)); } catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print(__FUNCTION__ . ": OK" . "\n"); -
Exclusão temporária
O código a seguir mostra como excluir temporariamente um objeto sem especificar um ID de versão:
<?php if (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php'; } if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php'; } use OSS\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; // 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. $provider = new EnvironmentVariableCredentialsProvider(); // The endpoint of the China (Hangzhou) region is used in this example. Replace it with your actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; $bucket= "<yourBucketName>"; $object = "<yourObjectName>"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); try{ // Temporarily delete an object without specifying its version ID. This operation adds a delete marker to the object. $ret = $ossClient->deleteObject($bucket, $object); print("delete marker:" .$ret['x-oss-delete-marker'] ."\n"); print("version id:" .$ret['x-oss-version-id']); } catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print(__FUNCTION__ . ": OK" . "\n");
Para mais informações, consulte DeleteObject.
Excluir vários objetos
Os exemplos a seguir mostram como excluir permanentemente ou temporariamente vários objetos.
-
Exclusão permanente
O código a seguir mostra como excluir permanentemente vários objetos especificando seus IDs de versão:
<?php if (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php'; } if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php'; } use OSS\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; use OSS\Model\DeleteObjectInfo; // 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. $provider = new EnvironmentVariableCredentialsProvider(); // The endpoint of the China (Hangzhou) region is used in this example. Replace it with your actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; $bucket= "<yourBucketName>"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); try{ // Delete multiple objects with specified version IDs. $objects = array(); $objects[] = new DeleteObjectInfo("<yourObject1Name>", "<object1VersionId>"); $objects[] = new DeleteObjectInfo("<yourObject2Name>", "<object2VersionId>"); $deletedObjectList = $ossClient->deleteObjectVersions($bucket, $objects); // View the deletion result. if (!empty($deletedObjectList)) { print("deletedObjectList:\n"); foreach ($deletedObjectList as $deletedObjectInfo) { print($deletedObjectInfo->getKey() . ","); print($deletedObjectInfo->getVersionId() . ","); print($deletedObjectInfo->getDeleteMarker() . ","); print($deletedObjectInfo->getDeleteMarkerVersionId() . "\n"); } } } catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print(__FUNCTION__ . ": OK" . "\n"); -
Exclusão temporária
O código a seguir mostra como excluir vários objetos sem especificar seus IDs de versão:
<?php if (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php'; } if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php'; } use OSS\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; use OSS\Model\DeleteObjectInfo; // 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. $provider = new EnvironmentVariableCredentialsProvider(); // The endpoint of the China (Hangzhou) region is used in this example. Replace it with your actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; $bucket= "<yourBucketName>"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); try{ // Temporarily delete multiple objects without specifying their version IDs. This operation adds a delete marker to each object. $objects = array(); $objects[] = new DeleteObjectInfo("<yourObject1Name>"); $objects[] = new DeleteObjectInfo("<yourObject2Name>"); $deletedObjectList = $ossClient->deleteObjectVersions($bucket, $objects); // View the deletion result. if (!empty($deletedObjectList)) { print("deletedObjectList:\n"); foreach ($deletedObjectList as $deletedObjectInfo) { print($deletedObjectInfo->getKey() . ","); print($deletedObjectInfo->getVersionId() . ","); print($deletedObjectInfo->getDeleteMarker() . ","); print($deletedObjectInfo->getDeleteMarkerVersionId() . "\n"); } } } catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print(__FUNCTION__ . ": OK" . "\n");
Para mais informações, consulte DeleteMultipleObjects.