Cette rubrique explique comment supprimer un objet unique ou plusieurs objets simultanément.
Les objets supprimés ne peuvent pas être récupérés. Agissez avec prudence.
Notes
Dans cette rubrique, le point de terminaison public de la région Chine (Hangzhou) est utilisé. Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région que votre bucket OSS, utilisez un point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez Régions et points de terminaison.
Cette rubrique illustre la création d'une instance OSSClient avec un point de terminaison OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Initialisation (SDK C# V1).
Pour supprimer un objet, vous devez disposer de l'autorisation
oss:DeleteObject. Pour en savoir plus, consultez Accorder une politique personnalisée.
Supprimer un objet unique
Le code suivant supprime un objet nommé exampleobject.txt d'un bucket nommé examplebucket :
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt.
var objectName = "exampledir/exampleobject.txt";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters based on your requirements.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Delete the object.
client.DeleteObject(bucketName, objectName);
Console.WriteLine("Delete object succeeded");
}
catch (Exception ex)
{
Console.WriteLine("Delete object failed. {0}", ex.Message);
}
Supprimer plusieurs objets
Vous pouvez supprimer jusqu'à 1 000 objets simultanément.
Le résultat peut être renvoyé selon deux modes. Sélectionnez un mode de retour en fonction de vos besoins.
verbose : si quietMode n'est pas spécifié ou est défini sur false, une liste de tous les objets supprimés est renvoyée. Il s'agit du mode de retour par défaut.
quiet : si quietMode est défini sur true, aucun corps de message n'est renvoyé.
Le code suivant montre comment supprimer plusieurs objets spécifiés d'un bucket nommé examplebucket :
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters based on your requirements.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Specify the paths of multiple objects that you want to delete. Do not include the bucket name in the full paths.
var keys = new List<string>();
keys.Add("exampleobject.txt");
keys.Add("testdir/sampleobject.txt");
// Leave quietMode empty or set quietMode to false to return the list of deleted objects.
var quietMode = false;
var request = new DeleteObjectsRequest(bucketName, keys, quietMode);
// Delete multiple objects.
var result = client.DeleteObjects(request);
if ((!quietMode) && (result.Keys != null))
{
foreach (var obj in result.Keys)
{
Console.WriteLine("Delete successfully : {0} ", obj.Key);
}
}
Console.WriteLine("Delete objects succeeded");
}
catch (Exception ex)
{
Console.WriteLine("Delete objects failed. {0}", ex.Message);
}
Références
-
Supprimer un objet unique
Pour plus d'informations sur l'opération API permettant de supprimer un objet unique, consultez DeleteObject.
-
Supprimer plusieurs objets
Pour obtenir l'exemple de code complet utilisé pour supprimer plusieurs objets, visitez GitHub.
Pour plus d'informations sur l'opération API permettant de supprimer plusieurs objets, consultez DeleteMultipleObjects.