mr. M
mr. M

Reputation: 31

Connecting to specific path in s3

I'm trying to connect to specific folder in s3 using python sample: s3://main_folder/sub_folder1/sub_folder2/

# want to enter sub_folder2 - s3://main_folder/sub_folder1/sub_folder2/
import boto3

def connect_to_s3():
    s3_cli = boto3.resourse('s3')
    bucket = s3_cli.Bucket('sub_folder2')
    for b in bucket.objects.all():
        print(b.key)

I get this error:

raise error_class(parsed_response, operation_name) botocore.errorfactory.NoSuchBucket: An error occurred (NoSuchBucket) when calling the ListObjects operation: The specified bucket does not exist

Upvotes: 1

Views: 963

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 270089

The error says: The specified bucket does not exist

This is because the s3_cli.Bucket('sub_folder2') wants the name of a Bucket, but you have provided the name of a folder and didn't tell it the name of the bucket.

If you wish to only perform operations on a subset of a bucket, you can use a filter with a prefix:

import boto3

def connect_to_s3():
    s3_cli = boto3.resourse('s3')
    bucket = s3_cli.Bucket('my-bucket')
    for b in bucket.objects.filter(Prefix='sub_folder1/sub_folder2/').all():
        print(b.key)

Upvotes: 2

Related Questions