0
votes

I have this:

with open('my_file', 'r') as f_in:
    for i in f_in:
        response = s3.head_bucket(Bucket='i')
        print(response)

I expect to get bucket properties for those buckets that are in my_file but instead I get:

botocore.exceptions.ClientError: An error occurred (403) when calling the HeadBucket operation: Forbidden

If I just put one bucket name it also fails

If I hardcode the bucket name "(Bucket='my-bucketoweiruowi')", it works!

If I get rid of the for loop:

var = 'my-bucketoweiruow'
response = s3.head_bucket(Bucket='var')

... it fails with the same 403 error

I removed the '' like this:

with open('my_file', 'r') as f_in:
    for i in f_in:
        response = s3.head_bucket(Bucket=i)
        print(response)

...but I get this other error:

botocore.exceptions.ClientError: An error occurred (400) when calling the HeadBucket operation: Bad Request".

I have 2 separate aws accounts with different buckets for testing. Same behavior.

Looks like when going into the loop it... breaks?

1
you are calling the bucket name 'line', that is not a variable. If it is a string variable, you wouldn't need '.Lamanus
TY. I replaced "line" for "i" and removed the ''. Now I get "botocore.exceptions.ClientError: An error occurred (400) when calling the HeadBucket operation: Bad Request". Looks like when going into the loop it... breaks?HelloWorld

1 Answers

0
votes

Looks like when 'my_file' is being read, for some reason, when jumping to the next line, a a blank line is being added in between bucket names.

That means that if I print the content using a for loop, I get this output:

bucket1

bucket2

bucket3

So if I remove such blank lines, the thing works fine:

with open('test','r') as f:
    for i in f:
        a = i.rstrip('\n')
        res = s3.head_bucket(Bucket=a)
        print(res)

Conclusion: looks like the failure occurs when the function finds a blank line.