Bu konu, Object Storage Service (OSS) SDK for Python kullanarak bir paketteki tüm nesnelerin nasıl listeleneceğini açıklar.
Kullanım notları
Bu konudaki örnek kod, Çin (Hangzhou) bölgesinin bölge kimliği olarak
cn-hangzhoudeğerini kullanır. Varsayılan olarak, bir paketteki kaynaklara erişmek için genel bir uç nokta kullanılır. Paketin bulunduğu bölgedeki diğer Alibaba Cloud hizmetlerini kullanarak paketteki kaynaklara 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.Nesneleri listelemek için
oss:ListObjectsiznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke atama.
Örnek kod
Aşağıdaki örnek kod, bir paket içindeki tüm nesneleri listelemek üzere ListObjectsV2 işlemi çağrısının nasıl yapılacağını gösterir:
import argparse
import alibabacloud_oss_v2 as oss
# Create a command line parameter parser.
parser = argparse.ArgumentParser(description="list objects v2 sample")
# Specify the --region parameter, which specifies the region in which the bucket is located. This command line parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the --bucket parameter, which specifies the name of the bucket. This command line parameter is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the --endpoint parameter, which specifies the endpoint that other services can use to access OSS. This command line parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
def main():
args = parser.parse_args() # Parse the command line parameters.
# Obtain access credentials from environment variables for authentication.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Load the default configurations of the SDK and specify the credential provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Set the region in the configuration to the one specified in the command line.
cfg.region = args.region
# If the endpoint parameter is provided, specify the endpoint.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Use the configurations to create an OSSClient instance.
client = oss.Client(cfg)
# Create a paginator to allow the ListObjectsV2 operation to list objects.
paginator = client.list_objects_v2_paginator()
# Traverse each page of the listed objects.
for page in paginator.iter_page(oss.ListObjectsV2Request(
bucket=args.bucket
)
):
# Traverse each object on each page.
for o in page.contents:
# Display the name, size, and last modified time of the object.
print(f'Object: {o.key}, {o.size}, {o.last_modified}')
if __name__ == "__main__":
main() # Specify the entry points in the main function of the script when the script is directly run.
Yaygın senaryolar
Belirli bir dizindeki tüm nesneleri listeleme
Adlarında belirli bir önek içeren nesneleri listeleme
Belirli sayıda nesneyi listeleme
Tüm nesneleri belirli bir konumdan itibaren listeleme
Başvurular
Nesneleri listelemek için kullanılan tam örnek kodu görüntülemek için list_objects_v2.py adresini ziyaret edin.