Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Dosya silme (Python SDK V1)

Son Güncelleme:Oct 13, 2026

Tek bir nesneyi, isme göre birden fazla nesneyi, ön ek ile eşleşen nesneleri veya tüm bir dizini silin.

Uyarı

Silinen nesneler geri alınamaz. Dikkatli olun.

Notlar

  • Bu konuda China (Hangzhou) bölgesinin genel uç noktası kullanılmaktadır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek için dahili 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 erişim kimlik bilgileri ortam değişkenlerinden alınmaktadır. Erişim kimlik bilgilerinin nasıl yapılandırılacağı hakkında daha fazla bilgi için bkz. Python 1.0 için OSS SDK kullanarak erişim kimlik bilgilerini yapılandırma.

  • Bu konu, bir OSS uç noktası ile bir OSSClient örneğinin nasıl oluşturulacağını göstermektedir. Özel etki alanı 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.

Tek bir dosya silme

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

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint for 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"

# Replace examplebucket with the bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Delete the file.
# Replace yourObjectName with the full path of the file to delete. The full path does not include the bucket name. For example, exampledir/exampleobject.txt.
# To delete a folder, set yourObjectName to the folder name. Empty the folder before deleting it.
bucket.delete_object('exampledir/exampleobject.txt')            

Birden fazla dosya silme

İsme, ön eke veya dizine göre tek seferde en fazla 1.000 nesneyi silebilirsiniz.

Nesneleri otomatik olarak silmek için yaşam döngüsü kuralları da yapılandırabilirsiniz. Daha fazla bilgi için bkz. Son değiştirilme zamanına dayalı yaşam döngüsü kuralları.

Belirtilen ada sahip birden çok nesneyi silme

Aşağıdaki kod, isme göre birden fazla nesneyi siler:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint for 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"

# Replace examplebucket with the bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Batch delete three files. You can delete up to 1,000 files in a single request.
# Specify the full paths of the three files to delete. The full paths do not include the bucket name.
result = bucket.batch_delete_objects(['exampleobject1.jpg', 'testobject2.png', 'destobject3.txt'])
# Print the names of the successfully deleted files.
print('\n'.join(result.deleted_keys))            

Belirtilen nesne adı ön ekiyle veya dizin içinde birden çok nesneyi silme

Aşağıdaki kod, ön eke göre nesneleri siler veya tüm bir dizini siler:

Uyarı

Ön ek belirtilmezse veya NULL olarak ayarlanırsa paketteki tüm nesneler silinir. Ön ekleri dikkatlice belirtin.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint for 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"

# Replace examplebucket with the bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Set the prefix for deletion. Use 'src' to delete all files with 'src' prefix.
# Use 'src/' to delete only the folder 'src' and all files within it.
# prefix = 'src/'

# Delete multiple files.
for obj in oss2.ObjectIterator(bucket, prefix=prefix):
    bucket.delete_object(obj.key)

İlgili konular