簡體   English   中英

如何將 s3 文件作為 GPG 加密的輸入,同時使用 Python 將加密數據流回 s3?

[英]How to stream s3 file as input for GPG encryption and meanwhile stream the encrypted data back to s3 using Python?

我在 aws s3 中有一個大文件。 我想使用 Python 將 s3 文件流作為 GPG 加密的輸入,同時,將加密數據流回 s3,直到加密完成。 這樣加密的數據就不會炸毀內存。

流程應該是:(s3 stream) -> (GPG Encrption Stream) -> (s3 stream)。

我試過下面的代碼,但 gnupg 庫不支持流體作為輸入。 有沒有另一種方法可以做到這一點?

import boto3
import gnupg
gpg = gnupg.GPG()
imported_keys = gpg.import_keys(key_data)
s3_object = s3.get_object(Bucket=Bucket, Key=Key)['Body']
encrypted_data = gpg.encrypt(s3_object, armor=False, always_trust=True, recipients = imported_keys.fingerprints)

我也試過下面的代碼,但是s3中沒有輸出文件。

from smart_open import open
gpg.encrypt_file(open('s3://Bucket/key/test.txt','rb'), armor=False, always_trust=True, recipients = imported_keys.fingerprints, output='s3://Bucket/key/test.gpg')

使用 gpg 的 on_data 功能

import gnupg
import s3fs

gpg = gnupg.GPG()
imported_keys = gpg.import_keys(key_data)
file_input = 's3://Bucket/key/test.txt'
file_output = 's3://Bucket/key/test.txt.gpg'
fs = s3fs.S3FileSystem(anon=False)
class Capturer(object):
    def __init__(self, file_output, fs):
        self.fout = fs.open(file_output, 'wb')

    def __call__(self, data):
        if data:
            self.fout.write(data)
        else:
            self.fout.close()
        return False


capturer = Capturer(file_output, fs)
gpg.on_data = capturer

with fs.open(file_input, 'rb') as fin:
    status = gpg.encrypt_file(fin,armor=False, always_trust=True, recipients = imported_keys.fingerprints)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM