使用AWS CLI批量上传S3文件时出现XAmzContentSHA256Mismatch错误求助
问题:S3批量上传出现XAmzContentSHA256Mismatch错误
执行以下AWS CLI命令批量上传本地文件至S3存储桶:
aws --profile 1prof --endpoint-url http://1.1.1.1:80 s3 --recursive cp /tmp/_out/1prof/tbl/ s3://1prof/tbl/tbl20231025/
部分文件上传成功,但部分文件出现如下错误:
upload failed: ./output-000003.csv to s3://1prof/tbl/tbl20231025/output-000003.csv An error occurred (XAmzContentSHA256Mismatch) when calling the CompleteMultipartUpload operation: The provided 'x-amz-content-sha256' header does not match what was computed. upload failed: ./output-000001.csv to s3://1prof/tbl/tbl20231025/output-000001.csv An error occurred (XAmzContentSHA256Mismatch) when calling the CompleteMultipartUpload operation: The provided 'x-amz-content-sha256' header does not match what was computed. upload failed: ./output-000011.csv to s3://1prof/tbl/tbl20231025/output-000011.csv An error occurred (XAmzContentSHA256Mismatch) when calling the CompleteMultipartUpload operation: The provided 'x-amz-content-sha256' header does not match what was computed. upload failed: ./output-000012.csv to s3://1prof/tbl/tbl20231025/output-000012.csv An error occurred (XAmzContentSHA256Mismatch) when calling the CompleteMultipartUpload operation: The provided 'x-amz-content-sha256' header does not match what was computed.
错误原因
- 上传期间本地文件被修改:AWS CLI预先计算了文件的SHA256哈希值,但上传过程中文件内容被编辑、覆盖或更新,导致S3收到的实际内容哈希和CLI提供的不一致
- 网络传输数据损坏:不稳定的网络导致分片传输时出现丢包或数据篡改,使得S3端计算的哈希与本地不匹配
- 中间设备篡改内容:上传路径中的代理、防火墙或CDN等设备修改了文件内容,破坏了哈希校验的一致性
- AWS CLI版本缺陷:旧版本CLI在多部分上传的哈希计算逻辑上存在bug,导致校验失败
解决方法
- 确保上传期间源文件无修改:关闭所有正在写入这些CSV文件的程序,避免上传过程中文件内容变更
- 校验本地文件完整性:重新计算失败文件的SHA256哈希,确认文件本身未损坏,执行命令:
sha256sum /tmp/_out/1prof/tbl/output-000003.csv - 单独重试失败文件:针对上传失败的文件单独执行上传命令,示例:
aws --profile 1prof --endpoint-url http://1.1.1.1:80 s3 cp /tmp/_out/1prof/tbl/output-000003.csv s3://1prof/tbl/tbl20231025/ - 绕过中间代理(若存在):如果上传经过代理设备,尝试直接连接S3端点,或配置代理允许完整传输文件不做修改
- 更新AWS CLI至最新版本:修复旧版本的已知bug,执行对应更新命令:
# pip安装方式 pip install --upgrade awscli # 系统包管理(以Ubuntu为例) sudo apt update && sudo apt install --only-upgrade awscli - 临时禁用多部分上传:添加
--no-multipart参数强制使用单部分上传,避免多部分哈希校验问题,命令示例:aws --profile 1prof --endpoint-url http://1.1.1.1:80 s3 --recursive cp --no-multipart /tmp/_out/1prof/tbl/ s3://1prof/tbl/tbl20231025/
内容的提问来源于stack exchange,提问作者Dennis
相关产品推荐
相关产品推荐

