将数据上传到 AWS S3 - EndpointConnectionError:无法连接到端点 URL:

问题描述

按照这个好的教程 https://medium.com/bilesanmiahmad/how-to-upload-a-file-to-amazon-s3-in-python-68757a1867c6 我编写了一个代码,目的是将文件上传到 S3 存储桶:

import boto3
from botocore.exceptions import NoCredentialsError

ACCESS_KEY = 'XXXXXXXXXXXXXXXXXXXXXXX'
SECRET_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'


def upload_to_aws(local_file,bucket,s3_file):
    s3 = boto3.client('s3',aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY)

    try:
        s3.upload_file(local_file,s3_file)
        print("Upload Successful")
        return True
    except FileNotFoundError:
        print("The file was not found")
        return False
    except NoCredentialsError:
        print("Credentials not available")
        return False

我指定的参数如下:

uploaded = upload_to_aws('path/to/my/file/test.csv','name-of-s3-bucket','test.csv')

当我现在尝试执行此操作时,出现以下错误

EndpointConnectionError: Could not connect to the endpoint URL: "https://name-of-s3-bucket.s3.amazonaws.com/test.csv"

是不是我指定的方式有误?

非常感谢!

解决方法

您的地区是否在 ~/.aws/config 文件中正确指定为 N.Virginia? 此错误的最常见原因是您的区域设置不正确。由于我看到它是标准的 .s3.amazonaws.com,您可能想检查您的地区是否在配置文件中设置为 us-east-1。