Trista_456
Trista_456

Reputation: 117

Python- Get AWS Bucket Key from Path

Given that I have something like this:

"s3://folder1/folder2/folder3/folder4/folder4.5/folder4.6/folder5/folder6/folder7/file_name.csv"

How do I parse it out so that I get the Bucket and then the Key as everything minus the last/ with the filename?

I am trying to get the path so that I can list all the files within the path.

Upvotes: 0

Views: 1642

Answers (1)

Marcin
Marcin

Reputation: 238189

One way would be to use simple split and join combo:

s3_path = "s3://folder1/folder2/folder3/folder4/folder4.5/folder4.6/folder5/folder6/folder7/file_name.csv"

s3_path_split = s3_path.split('/')

bucket_name = s3_path_split[2]

# 'folder1'

key_name = '/'.join(s3_path_split[3:])

# 'folder2/folder3/folder4/folder4.5/folder4.6/folder5/folder6/folder7/file_name.csv'

key_name_without_file = '/'.join(s3_path_split[3:-1])

# folder2/folder3/folder4/folder4.5/folder4.6/folder5/folder6/folder7'

Upvotes: 1

Related Questions