You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用boto3上传CSV到S3遇SSLError,求解决方案

S3上传SSL证书验证失败解决办法

问题重现

上传CSV到S3的代码:

from boto3.s3.transfer import S3Transfer
import boto3
client = boto3.client('s3', aws_access_key_id=AWS_ACCESS_KEY_ID,aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
transfer = S3Transfer(client)
transfer.upload_file('C:\Users\AppData\Local\Workbook.csv', 'bucketname', 'folder in s3')

遇到的错误:

botocore.exceptions.SSLError: SSL validation failed for https://bucketname.s3.amazonaws.com/ [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)

解决办法

  • 指定可信CA证书路径
    多数情况是系统缺少AWS S3证书的信任链,可使用certifi库提供的标准CA证书集合,或直接指定系统CA证书文件路径。
    先安装certifi:

    pip install certifi
    

    修改代码:

    import boto3
    from boto3.s3.transfer import S3Transfer
    import certifi
    
    client = boto3.client(
        's3',
        aws_access_key_id=AWS_ACCESS_KEY_ID,
        aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
        verify=certifi.where()  # 调用certifi的CA证书
    )
    transfer = S3Transfer(client)
    # Windows路径注意转义,用双反斜杠
    transfer.upload_file('C:\\Users\\AppData\\Local\\Workbook.csv', 'bucketname', 'folder in s3')
    

    也可通过环境变量指定证书路径,无需修改代码:

    • Windows命令行:set AWS_CA_BUNDLE=C:\path\to\ca-bundle.crt
    • Linux/macOS终端:export AWS_CA_BUNDLE=/path/to/ca-bundle.crt
  • 临时禁用SSL验证(仅测试场景)
    若只是临时测试,可关闭SSL验证,但生产环境绝对禁止使用,会引发安全风险:

    client = boto3.client(
        's3',
        aws_access_key_id=AWS_ACCESS_KEY_ID,
        aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
        verify=False
    )
    
  • 检查网络代理配置
    若处于代理环境,代理服务器的证书可能未被系统信任,需将代理CA证书加入信任列表,或在client中直接指定代理及证书:

    client = boto3.client(
        's3',
        aws_access_key_id=AWS_ACCESS_KEY_ID,
        aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
        verify='/path/to/proxy-ca-cert.crt',
        config=boto3.client.Config(
            proxies={
                'https': 'http://your-proxy-address:port'
            }
        )
    )
    

内容的提问来源于stack exchange,提问作者user5062673

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 03:35:12