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

使用boto3向moto本地S3存储桶子路径上传文件报404错误

编辑:此问题已关闭,彻底卸载并重新安装moto后问题解决。

S3带斜杠键上传404错误排查

当前环境:boto3==1.28.45 + moto==1.3.14,本地Moto服务运行在5353端口。

该功能此前稳定运行多年,但仅在我的机器上,向已创建的存储桶上传带斜杠的键(即类似“子目录”的路径,实际为S3键值存储的键)时,会触发404错误。

不清楚从何处入手调试,寻求排查思路。

补充编辑:我明确知晓这并非真正的“子目录”,而是键值存储结构,但所有带斜杠的路径均上传失败。

测试代码

(py-3.8.13) % cat test.txt
test
(py-3.8.13) % python
Python 3.8.13 (default, Aug 26 2022, 14:51:46) 
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto3
>>> client = boto3.client('s3', region_name='us-east-1', endpoint_url='http://127.0.0.1:5353')
>>> client.create_bucket(Bucket='test')
{'ResponseMetadata': {'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Werkzeug/2.3.5 Python/3.8.13', 'date': 'Tue, 12 Sep 2023 19:17:47 GMT', 'content-type': 'text/html; charset=utf-8', 'content-length': '158', 'connection': 'close'}, 'RetryAttempts': 0}}
>>> client.upload_file('test.txt', 'test', 'test.txt')
>>> client.upload_file('test.txt', 'test', 'test_dir/test.txt')
Traceback (most recent call last):
   ... [ 省略部分内容 ]
botocore.exceptions.ClientError: An error occurred (404) when calling the PutObject operation: Not Found

额外尝试操作

>>> resource = boto3.resource('s3', region_name='us-east-1', endpoint_url='http://127.0.0.1:5353')
>>> bucket = resource.Bucket('test')
>>> bucket.upload_file('test.txt', 'test.txt')
>>> bucket.upload_file('test.txt', 'test_dir/test.txt')
Traceback (most recent call last):
...
botocore.exceptions.ClientError: An error occurred (404) when calling the PutObject operation: Not Found
>>> bucket.put_object(Key='test_dir/')
>>> list(bucket.objects.iterator())
[s3.ObjectSummary(bucket_name='test', key='test.txt'), s3.ObjectSummary(bucket_name='test', key='test_dir/')]
>>> bucket.upload_file('test.txt', 'test_dir/test.txt')
Traceback (most recent call last):
...
botocore.exceptions.ClientError: An error occurred (404) when calling the PutObject operation: Not Found

Moto服务日志

127.0.0.1 - - [12/Sep/2023 12:28:15] "PUT /test/test_dir/test.txt HTTP/1.1" 404 -

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 20:05:33