这有点类似于之前提出的这个问题:gnupg - decrypt into Python bytesio stream
我需要从GCP云存储中流式传输文件,并使用GPG在流中对其进行加密,同时将加密后的数据上传到GCP云存储。这个是可能的吗?
阅读API文档(https://googleapis.dev/python/storage/latest/blobs.html),我可以使用以下命令流式传输文件:
from google.cloud import storage
client = storage.Client()
bucket = client.bucket("bucket-name")
blob = bucket.get_blob("blob-name.txt")
with blob.open("rt") as f:
print(f.read())我可以使用以下命令对本地文件进行加密:
with open('Test.txt', 'rb') as f:
status = gpg.encrypt_file(
f,sign=public_key_fingerprint,
recipients=private_key_recipient,
passphrase=private_key_pass,
always_trust=True,
output = output_file
)在加密过程中,我可以使用以下命令将GCP文件流式传输到本地.gpg文件:
with blob.open("rb") as f:
status = gpg.encrypt_file(
f,sign=public_key_fingerprint,
recipients=private_key_recipient,
passphrase=private_key_pass,
always_trust=True,
output = output_file
)但我需要的输出是云存储中的文件,而不是本地系统。
我试过了:
bucket = storage_client.bucket(input_bucket)
blob = bucket.get_blob(input_file)
uploadbucket = storage_client.bucket(output_bucket)
uploadblob = bucket.blob(output_file)
with blob.open("rt") as f:
with uploadblob.open("wt") as en:
status = gpg.encrypt_file(
f,sign=public_key_fingerprint,
recipients=private_key_recipient,
passphrase=private_key_pass,
always_trust=True,
output = en
)但这给了我一个错误:
path should be string, bytes, os.PathLike or integer, not TextIOWrapper在这方面的任何帮助都将非常感谢!
发布于 2021-05-28 17:36:09
问题出在gpg.encrypt_file(..., output=en)调用中。output参数必须是包含内容目标的字符串。
您可以改用gpg.encrypt(),然后将内容写入blob,如下所示:
with blob.open("rt") as f:
with uploadblob.open("wt") as en:
encrypted = gpg.encrypt(
f,sign=public_key_fingerprint,
recipients=private_key_recipient,
passphrase=private_key_pass,
always_trust=True,
armor=True)
en.write(encrypted)(我显式地使用了armor=True来记住输出是ascii装甲输出,而不是二进制)。
https://stackoverflow.com/questions/67723303
复制相似问题