s3 urls - get bucket name and path

后端 未结 7 826
南方客
南方客 2020-12-13 23:20

I have a variable which has the aws s3 url

s3://bucket_name/folder1/folder2/file1.json

I want to get the bucket_name in a variables and re

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 23:48

    This is a nice project:

    s3path is a pathlib extention for aws s3 service

    >>> from s3path import S3Path
    >>> path = S3Path.from_uri('s3://bucket_name/folder1/folder2/file1.json')
    >>> print(path.bucket)
    '/bucket_name'
    >>> print(path.key)
    'folder1/folder2/file1.json'
    >>> print(list(path.key.parents))
    [S3Path('folder1/folder2'), S3Path('folder1'), S3Path('.')]
    

提交回复
热议问题