Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access S3 bucket from url using boto3?

I have been given access to an S3 bucket:

S3 base path : s3://folder1/folder2/*

with an IAM user account:

arn:aws:iam::< Account >:user/< username >

I've tried the following but no luck.

import boto3
client = boto3.client(
    's3',
    aws_access_key_id='****',
    aws_secret_access_key='****'
)
obj1 = client.get_object("folder1/folder2/*") #TypeError
obj2 = boto3.resource("folder1/folder2/*") # DataNotFoundError

Any help regarding this would be appreciated. Thanks!

like image 917
piby190 Avatar asked Mar 20 '16 14:03

piby190


1 Answers

s3 path consists of bucket and object in the form:

s3://<Bucket>/<Key>

You can use the following expression to split your "s3_key" into bucket and key:

bucket, key = s3_key.split('/',2)[-1].split('/',1)

So to access object from the path s3://folder1/folder2 you would do the following:

import boto3
client = boto3.client('s3')
client.get_object(Bucket='folder1', Key='folder2')
like image 150
Vor Avatar answered Sep 25 '22 19:09

Vor