无法通过boto3访问AWS S3桶,遇EndpointConnectionError求助
Hey there! Let's work through that EndpointConnectionError you're facing when trying to access AWS S3 with boto3. This error usually points to a problem connecting to the S3 service endpoint, so here are the key checks and fixes to try:
1. Verify Network Connectivity
First, rule out basic network issues. Your machine might be blocked from reaching the S3 endpoint by a firewall, corporate proxy, or internet restriction.
- Test connectivity directly with a command like:
If this fails, you'll need to check your network settings or reach out to your IT team to allow traffic to AWS S3 endpoints.curl -v https://s3.amazonaws.com
2. Double-Check Region Configuration
Even if you added region_name, make sure it's correctly set to match where your S3 buckets are hosted (or the default region for your account). For example:
- If your buckets are in
us-east-1(the default S3 region), explicitly set it:client_access = client( 's3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY, region_name='us-east-1' ) - For non-default regions (like
ap-southeast-1), boto3 should auto-resolve the correct endpoint, but if issues persist, you can explicitly specify the endpoint URL:client_access = client( 's3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY, region_name='ap-southeast-1', endpoint_url='https://s3.ap-southeast-1.amazonaws.com' )
3. Validate Your AWS Credentials
It's easy to accidentally load incorrect credentials. Let's confirm they're being read properly:
- Add print statements to check the values loaded from your CSV:
Ensure there are no extra spaces or newline characters in the keys.print("Access Key:", ACCESS_KEY.strip()) print("Secret Key:", SECRET_KEY.strip()) - Also, head to the AWS IAM Console to verify:
- The user is in a group with the
AmazonS3FullAccesspolicy attached. - The access keys you're using are active (not revoked or expired).
Note: IAM policy changes can take 5-10 minutes to take effect, so if you just added the policy, wait a bit before retrying.
- The user is in a group with the
4. Check for Proxy Settings
If you're working behind a corporate proxy, boto3 needs proxy configuration to reach the internet. You can add proxy details directly to the client:
client_access = client( 's3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY, region_name='us-east-1', proxies={ 'http': 'http://your-proxy-server:port', 'https': 'http://your-proxy-server:port' } )
Alternatively, set the HTTP_PROXY and HTTPS_PROXY environment variables before running your script.
5. Try Using a Boto3 Session
Sometimes initializing via a session instead of directly creating a client can resolve subtle configuration issues:
import boto3 session = boto3.Session( aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY, region_name='us-east-1' ) s3_client = session.client('s3') s3_client.list_buckets()
Start with the network and credential checks first—those are the most common culprits for this error!
内容的提问来源于stack exchange,提问作者Arnab Mukherjee

