I'm trying to list the files under sub-directory in S3 but I'm not able to list the files name:
import boto
from boto.s3.connection import S3Connection
access=''
secret=''
conn=S3Connection(access,secret)
bucket1=conn.get_bucket('bucket-name')
prefix='sub -directory -path'
print bucket1.list(prefix)
files_list=bucket1.list(prefix,delimiter='/')
print files_list
for files in files_list:
print files.name
Can you please help me to resolve this issue.
open the bucket, select "none" on the right hand side, and start typing in the file name. Still only let's you search by the prefix of the item name.
Boto3 is the official AWS SDK for Python, used to create, configure, and manage AWS services. The following are examples of defining a resource/client in boto3 for the Weka S3 service, managing credentials, and pre-signed URLs, generating secure temporary tokens, and using those to run S3 API calls.
Your code can be fixed by adding a /
at the end of the Prefix.
The modern equivalent using boto3 would be:
import boto3
s3 = boto3.resource('s3')
## Bucket to use
bucket = s3.Bucket('my-bucket')
## List objects within a given prefix
for obj in bucket.objects.filter(Delimiter='/', Prefix='fruit/'):
print(obj.key)
Output:
fruit/apple.txt
fruit/banana.txt
Rather than using the S3 client, this code uses the S3 object provided by boto3, which makes some code simpler.
You can do this by using boto3. Listing out all the files.
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('bucket-name')
objs = list(bucket.objects.filter(Prefix='sub -directory -path'))
for i in range(0, len(objs)):
print(objs[i].key)
This piece of code will print all the files with path present in the sub-directory
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With