修复以下代码的所有变量都会返回异常“SignatureDoesNotMatch”,仅当它尝试返回早期字节范围(例如字节=0-1000)时。如果范围更高(例如字节=400000-500000),则不会发生异常
import sys
import boto3
import botocore
from boto3.session import Session, Config
def transfer(bucket, access_key, secret_key, file_name, byte_range):
src_s3 = boto3.client('s3',
verify=False,
aws_access_key_id = access_key,
aws_secret_access_key = secret_key,
config = Config(signature_version='s3v4')
)
try:
print('Now will start reading using byte_range:', byte_range)
part_object = src_s3.get_object(Bucket=bucket, Key=file_name, Range=byte_range)
print('Done reading byte_range:', byte_range)
except Exception as e:
print('Failed reading byte_range:', byte_range)
print(e)
raise
if __name__ == "__main__":
bucket = sys.argv[1]
access_key = sys.argv[2]
secret_key = sys.argv[3]
file_name = sys.argv[4]
byte_range = sys.argv[5]
transfer(bucket, access_key, secret_key, file_name, byte_range)
注意:该问题并非在所有环境中都可重现,但在我部署到的环境(Pivotal Cloud Foundry)上是一致的。
故障排除,我推测在计算签名时存在混淆boto3的环境因素。关于 boto3 - Pivotal Cloud Foundry 交互可能导致此异常的任何想法?
如果可能的话,您可以指出我可以理解 boto3 如何验证签名的方向。
例外
2018-12-17T14:52:07.710-05:00 [APP/TASK/7dbf1c0d/0] [ERR] Traceback (most recent call last):
2018-12-17T14:52:07.710-05:00 [APP/TASK/7dbf1c0d/0] [ERR] File "download_part.py", line 32, in <module>
2018-12-17T14:52:07.710-05:00 [APP/TASK/7dbf1c0d/0] [ERR] transfer(bucket, access_key, secret_key, file_name, byte_range)
2018-12-17T14:52:07.710-05:00 [APP/TASK/7dbf1c0d/0] [ERR] File "download_part.py", line 16, in transfer
2018-12-17T14:52:07.710-05:00 [APP/TASK/7dbf1c0d/0] [ERR] part_object = src_s3.get_object(Bucket=bucket, Key=file_name, Range=byte_range)
2018-12-17T14:52:07.710-05:00 [APP/TASK/7dbf1c0d/0] [ERR] File "/home/vcap/deps/0/python/lib/python3.5/site-packages/botocore/client.py", line 320, in _api_call
2018-12-17T14:52:07.710-05:00 [APP/TASK/7dbf1c0d/0] [ERR] return self._make_api_call(operation_name, kwargs)
2018-12-17T14:52:07.710-05:00 [APP/TASK/7dbf1c0d/0] [ERR] File "/home/vcap/deps/0/python/lib/python3.5/site-packages/botocore/client.py", line 624, in _make_api_call
2018-12-17T14:52:07.711-05:00 [APP/TASK/7dbf1c0d/0] [ERR] raise error_class(parsed_response, operation_name)
2018-12-17T14:52:07.711-05:00 [APP/TASK/7dbf1c0d/0] [ERR] botocore.exceptions.ClientError: An error occurred (SignatureDoesNotMatch) when calling the GetObject operation: The request signature we calculated does not match the signature you provided. Check your key and signing method.