liormizr / s3path

s3path is a pathlib extension for AWS S3 Service
Apache License 2.0
208 stars 40 forks source link

Configuration example not working: NoCredentialsError #87

Closed StefanBrand closed 2 years ago

StefanBrand commented 3 years ago

Problem

I tried to set the credentials using boto3.setup_default_session as described in the s3path docs. However, it seems that s3path ignores that setting. I get a NoCredentialsError, while using boto3 directly works.

Code

import boto3
from s3path import S3Path

AWS_ACCESS_KEY_ID = "..."
AWS_SECRET_ACCESS_KEY = "..."

boto3.setup_default_session(
    region_name='eu-central-1',    
    aws_access_key_id=AWS_ACCESS_KEY_ID,    
    aws_secret_access_key=AWS_SECRET_ACCESS_KEY
)

# works fine
s3 = boto3.client('s3')
for obj in s3.list_objects_v2(Bucket="my-bucket" Prefix="2021/")['Contents']:
    print(obj['Key'])
    break

# NoCredentialsError
bucket_path = S3Path('/my-bucket/2021/')
for p in bucket_path.iterdir():
    print(p)
    break

Workaround

Set credentials via environment variables before importing boto3:

import os

os.environ["AWS_ACCESS_KEY_ID"] = "..."
os.environ["AWS_SECRET_ACCESS_KEY"] = "..."

import boto3
from s3path import S3Path
liormizr commented 3 years ago

Thanks for the issue, Checking

liormizr commented 3 years ago

PR: #92 This fix will be merged to the next version

liormizr commented 2 years ago

Deployed in version 0.3.3