简体   繁体   English

如何使用 Python smart_open 模块通过服务器端加密写入 S3

[英]How to use Python smart_open module to write to S3 with server-side encryption

In older versions (<5.0.0) of the Python smart_open module you could write to an S3 location with server-side encryption like this:在 Python smart_open模块的旧版本 (<5.0.0) 中,您可以使用服务器端加密写入 S3 位置,如下所示:

import smart_open
writer = smart_open.open("s3://bucket-name/path/to/file.txt", 'w', transport_params={'multipart_upload_kwargs':{'ServerSideEncryption': 'AES256'}})
writer.write("nothing to see here\n")
writer.close()

However, the underlying mechanisms changed with version 5.0.0 (see here ).然而,底层机制在 5.0.0 版本中发生了变化(参见此处)。 The old way to doing it no longer works.这样做的旧方法不再有效。 It acts like I did not pass the encryption parameter:就像我没有传递加密参数一样:

ValueError: the bucket 'bucket-name' does not exist, or is forbidden for access (ClientError('An error occurred (AccessDenied) when calling the CreateMultipartUpload operation: Access Denied'))

I find the documentation to be confusing and insufficient to figure it out myself.我发现文档令人困惑,不足以自己弄清楚。 How can I write to S3 with encryption using newer versions of smart_open?如何使用较新版本的 smart_open 加密写入 S3?

Got a very quick and helpful response from the smart_open team.从 smart_open 团队得到了非常快速和有用的回复。 Here is how it works in smart_open version 5.0.0以下是它在 smart_open 版本 5.0.0 中的工作方式

import smart_open
client_kwargs = {'S3.Client.create_multipart_upload': {'ServerSideEncryption': 'AES256'}}
writer = smart_open.open("s3://bucket-name/path/to/file.txt", 'w', transport_params={'client_kwargs': client_kwargs})
writer.write("nothing to see here\n")
writer.close()

With that simple change it works as great as ever!有了这个简单的改变,它就和以前一样好用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM