0

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.

Marcin
  • 215,873
  • 14
  • 235
  • 294
Trista_456
  • 117
  • 10
  • Does this answer your question? [s3 urls - get bucket name and path](https://stackoverflow.com/questions/42641315/s3-urls-get-bucket-name-and-path) – Dunedan Jun 17 '20 at 04:54

1 Answers1

1

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'

Marcin
  • 215,873
  • 14
  • 235
  • 294