Reputation: 145
I have a s3 bucket inside which there are many subfolders. I want to print just the keys of all the files from the specific subfolder 'abcxyz' inside that bucket
Succeeded in achieving the files of just that subfolder with the following code:
import boto3, os, sys
from botocore.client import Config
# Removed my credentials here on StackOverflow
ACCESS_KEY_ID = ''
ACCESS_SECRET_KEY = ''
BUCKET_NAME = ''
s3 = boto3.resource(
's3',
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=ACCESS_SECRET_KEY,
config=Config(signature_version='s3v4')
)
my_bucket = s3.Bucket(BUCKET_NAME)
for obj in my_bucket.objects.filter(Prefix=Folder_name + '/'):
print(obj)
It's giving me output as:
s3.ObjectSummary(bucket_name='', key='abcxyz/1.jpg')
s3.ObjectSummary(bucket_name='', key='abcxyz/2.jpg')
s3.ObjectSummary(bucket_name='', key='abcxyz/3.jpg')
.
.
.
and so on.. (Removed the bucket name here)
I want to print just the key name of all the files like:
abcxyz/1.jpg
abcxyz/2.jpg
abcxyz/3.jpg
.
.
.
Referred the links:
https://alexwlchan.net/2017/07/listing-s3-keys/
and
Accessing a specific key in a s3 bucket using boto3
But couldn't think out of the box. Could I get a help to print the desired output?
Upvotes: 0
Views: 2037
Reputation: 11
Add this in the end, it will print
print (obj.get("Key").split("/")[1])
Upvotes: 1
Reputation: 269320
Use:
print(obj.key)
You might want to string the sub-folder off the front.
Upvotes: 3