Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Delete files (Python SDK V1)

Son Güncelleme:Oct 01, 2026

Bu konuda, sürüm oluşturma özelliği etkinleştirilmiş bir paketten bir veya daha fazla dosyanın (nesne) ya da belirtilen bir öneke sahip dosyaların nasıl silineceği açıklanmaktadır.

Notlar

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

  • Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınır. Erişim kimlik bilgilerinin nasıl yapılandırılacağı hakkında daha fazla bilgi için bkz. OSS SDK for Python 1.0 kullanarak erişim kimlik bilgilerini yapılandırma.

  • Bu konuda bir OSS uç noktası kullanarak bir OSSClient örneğinin nasıl oluşturulacağı gösterilmektedir. Özel alan adı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. Başlatma.

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

Sürüm oluşturma özelliği etkinleştirilmiş bir pakette silme davranışları

Silme davranışı aşağıdaki gibidir:

  • versionId belirtilmeden (yumuşak silme):

    versionId belirtmeden bir silme işlemi yaparsanız OSS, nesneyi silmek yerine nesnenin geçerli sürümü için bir silme belirteci ekler. Ardından GetObject işlemi yaptığınızda OSS silme belirtecini algılar ve 404 Not Found hatası döndürür. Yanıt ayrıca x-oss-delete-marker = true üstbilgisini ve yeni silme belirtecinin sürüm numarasını x-oss-version-id üstbilgisinde döndürür.

    x-oss-delete-marker değeri true olarak dönerse bu, döndürülen x-oss-version-id değerine karşılık gelen sürümün bir silme belirteci olduğunu gösterir.

  • versionId belirtilerek (kalıcı silme):

    Bir silme işlemi yapıp versionId belirtirseniz OSS, params nesnesinin versionId parametresine göre nesnenin ilgili sürümünü kalıcı olarak siler. Kimliği "null" olan bir sürümü silmek için params parametresini kullanın ve params['versionId'] = "null" olarak ayarlayın. OSS, "null" dizesini versionId "null" olarak değerlendirir ve bu versionId'ye sahip nesneyi siler.

Tek bir dosyayı silme

Aşağıdaki örneklerde tek bir nesnenin kalıcı veya yumuşak şekilde nasıl silineceği gösterilmektedir.

  • Kalıcı silme

    Aşağıdaki kod, versionId'sini belirterek bir nesnenin nasıl kalıcı olarak silineceğini gösterir:

    # -*- coding: utf-8 -*-
    import os
    import oss2
    from oss2.credentials import EnvironmentVariableCredentialsProvider
    # 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.
    auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
    
    # Specify 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.
    endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
    # Specify the region information that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
    region = "cn-hangzhou"
    
    # Set yourBucketName to the name of the bucket.
    bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
    
    # Set yourObjectName to the full path of the object. Do not include the bucket name. Example: example/test.txt.
    object_name = 'yourObjectName'
    
    # Specify the versionId of the object. This can also be the versionId of a delete marker.
    params = dict()
    params['versionId'] = 'yourObjectVersionIdOrDeleteMarkerVersionId'
    
    # Delete the object or the object associated with the delete marker that has the specified versionId.
    result = bucket.delete_object(object_name, params=params)
    print("delete object name: ", object_name)
    # 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 result.delete_marker:
        print("delete del-marker versionid: ",result.versionid)
    else:
        print("delete object versionid:", result.versionid)
  • Yumuşak silme

    Aşağıdaki kod, versionId belirtmeden bir nesnenin nasıl yumuşak şekilde silineceğini gösterir:

    # -*- coding: utf-8 -*-
    import os
    import oss2
    from oss2.credentials import EnvironmentVariableCredentialsProvider
    # 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.
    auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
    
    # Specify 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.
    endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
    # Specify the region information that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
    region = "cn-hangzhou"
    
    # Set yourBucketName to the name of the bucket.
    bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
    
    # Set yourObjectName to the full path of the object. Do not include the bucket name. Example: example/test.txt.
    object_name = 'yourObjectName'
    
    # Soft delete the object without specifying a versionId. This operation adds a delete marker to the object.
    result = bucket.delete_object(object_name)
    # View the delete marker.
    print("delete marker: ", result.delete_marker)
    # View the versionId of the returned delete marker.
    print("delete marker versionid: ", result.versionid)

Birden fazla dosyayı silme

