由于编码,botocore s3 put 存在散列文件问题?

dla*_*lin 6 python amazon-s3 botocore

我无法弄清楚为什么文件,其内容为“稍后删除”,该文件加载了编码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
Run Code Online (Sandbox Code Playgroud)

产生:

deleteme 无法放置
回溯(最近一次调用最后一次):文件
“./test_operator.py”,第 41 行,在
Key='testing/a/put' 文件“/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
响应= 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) 文件“/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-包/botocore/handlers.py”,
第 179 行,在 calculate_md5
binary_md5 = _calculate_md5_from_file(body) 文件“/Users/lamblin/VEnvs/awscli/lib/python3.6/site-packages/botocore/handlers.py”,
第 193 行, in _calculate_md5_from_file md5.update(chunk)
TypeError: Unicode-objects must be encoding before hashing

现在可以通过使用“rb”打开文件来避免这种情况,但是文件对象不是f清楚地使用了编码吗?

wim*_*wim 4

现在可以通过使用“rb”打开文件来避免这种情况,但是文件对象 f 不是明确使用了编码吗?

io.openin中指定的编码mode='r'用于解码内容。所以当你迭代时f,内容已经被Python转换为bytesstr文本)。

要直接交互botocore,请使用 mode 打开文件'rb',然后删除编码kwargbotocore当为了传输内容而必须做的 第一件事只是再次编码回字节时,将其解码为文本是没有意义的。