我无法弄清楚为什么文件,其中的内容是"DELETE ME LATER",其中加载了编码 utf-8
导致botocore中的异常被哈希时 .
with io.open('deleteme','r', encoding='utf-8') as f:
try:
resp=client.put_object(
Body=f,
Bucket='s3-bucket-actual-name-for-real',
Key='testing/a/put'
)
print('deleteme exists')
print(resp)
except:
print('deleteme could not put')
raise
生产环境 :
deleteme无法将Traceback(最近一次调用最后一次):文件“./test_operator.py”,第41行,在Key ='testing / a / put'File“/ Users / lamblin / VEnvs / awscli / lib / python3中 . 6 / site-packages / botocore / client.py“,第312行,在_api_call中返回self._make_api_call(operation_name,kwargs)文件”/Users/lamblin/VEnvs/awscli/lib/python3.6/site-packages/botocore/ client.py“,第582行,_make_api_call request_signer = self._request_signer,context = request_context)文件”/Users/lamblin/VEnvs/awscli/lib/python3.6/site-packages/botocore/hooks.py“,第242行,在emit_until_response answers = self._emit(event_name,kwargs,stop_on_response = True)文件“/Users/lamblin/VEnvs/awscli/lib/python3.6/site-packages/botocore/hooks.py”,第210行,在_emit中response = handler(** kwargs)file“/Users/lamblin/VEnvs/awscli/lib/python3.6/site-packages/botocore/handlers.py”,第201行,在conditionally_calculate_md5 calculate_md5(params,** kwargs)文件“/Users/lamblin/VEnvs/awscli/lib/python3.6/site-packages/botocore/handlers.py “,第179行,在calculate_md5中,binary_md5 = _calculate_md5_from_file(正文)文件”/Users/lamblin/VEnvs/awscli/lib/python3.6/site-packages/botocore/handlers.py“,第193行,在_calculate_md5_from_file中md5.update( chunk)TypeError:必须在散列之前对Unicode对象进行编码
现在这可以通过使用'rb'打开文件来避免,但是,文件对象 f
显然不是使用编码吗?
1 回答
在
mode='r'
中为io.open
指定的编码用于解码内容 . 因此,当您迭代f
时,内容已经由Python从bytes
转换为str
(文本) .要直接与
botocore
连接,请使用模式'rb'
打开文件,然后删除编码kwarg
. 当为了传输内容而必须做的第一件事就是再次编码回字节时,没有必要将它解码为文本 .