Este tema explica cómo eliminar un solo objeto, varios objetos o los objetos con un prefijo específico de un bucket con control de versiones.
Precauciones
En este tema se utiliza el endpoint público de la región China (Hangzhou). Para acceder a OSS desde otros servicios de Alibaba Cloud en la misma región, use un endpoint interno. Para obtener más información sobre las regiones y los endpoints compatibles, consulte Regions and endpoints.
En este tema se muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para conocer configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Create an OssClient.
Para eliminar un objeto, debe contar con el permiso
oss:DeleteObject. Para obtener más información, consulte Grant custom permissions to a RAM user.
Comportamiento de eliminación en buckets con control de versiones
-
Eliminación temporal (sin especificar un identificador de versión):
Si ejecuta una operación de eliminación sin especificar un identificador de versión, OSS no elimina la versión actual del objeto. En su lugar, inserta un marcador de eliminación para dicho objeto. Cuando recupera el objeto, OSS detecta que la versión actual es un marcador de eliminación y devuelve
404 Not Found. La respuesta también incluye el encabezadox-oss-delete-marker: truey el valorx-oss-version-iddel nuevo marcador de eliminación. -
Eliminación permanente (al especificar un identificador de versión):
Si ejecuta una operación de eliminación y especifica un identificador de versión, OSS elimina permanentemente la versión indicada según el parámetro
versionIddel objetoparams. Para eliminar una versión cuyo identificador es "null", agregue al objetoparamsla configuraciónparams['versionId'] = "null". OSS interpreta la cadena "null" como el identificador de versión y elimina la versión del objeto cuyo identificador es "null".
Eliminar un solo objeto
A continuación se muestran ejemplos de cómo eliminar un solo objeto de forma permanente o temporal.
-
Eliminación permanente
El siguiente código muestra cómo eliminar permanentemente un objeto al especificar su identificador de versión:
<?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"); -
Eliminación temporal
El siguiente código muestra cómo eliminar temporalmente un objeto sin especificar su identificador de versión:
<?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 obtener más información, consulte DeleteObject.
Eliminar varios objetos
A continuación se muestran ejemplos de cómo eliminar varios objetos de forma permanente o temporal.
-
Eliminación permanente
El siguiente código muestra cómo eliminar permanentemente varios objetos al especificar sus identificadores de versión:
<?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"); -
Eliminación temporal
El siguiente código muestra cómo eliminar varios objetos sin especificar sus identificadores de versión:
<?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 obtener más información, consulte DeleteMultipleObjects.