Aşağıdaki örneklerde birden fazla nesnenin kalıcı veya yumuşak şekilde nasıl silineceği gösterilmektedir.

  • Kalıcı silme

    Aşağıdaki kod, versionIds'lerini belirterek birden fazla nesnenin ve silme belirtecinin nasıl kalıcı olarak silineceğini gösterir:

    # -*- coding: utf-8 -*-
    import os
    import oss2
    from oss2.credentials import EnvironmentVariableCredentialsProvider
    from oss2.models import BatchDeleteObjectVersion
    from oss2.models import BatchDeleteObjectVersionList
    # 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.
    auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
    
    # Specify 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.
    endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
    # Specify the region information that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
    region = "cn-hangzhou"
    
    # Set yourBucketName to the name of the bucket.
    bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
    
    version_list = BatchDeleteObjectVersionList()
    # You can pass the versionId of an object or a delete marker.
    obj1_versionid = 'yourObject1VersionId'
    obj1_del_marker_versionid = 'yourObject1DelMarkerVersionId'
    obj2_versionid = 'yourObject2VersionId'
    obj2_del_marker_versionid = 'yourObject2DelMarkerVersionId'
    version_list.append(BatchDeleteObjectVersion(key='yourObject1Name', versionid=obj1_versionid))
    version_list.append(BatchDeleteObjectVersion(key='yourObject1Name', versionid=obj1_del_marker_versionid))
    version_list.append(BatchDeleteObjectVersion(key='yourObject2Name', versionid=obj2_versionid))
    version_list.append(BatchDeleteObjectVersion(key='yourObject2Name', versionid=obj2_del_marker_versionid))
    
    # Batch delete objects or delete markers with specified versionIds.
    result = bucket.delete_object_versions(version_list)
    # View the versionIds of the deleted objects or delete markers.
    for del_version in result.delete_versions:
        print('del object name:', del_version.key)
        # Check whether a delete marker was deleted.
        print('Is del marker:', del_version.delete_marker)
        # If a delete marker was deleted, print the versionId of the deleted delete marker. Otherwise, print the versionId of the deleted object.
        if del_version.delete_marker:
            print('del object del_marker.versionid', del_version.delete_marker_versionid)
        else:
            print('del object versionid:', del_version.versionid)
  • Yumuşak silme

    Aşağıdaki kod, versionIds'lerini belirtmeden birden fazla nesnenin nasıl yumuşak şekilde silineceğini gösterir:

    # -*- coding: utf-8 -*-
    import os
    import oss2
    from oss2.credentials import EnvironmentVariableCredentialsProvider
    from oss2.models import BatchDeleteObjectVersion
    from oss2.models import BatchDeleteObjectVersionList
    # 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.
    auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
    
    # Specify 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.
    endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
    # Specify the region information that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
    region = "cn-hangzhou"
    
    # Set yourBucketName to the name of the bucket.
    bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
    
    key_list = ['yourObject1Name', 'yourObject2Name']
    # After you perform a delete operation without specifying a versionId, a delete marker is added to the object.
    result = bucket.batch_delete_objects(key_list)
    for del_version in result.delete_versions:
        print('key name:', del_version.key)
        # Print the returned delete marker.
        print('Is del marker:', del_version.delete_marker)
        print('key del_marker.versionid', del_version.delete_marker_versionid)

Belirtilen öneke sahip dosyaları silme

Aşağıdaki kod, belirtilen öneke sahip dosyaların nasıl silineceğini gösterir:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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.
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify 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.
# Set yourBucketName to the name of the bucket.
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName')
prefix = "yourKeyPrefix"

# List the versionIds of all files with the specified prefix and delete these files.
next_key_marker = None
next_versionid_marker = None
while True:
    result = bucket.list_object_versions(prefix=prefix, key_marker=next_key_marker, versionid_marker=next_versionid_marker)

    for version_info in result.versions:
        bucket.delete_object(version_info.key, params={'versionId': version_info.versionid})

    for del_marker_info in result.delete_marker:
        bucket.delete_object(del_marker_info.key, params={'versionId': del_marker_info.versionid})

    is_truncated = result.is_truncated

    if is_truncated:
        next_key_marker = result.next_key_marker
        next_versionid_marker = result.next_versionid_marker
    else:
        break

İlgili konular

  • Tek bir dosyayı silmeye yönelik API işlemi hakkında daha fazla bilgi için bkz. DeleteObject.

  • Birden fazla dosyayı silmeye yönelik API işlemi hakkında daha fazla bilgi için bkz. DeleteMultipleObjects.