Bu konu, sürüm oluşturulmuş bir paketteki nesnelerin nasıl listeleneceğini açıklar. Tüm nesneleri, belirtilen sayıda nesneyi ve adları belirli bir ön ek içeren nesneleri listeleyebilirsiniz.
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'ye erişmek istiyorsanız 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. Python için OSS SDK 1.0 kullanarak erişim kimlik bilgilerini yapılandırma.
Bu konuda bir OSS uç noktası kullanılarak bir OSSClient örneği oluşturulmaktadır. Özel etki alanı kullanma veya Güvenlik Belirteci Hizmeti (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. Başlatma.
Nesneleri listelemek için
oss:ListObjectVersionsiznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke atama.
Bir paketteki tüm nesnelerin sürümlerini listeleme
Aşağıdaki kod, belirtilen bir paketteki silme işaretçileri dahil olmak üzere tüm nesnelerin sürümlerinin nasıl listeleneceğine ilişkin bir örnek sunar:
# -*- 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 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.
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, "yourBucketName", region=region)
# Call the list_object_versions operation to list the versions of objects in a versioning-enabled bucket.
# List the versions of all objects, including delete markers in the bucket.
result = bucket.list_object_versions()
# List the versions of all objects in the bucket.
next_key_marker = None
next_versionid_marker = None
while True:
result = bucket.list_object_versions(key_marker=next_key_marker, versionid_marker=next_versionid_marker)
# Display the versions of the listed objects.
for version_info in result.versions:
print('version_info.versionid:', version_info.versionid)
print('version_info.key:', version_info.key)
print('version_info.is_latest:', version_info.is_latest)
# Display the versions of the listed delete markers.
for del_maker_Info in result.delete_marker:
print('del_maker.key:', del_maker_Info.key)
print('del_maker.versionid:', del_maker_Info.versionid)
print('del_maker.is_latest:', del_maker_Info.is_latest)
is_truncated = result.is_truncated
# Check whether all versions of all objects in the bucket are listed. If the versions of all objects are incompletely listed, the list operation continues. If the versions of all objects are completely listed, the list operation stops.
if is_truncated:
next_key_marker = result.next_key_marker
next_versionid_marker = result.next_versionid_marker
else:
break
Adları belirtilen bir ön ek içeren nesnelerin sürümlerini listeleme
Aşağıdaki kod, adları belirtilen bir ön ek içeren nesnelerin sürümlerinin nasıl listeleneceğine ilişkin bir örnek sunar:
# -*- 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 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.
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, "yourBucketName", region=region)
# Call the list_object_versions operation to list the versions of objects in a versioning-enabled bucket.
# List the versions of all objects, including delete markers in the bucket.
result = bucket.list_object_versions()
# List the versions of objects whose names contain the test- prefix.
prefix = 'test-'
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)
# Display the versions of the listed objects.
for version_info in result.versions:
print('version_info.versionid:', version_info.versionid)
print('version_info.key:', version_info.key)
print('version_info.is_latest:', version_info.is_latest)
# Display the versions of the listed delete markers.
for del_maker_Info in result.delete_marker:
print('del_maker.key:', del_maker_Info.key)
print('del_maker.versionid:', del_maker_Info.versionid)
print('del_maker.is_latest:', del_maker_Info.is_latest)
is_truncated = result.is_truncated
# Check whether all versions of all objects in the bucket are listed. If the versions of all objects are incompletely listed, the list operation continues. If the versions of all objects are completely listed, the list operation stops.
if is_truncated:
next_key_marker = result.next_key_marker
next_versionid_marker = result.next_versionid_marker
else:
break
Belirtilen sayıda nesnenin sürümlerini listeleme
Aşağıdaki kod, belirli sayıda nesnenin sürümlerinin nasıl listeleneceğine ilişkin bir örnek sunar:
# -*- 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 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.
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, "yourBucketName", region=region)
# Call the list_object_versions operation to list the versions of objects in a versioning-enabled bucket.
# List the versions of all objects, including delete markers in the bucket.
result = bucket.list_object_versions()
# List up to 200 object versions.
max_keys = 200
result = bucket.list_object_versions(max_keys=max_keys)
# Display the versions of the listed objects.
for version_info in result.versions:
print('version_info.versionid:', version_info.versionid)
print('version_info.key:', version_info.key)
print('version_info.is_latest:', version_info.is_latest)
# Display the versions of the listed delete markers.
for del_maker_Info in result.delete_marker:
print('del_maker.key:', del_maker_Info.key)
print('del_maker.versionid:', del_maker_Info.versionid)
print('del_maker.is_latest:', del_maker_Info.is_latest)
# Check whether the listing is truncated.
# If the number of objects in the bucket is greater than 200, the value of is_truncated is True, which indicates that the listing is truncated. If the number of objects in the bucket is smaller than 200, the value of is_truncated is False, which indicates that the listing is not truncated.
print('is truncated', result.is_truncated)
Nesneleri dizine göre listeleme
OSS, nesneleri depolamak için düz bir yapı kullanır. Dizin, adı eğik çizgi (/) ile biten sıfır baytlık bir nesnedir. Bir dizini yükleyebilir ve indirebilirsiniz. Varsayılan olarak, adı eğik çizgi (/) ile biten bir nesne OSS konsolunda dizin olarak görüntülenir.
İstek içinde delimiter ve prefix parametrelerini belirterek nesneleri dizine göre listeleyebilirsiniz.
İstekte
prefixdeğerini bir dizin adına ayarlarsanız, adları bu ön eki içeren nesneler ve alt dizinler listelenir.Bir
prefixbelirtipdelimiterdeğerini eğik çizgi (/) olarak ayarlarsanız, dizindeki belirtilen ön ekle başlayan nesneler ve alt dizinler listelenir. Her alt dizin,CommonPrefixesiçinde tek bir sonuç öğesi olarak listelenir. Bu alt dizinlerdeki nesneler ve dizinler listelenmez.
Örnek: examplebucket adlı bir paket şu nesneleri içerir: oss.jpg, fun/test.jpg, fun/movie/001.avi ve fun/movie/007.txt. Eğik çizgi (/), dizin için sınırlayıcı olarak kullanılır. Aşağıdaki yapı, examplebucket paketindeki nesneleri ve dizinleri göstermektedir.
examplebucket
└── oss.jpg
└── fun
└── test.jpg
└── movie
└── 001.avi
└── 007.txt
Aşağıdaki örnekler, simüle edilmiş dizinlerdeki nesnelerin nasıl listeleneceğini açıklamaktadır.
-
Bir paketin kök dizinindeki nesnelerin sürümlerini listeleme
Aşağıdaki kod, bir paketin kök dizinindeki nesnelerin sürümlerinin nasıl listeleneceğine ilişkin bir örnek sunar:
# -*- 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 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. 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, "yourBucketName", region=region) # Call the list_object_versions operation to list the versions of objects in a versioning-enabled bucket. # List the versions of all objects, including delete markers in the bucket. result = bucket.list_object_versions() # Specify the forward slash (/) as the delimiter. delimiter = "/" next_key_marker = None next_versionid_marker = None while True: result = bucket.list_object_versions(delimiter=delimiter, key_marker=next_key_marker, versionid_marker=next_versionid_marker) # Display the versions of the listed objects. for version_info in result.versions: print('version_info.versionid:', version_info.versionid) print('version_info.key:', version_info.key) print('version_info.is_latest:', version_info.is_latest) # Display the versions of the listed delete markers. for del_maker_Info in result.delete_marker: print('del_maker.key:', del_maker_Info.key) print('del_maker.versionid:', del_maker_Info.versionid) print('del_maker.is_latest:', del_maker_Info.is_latest) # Display the directories whose names end with a forward slash (/). for common_prefix in result.common_prefix: print("common_prefix:", common_prefix) is_truncated = result.is_truncated # Check whether all versions of all objects in the bucket are listed. If the versions of all objects are incompletely listed, the list operation continues. If the versions of all objects are completely listed, the list operation stops. if is_truncated: next_key_marker = result.next_key_marker next_versionid_marker = result.next_versionid_marker else: breakSonuç
('version_info.versionid:', 'CAEQEhiBgMCw8Y7FqBciIGIzMDE3MTEzOWRiMDRmZmFhMmRlMjljZWI0MWU4****') ('version_info.key:', 'oss.jpg') ('version_info.is_latest:', True) ('common_prefix:', 'fun/') -
Belirtilen bir dizindeki nesneleri ve alt dizinleri listeleme
Aşağıdaki kod, bir paketin dizinindeki nesnelerin ve alt dizinlerin nasıl listeleneceğine ilişkin bir örnek sunar:
# -*- 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 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. 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, "yourBucketName", region=region) # Call the list_object_versions operation to list the versions of objects in a versioning-enabled bucket. # List the versions of all objects including delete markers in a bucket. result = bucket.list_object_versions() # Set delimiter to a forward slash (/) and prefix to fun/. prefix = "fun/" delimiter = "/" next_key_marker = None next_versionid_marker = None while True: result = bucket.list_object_versions(prefix=prefix, delimiter=delimiter, key_marker=next_key_marker, versionid_marker=next_versionid_marker) # Display the versions of the listed objects. for version_info in result.versions: print('version_info.versionid:', version_info.versionid) print('version_info.key:', version_info.key) print('version_info.is_latest:', version_info.is_latest) # Display the versions of the listed delete markers. for del_maker_Info in result.delete_marker: print('del_maker.key:', del_maker_Info.key) print('del_maker.versionid:', del_maker_Info.versionid) print('del_maker.is_latest:', del_maker_Info.is_latest) # Display the directories whose names end with a forward slash (/). for common_prefix in result.common_prefix: print("common_prefix:", common_prefix) is_truncated = result.is_truncated # Check whether all versions of all objects in the bucket are listed. If the versions of all objects are incompletely listed, the list operation continues. If the versions of all objects are completely listed, the list operation stops. if is_truncated: next_key_marker = result.next_key_marker next_versionid_marker = result.next_versionid_marker else: breakSonuç
('version_info.versionid:', 'CAEQFRiBgMCh9JDkrxciIGE3OTNkYzFhYTc2YzQzOTQ4Y2MzYjg2YjQ4ODg*****') ('version_info.key:', 'fun/test.jpg') ('version_info.is_latest:', True) ('commonPrefix:', 'fun/movie/')
İlgili konular
Nesneleri listelemek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. ListObjectVersions (GetBucketVersions).