Object Storage Service (OSS), geleneksel dosya sistemlerinin kullandığı hiyerarşik yapı yerine düz bir yapı kullanarak nesneleri depolar. OSS'deki tüm veriler paketlerde nesne olarak saklanır. OSS konsolu, adları eğik çizgi (/) ile biten nesneleri dosya sistemlerindeki klasörlere benzer şekilde dizin olarak görüntüler. Nesneleri hiyerarşik olarak düzenlemek ve erişim kontrolünü basitleştirmek için dizinleri kullanabilirsiniz.
Notlar
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmaktadır. Aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye erişmek için dahili uç nokta kullanın. Daha fazla bilgi için bkz. Bölgeler ve uç noktalar.
Bu konuda kimlik bilgileri ortam değişkenlerinden alınır. Kimlik bilgilerini nasıl yapılandıracağınız hakkında daha fazla bilgi için bkz. Python 1.0 için OSS SDK kullanarak kimlik bilgilerini yapılandırma.
Bu konuda bir OSS uç noktası kullanılarak bir OSSClient örneği oluşturulur. Özel alan adları veya Security Token Service (STS) kullanarak bir OSSClient örneği oluşturmak için bkz. Başlatma.
Dizin oluşturma
Aşağıdaki örnek kod, bir dizinin nasıl oluşturulacağını 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.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint for 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.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Specify the name of the directory. The directory name must end with a forward slash.
bucket.put_object('exampledir/', '')
Dizinleri listeleme
Aşağıdaki örnek kod, kök dizindeki tüm alt dizinlerin nasıl listeleneceğ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.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint for 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.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# List directories
def list_directories(bucket, prefix='', delimiter='/'):
result = bucket.list_objects_v2(prefix=prefix, delimiter=delimiter)
directories = []
# Obtain the directory at the current level.
for common_prefix in result.prefix_list:
directories.append(common_prefix)
return directories
# Invoke the function to obtain all subdirectories in the root directory.
directories = list_directories(bucket)
print("Directory list:", directories)
Dizin silme
Bir dizini silmek, içindeki tüm alt dizinleri ve nesneleri eş zamanlı olarak siler. Lütfen dikkatli olun.
Aşağıdaki örnek kod, examplebucket paketinden log adlı bir dizinin ve içindeki tüm nesnelerin 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.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint for 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.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
prefix = "exampledir/"
# Delete the directory and all objects stored within.
for obj in oss2.ObjectIterator(bucket, prefix=prefix):
bucket.delete_object(obj.key)
İlgili konular
-
Dizin oluşturma
Dizin oluşturmak için kullanılan API işlemi hakkında bilgi edinmek için bkz. PutObject.
-
Dizin silme
Bir dizini ve içindeki tüm nesneleri silen eksiksiz örnek kod için aliyun-oss-python-sdk deposunu
Bir dizini ve içindeki tüm nesneleri silmek için kullanılan API işlemi hakkında bilgi edinmek için bkz. DeleteObject.