Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Delete objects (OSS SDK for C# 1.0)

Son Güncelleme:Oct 01, 2026

Bu konuda, tek bir nesnenin veya birden çok nesnenin aynı anda nasıl silineceği açıklanmaktadır.

Uyarı

Silinen nesneler kurtarılamaz. Lütfen dikkatli olun.

Notlar

  • Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmaktadır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye erişmek için dahili bir uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.

  • Bu konuda, bir OSS uç noktası kullanılarak bir OSSClient örneğinin oluşturulması gösterilmektedir. Özel bir etki alanı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. Başlatma (C# SDK V1).

  • Bir nesneyi silmek için oss:DeleteObject iznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke atama.

Tek bir nesneyi silme

Aşağıdaki kod, examplebucket adlı paketten exampleobject.txt adlı bir nesneyi siler:

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);
}

Birden çok nesneyi silme

Tek seferde en fazla 1.000 nesne silebilirsiniz.

Sonuç aşağıdaki iki modda döndürülebilir. İhtiyaçlarınıza göre bir dönüş modu seçin.

  • verbose: quietMode belirtilmemişse veya false olarak ayarlanmışsa, silinen tüm nesnelerin listesi döndürülür. Bu, varsayılan dönüş modudur.

  • quiet: quietMode true olarak ayarlanırsa hiçbir mesaj gövdesi döndürülmez.

Aşağıdaki kod, examplebucket adlı bir paketten belirtilen birden çok nesnenin nasıl silineceğine ilişkin bir örnek sunar:

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);
}

İlgili bilgiler

  • Tek bir nesneyi silme

    Tek bir nesneyi silmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. DeleteObject.

  • Birden çok nesneyi silme

    • Birden çok nesneyi silmek için kullanılan eksiksiz örnek kod için GitHub adresini ziyaret edin.

    • Birden çok nesneyi silmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. DeleteMultipleObjects.