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

如何使用aiohttp结合requests_aws4auth实现异步AWS S3文件上传

问题描述

我有一段用requests_aws4auth向AWS S3存储桶上传文件的同步代码,想要改用aiohttp实现异步处理,但发现aiohttp仅支持BasicAuth的元组形式认证参数,无法直接兼容AWS4Auth认证方式。

可用的同步代码:

self.auth = AWS4Auth(access_key, secret_key, 'us-east-1', 's3')
response = requests.put(
    url,
    auth=self.auth,
    data=content)

期望的异步代码(无法直接运行):

async with aiohttp.ClientSession(url=url, auth=self.auth) as session:
    async with session.put(data=content) as resp:
        await session.close()
解决方案

方法1:手动生成AWS4认证头,配合aiohttp发送请求

requests_aws4auth的AWS4Auth类可以生成请求所需的认证头,我们可以先构造请求的核心参数,调用它的__call__方法生成认证头,再传递给aiohttp使用:

import aiohttp
from requests_aws4auth import AWS4Auth

async def async_upload_to_s3(url, access_key, secret_key, region, service, content):
    auth = AWS4Auth(access_key, secret_key, region, service)
    # 定义请求方法、目标URL、初始空headers
    method = "PUT"
    headers = {}
    # 调用AWS4Auth生成认证头
    auth_headers = auth(method, url, headers=headers, data=content)
    # 合并初始headers与认证头
    final_headers = {**headers, **auth_headers}
    
    async with aiohttp.ClientSession() as session:
        async with session.put(url, headers=final_headers, data=content) as resp:
            return await resp.text()

方法2:使用异步AWS SDK(推荐)

如果只是针对S3进行操作,更推荐使用aiobotocore——AWS官方SDK的异步版本,它原生支持AWS认证,无需手动处理签名逻辑:

import aiobotocore

async def async_upload_with_aiobotocore(bucket_name, key, content, access_key, secret_key, region):
    session = aiobotocore.get_session()
    async with session.create_client(
        "s3",
        region_name=region,
        aws_access_key_id=access_key,
        aws_secret_access_key=secret_key
    ) as client:
        await client.put_object(Bucket=bucket_name, Key=key, Body=content)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 21:12:39