s3 urls - get bucket name and path

后端 未结 7 815
南方客
南方客 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:53

    For those who like me was trying to use urlparse to extract key and bucket in order to create object with boto3. There's one important detail: remove slash from the beginning of the key

    from urlparse import urlparse
    o = urlparse('s3://bucket_name/folder1/folder2/file1.json')
    bucket = o.netloc
    key = o.path
    boto3.client('s3')
    client.put_object(Body='test', Bucket=bucket, Key=key.lstrip('/'))
    

    It took a while to realize that because boto3 doesn't throw any exception.

提交回复
热议问题