I had the same issue but managed to resolve it using boto3.client
and list_objects_v2
with Bucket
and StartAfter
parameters.
s3client = boto3.client('s3')
bucket = 'my-bucket-name'
startAfter = 'firstlevelFolder/secondLevelFolder'
theobjects = s3client.list_objects_v2(Bucket=bucket, StartAfter=startAfter )
for object in theobjects['Contents']:
print object['Key']
The output result for the code above would display the following:
firstlevelFolder/secondLevelFolder/item1
firstlevelFolder/secondLevelFolder/item2
Boto3 list_objects_v2 Documentation
In order to strip out only the directory name for secondLevelFolder
I just used python method split()
:
s3client = boto3.client('s3')
bucket = 'my-bucket-name'
startAfter = 'firstlevelFolder/secondLevelFolder'
theobjects = s3client.list_objects_v2(Bucket=bucket, StartAfter=startAfter )
for object in theobjects['Contents']:
direcoryName = object['Key'].encode("string_escape").split('/')
print direcoryName[1]
The output result for the code above would display the following:
secondLevelFolder
secondLevelFolder
If you'd like to get the directory name AND contents item name then replace the print line with the following:
print "{}/{}".format(fileName[1], fileName[2])
And the following will be output:
secondLevelFolder/item2
secondLevelFolder/item2
Hope this helps