I tried to set the credentials using boto3.setup_default_session as described in the s3pathdocs. 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
Problem
I tried to set the credentials using
boto3.setup_default_session
as described in thes3path
docs. However, it seems thats3path
ignores that setting. I get aNoCredentialsError
, while usingboto3
directly works.Code
Workaround
Set credentials via environment variables before importing
boto3
: