第5章 バケット内のファイルのリスト表示
特定のバケット内のファイルをリスト表示するには、list_bucket_v2()
メソッドを使用します。
前提条件
-
odh-doc-examples
リポジトリーをワークベンチにクローンしている。 -
ワークベンチで
s3client_examples.ipynb
ファイルを開いている。 - Boto3 をインストールし、S3 クライアントを設定している。
手順
ノートブックで、ファイルをリスト表示するための次のコードを参照します。
#List files #Replace <bucket_name> with the name of the bucket. bucket_name = ‘<bucket_name>’ s3_client.list_objects_v2(Bucket=bucket_name)
例に示すように、
<bucket_name>
を独自のバケットの名前に置き換えて、コードセルを実行します。#List files #Replace <bucket_name> with the name of the bucket. bucket_name = ‘aqs27-registry’ s3_client.list_objects_v2(Bucket=bucket_name)
出力には、指定されたバケットで使用可能なファイルに関する情報が表示されます。
ファイル名のみをリストするコードセルを参照します。
#Print only names of files bucket_name = ‘<bucket_name>’ for key in s3_client.list_objects_v2(Bucket=bucket_name)[‘Contents’]: print(key[‘Key’])
例に示すように、
<bucket_name>
をバケットの名前に置き換えて、コードセルを実行します。#Print only names of files bucket_name = ‘aqs27-registry’ for key in s3_client.list_objects_v2(Bucket=bucket_name)[‘Contents’]: print(key[‘Key’])
出力には、指定されたバケットで使用可能なファイル名のリストが表示されます。
次のコードセルを参照して、前のクエリーを改良し、ファイルパスを指定します。
bucket_name = ‘<bucket_name>’ for key in s3_client.list_objects_v2(Bucket=bucket_name,Prefix=’<start_of_file_path’)[‘Contents’]: print(key[‘Key’])
-
<bucket_name>
と<start_of_file_path>
を独自の値に置き換えて、コードセルを実行します。