繁体   English   中英

使用Python的boto,如何获取加密文件的内容?

[英]With Python's boto, how can I get the contents of an encrypted file?

对于未加密的文件,我可以执行以下操作:

key = s3_bucket.get_key(path)
value_as_string = key.get_contents_as_string()

但是,如果文件已加密,则需要对此进行更改。 我无法从阅读文档中找出答案。 我要改变什么?

我知道主对称密钥,它是一个像30个字符左右的字符串。

目前,尚无Boto版本在其API中支持客户端提供的密钥。 相反,您可以使用AWS开发工具包。

一般过程是这样的:

下载对象时–客户端首先从Amazon S3下载加密的对象以及元数据。 客户端使用元数据中的材料描述,首先确定要使用哪个主密钥来解密加密的数据密钥。 客户端使用该主密钥来解密数据密钥,并使用它来解密对象。 http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingClientSideEncryption.html

这是一个使用Java的示例,该示例显示密钥的创建,使用客户端密钥加密的文件的上载,以及使用客户端密钥解密的文件的检索:

KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA");
keyGenerator.initialize(1024, new SecureRandom());
KeyPair myKeyPair = keyGenerator.generateKeyPair();

// Construct an instance of AmazonS3EncryptionClient
AWSCredentials credentials = new BasicAWSCredentials(myAccessKeyId, mySecretKey);
EncryptionMaterials encryptionMaterials = new EncryptionMaterials(myKeyPair);
AmazonS3EncryptionClient s3 = new AmazonS3EncryptionClient(credentials, encryptionMaterials);

// Then just use the S3 client as normal...
//
// When we use the putObject method, the data in the file or InputStream
// we specify is automatically encrypted on the fly as it's uploaded to Amazon S3.
s3.putObject(bucketName, key, myFile);

// When you use the getObject method, the data retrieved from Amazon S3
// is automatically decrypted on the fly.
S3Object downloadedObject = s3.getObject(bucketName, key);

从此处使用/改编的来源: http : //aws.amazon.com/articles/2850096021478074

在Python中,不存在任何AWS“受支持”的.eg Boto选项。 但是,确实存在Boto的包装器库,该库提供了使用客户端侧键进行加密/解密的功能,该库为: https : //pypi.python.org/pypi/s3-encryption/0.1.0

如果这是AWS为您加密的文件(通过在boto中设置标志以打开加密),则您无需执行任何其他操作。 S3透明地加密和解密内容。 它们在S3内部进行了加密,但是在您的程序中是明确的。 因此,无论哪种方式,相同的代码都可以工作。

暂无
暂无

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

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