Cette rubrique explique comment supprimer un objet unique, plusieurs objets ou des objets dotés d'un préfixe spécifié dans un compartiment versionné.
Précautions
Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Pour accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un point de terminaison interne. Pour en savoir plus sur les régions et points de terminaison pris en charge, consultez Régions et points de terminaison.
Cette rubrique montre comment créer une instance OssClient avec un endpoint OSS. Pour d'autres configurations, comme l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Créer un OssClient.
Pour supprimer un objet, vous devez disposer de l'autorisation
oss:DeleteObject. Pour plus d'informations, consultez Accorder des autorisations personnalisées à un utilisateur RAM.
Comportements de suppression pour les compartiments versionnés
-
Suppression temporaire (sans spécifier d'ID de version) :
Si vous supprimez un objet sans spécifier d'ID de version, OSS ne supprime pas la version actuelle de l'objet. Il insère plutôt un marqueur de suppression. Lors de la récupération de l'objet, OSS détecte que la version actuelle est un marqueur de suppression et renvoie
404 Not Found. La réponse inclut également l'en-têtex-oss-delete-marker: trueainsi que l'x-oss-version-iddu nouveau marqueur de suppression. -
Suppression définitive (en spécifiant un ID de version) :
Si vous supprimez un objet en spécifiant un ID de version, OSS supprime définitivement la version indiquée par le paramètre
versionIdde l'objetparams. Pour supprimer une version dont l'ID est « null », ajoutez le paramètreparams['versionId'] = "null"à l'objetparams. OSS traite la chaîne « null » comme un ID de version et supprime la version de l'objet correspondante.
Supprimer un objet unique
Les exemples suivants montrent comment supprimer définitivement ou temporairement un objet unique.
-
Suppression définitive
Le code suivant montre comment supprimer définitivement un objet en spécifiant son ID de version :
<?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"); -
Suppression temporaire
Le code suivant montre comment supprimer temporairement un objet sans spécifier d'ID de version :
<?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");
Pour plus d'informations, consultez DeleteObject.
Supprimer plusieurs objets
Les exemples suivants montrent comment supprimer définitivement ou temporairement plusieurs objets.
-
Suppression définitive
Le code suivant montre comment supprimer définitivement plusieurs objets en spécifiant leurs ID de version :
<?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"); -
Suppression temporaire
Le code suivant montre comment supprimer temporairement plusieurs objets sans spécifier leurs ID de version :
<?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");
Pour plus d'informations, consultez DeleteMultipleObjects.