Cette rubrique explique comment supprimer un seul fichier (objet), plusieurs fichiers et des fichiers avec un préfixe spécifié dans un bucket pour lequel le versioning est activé.
Notes
Cette rubrique utilise l'endpoint public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez Régions et endpoints.
Cette rubrique montre comment créer une instance OssClient avec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification avec des identifiants du Security Token Service (STS), consultez Créer une instance OssClient.
Pour supprimer un objet, vous devez disposer de l'autorisation
oss:DeleteObject. Pour plus d'informations, consultez Accorder une politique personnalisée.
Comportement de suppression dans les buckets versionnés
Lorsque vous supprimez un objet d'un bucket versionné, vous devez décider si vous souhaitez spécifier un ID de version.
-
Supprimer un objet sans spécifier d'ID de version (suppression temporaire)
Si vous ne spécifiez pas d'ID de version, OSS ne supprime pas la version actuelle. Il ajoute plutôt un marqueur de suppression en tant que dernière version. Les requêtes GetObject ultérieures renvoient
404 Not Foundavecheader:x-oss-delete-marker = trueet l'x-oss-version-iddu marqueur de suppression dans la réponse.Lorsque
x-oss-delete-markerest vrai,x-oss-version-idrenvoie l'ID de version du marqueur de suppression. -
Supprimer un objet en spécifiant un ID de version (suppression permanente)
Si vous spécifiez un ID de version, OSS supprime définitivement cette version à l'aide du paramètre
versionIddansparams. Pour supprimer la version avec un ID nul, définissezparams['versionId'] = "null"dansparams. OSS traite la chaîne « null » comme l'ID de version cible.
Supprimer un seul fichier par nom
Supprimer définitivement un fichier
Le code suivant montre comment supprimer définitivement une version spécifique d'un objet en spécifiant son versionId. Les versions d'objets supprimées définitivement ne peuvent pas être récupérées.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name, for example, examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 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 set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Delete the object with the specified versionId or the object associated with the delete marker that has the specified versionId. */
auto outcome = client.DeleteObject(DeleteObjectRequest(BucketName, ObjectName, "yourObjectVersionIdOrDeleteMarkerVersionId"));
/* If the versionId of an object is specified, the returned delete_marker is None and the returned versionId is the versionId of the specified object. */
/* If the versionId of a delete marker is specified, the returned delete_marker is True and the returned versionId is the versionId of the specified delete marker. */
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "DeleteObject fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Supprimer temporairement un fichier
Le code suivant montre comment supprimer temporairement un objet sans spécifier de versionId. Une version d'objet supprimée temporairement peut être récupérée.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name, for example, examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 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 set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
DeleteObjectRequest request(BucketName, ObjectName);
/* Temporarily delete the object without specifying a versionId. This operation adds a delete marker to the object. */
auto outcome = client.DeleteObject(request);
/* View the versionId of the returned delete marker. */
if (outcome.isSuccess()) {
std::cout << "versionid:" << outcome.result().VersionId() << ",DeleteMarker:" << outcome.result().DeleteMarker() << std::endl;
}
else {
/* Handle exceptions. */
std::cout << "DeleteObject fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Supprimer plusieurs fichiers par nom
Supprimer définitivement des fichiers
Le code suivant montre comment supprimer définitivement plusieurs versions d'objets en spécifiant leurs versionIds, ou supprimer des marqueurs de suppression en spécifiant leurs versionIds. Les versions d'objets supprimées définitivement ne peuvent pas être récupérées.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name, for example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 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 set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
DeleteObjectVersionsRequest request(BucketName);
/* Add the versionIds of the objects or delete markers to delete. */
ObjectIdentifier obj1("yourObject1Name");
obj1.setVersionId("yourVersionId");
ObjectIdentifier obj2("yourObject2Name");
obj2.setVersionId("obj2_del_marker_versionid");
request.addObject(obj1);
request.addObject(obj2);
/* Batch delete the objects with the specified versionIds or the objects associated with the specified delete marker versionIds. */
auto outcome = client.DeleteObjectVersions(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "DeleteObjectVersions fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Supprimer temporairement des fichiers
Le code suivant montre comment supprimer temporairement plusieurs objets sans spécifier leurs versionIds. Les versions d'objets supprimées temporairement peuvent être récupérées.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name, for example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 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 set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
DeleteObjectVersionsRequest request(BucketName);
/* Specify the names of the objects to delete. */
ObjectIdentifier obj1("ObjectName1");
ObjectIdentifier obj2("ObjectName2");
ObjectIdentifier obj3("ObjectName3");
request.addObject(obj1);
request.addObject(obj2);
request.addObject(obj3);
/* Delete the objects. */
auto outcome = client.DeleteObjectVersions(request);
if (outcome.isSuccess()) {
for (auto const &obj : outcome.result().DeletedObjects()) {
std::cout << "versionid:" << obj.VersionId() << ",DeleteMarker:" << obj.DeleteMarker() << std::endl;
}
}
else {
/* Handle exceptions. */
std::cout << "DeleteObjectVersions fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Supprimer des fichiers avec un préfixe spécifié
Le code suivant montre comment supprimer des fichiers avec un préfixe spécifié.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name, for example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 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 set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
ListObjectVersionsRequest request(BucketName);
bool IsTruncated = false;
do {
request.setPrefix("yourkeyPrefix");
auto outcome = client.ListObjectVersions(request);
if (outcome.isSuccess()) {
/* View the version information of the listed objects and delete markers. */
for (auto const &marker : outcome.result().DeleteMarkerSummarys()) {
client.DeleteObject(DeleteObjectRequest(BucketName, marker.Key(), marker.VersionId()));
}
/* List the version information of all objects with the specified prefix and delete these files. */
for (auto const &obj : outcome.result().ObjectVersionSummarys()) {
client.DeleteObject(DeleteObjectRequest(BucketName, obj.Key(), obj.VersionId()));
}
}
else {
std::cout << "ListObjectVersions fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
break;
}
request.setKeyMarker(outcome.result().NextKeyMarker());
request.setVersionIdMarker(outcome.result().NextVersionIdMarker());
IsTruncated = outcome.result().IsTruncated();
} while (IsTruncated);
/* Release network resources. */
ShutdownSdk();
return 0;
}
Références
Pour plus d'informations sur l'opération API permettant de supprimer un seul fichier, consultez DeleteObject.
Pour plus d'informations sur l'opération API permettant de supprimer plusieurs fichiers, consultez DeleteMultipleObjects.