簡體   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