我正在尝试从 azure blob 存储读取 csv 文件(加密)并使用 gnupg 解密并在 python 中读取它。我能够访问 blob 文件,但是当我将其传递给 dcrypt 函数时,它会抛出错误。
错误:需要 str、bytes 或 os.PathLike 对象,而不是 StorageStreamDownloader
blob文件是StorageStreamDownloader类型。当我将其转换为字节时,我收到“嵌入空字节”错误。
有人可以帮我解决这个问题吗?下面是我的代码。
from azure.storage.blob import BlobServiceClient, BlobClient
import pandas as pd
import csv
from io import StringIO
from pyspark.sql import SparkSession
import io
connection_string = "AAAA"
container_name = "BBBB"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(container_name)
import gnupg
gpg = gnupg.GPG()
gpg.encoding = 'utf-8'
passphrase = "12345"
secret = "112233"
def decrypt_file(filename, secret, passphrase):
gpg.encoding = 'utf-8'
with open(filename, 'rb') as f:
decrypted_data = gpg.decrypt(f, passphrase=passphrase)
if decrypted_data.ok:
print("done")
else:
print("error:", decrypted_data.status)
print("error:", decrypted_data.stderr)
return str(decrypted_data)
blob_client = container_client.get_blob_client(file)
blob_file_tinb = blob_client.download_blob()
tinb_file = decrypt_file(blob_file_tinb,secret,passphrase)
从 azure blob 存储中读取 csv 文件(加密)并使用 gnupg 解密并在 python 中读取。
您可以使用下面的代码使用 Azure Python SDK 从 Azure Blob 存储读取 CSV 文件(加密到解密):
代码:
from azure.storage.blob import BlobServiceClient
import io
import gnupg
connection_string = "xxxxx"
container_name = "logs"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(container_name)
gpg = gnupg.GPG()
gpg.encoding = 'utf-8'
passphrase = "12344"
secret="12344"
def decrypt_file(file, secret, passphrase):
gpg.encoding = 'utf-8'
decrypted_data = gpg.decrypt(file.read(), passphrase=passphrase)
if decrypted_data.ok:
print("done")
else:
print("error:", decrypted_data.status)
print("error:", decrypted_data.stderr)
return str(decrypted_data)
blob_client = container_client.get_blob_client("<filename>")
blob_data = blob_client.download_blob().readall()
tinb_file = decrypt_file(io.BytesIO(blob_data), passphrase, secret)
print(tinb_file)
上面的代码从 Azure Blob 存储下载加密文件,使用
GnuPG
对其进行解密,并打印解密的数据。
它设置
connection string
、container name
,并创建 BlobServiceClient 和 ContainerClient 对象。它还设置 GnuPG 对象和密码来解密文件。最后,它获取加密文件的BlobClient对象,下载文件数据,并将其传递给decrypt_file()函数来解密并打印数据。
输出:
done
Industry
Accounting/Finance
Advertising/Public Relations
Aerospace/Aviation
Arts/Entertainment/Publishing
Automotive
Banking/Mortgage
Business Development
Business Opportunity
Clerical/Administrative
Construction/Facilities