python - 无法使用 python 代码从存储容器上传/下载文件
问题描述
这是我们使用的python代码,
这些文件将被访问,然后上传到存储容器。
def az_upload_blob(tenantID, container_name, file_name, data):
try:
logger.debug("Info::Acessing uBlob.")
AZURE_STORAGE_CONNECTION_STRING = az_kv_getsecret(Con.KV_RINGR_URI, Con.KV_SEC_CONN_STRING)
blob_service_client = BlobServiceClient.from_connection_string(AZURE_STORAGE_CONNECTION_STRING)
blob_client = blob_service_client.get_blob_client(container=container_name, blob=file_name)
blob_client.upload_blob(data)
logger.debug("Info:: Blob uploaded")
except Exception as ex:
logger.error(f"uBlob:: {ex}")
raise Exception(f"AZ-uBlob-Exception: {ex}")
错误信息:
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:23025256-801e-0047-3150-3b8ba5000000
Time:2021-04-27T10:34:18.1487489Z
ErrorCode:AuthenticationFailed
Error:None
AuthenticationErrorDetail:The MAC signature found in the HTTP request 'xyyz' is not the same as any computed signature. Server used following string to sign: 'PUT
636
application/octet-stream
*
x-ms-blob-type:BlockBlob
x-ms-client-request-id:1ebf24c8-a744-11eb-be9d-000d3a99de90
x-ms-date:Tue, 27 Apr 2021 10:34:18 GMT
x-ms-encryption-algorithm:AES256
x-ms-version:2020-06-12
请帮助解决丢失的项目。谢谢!
解决方案
更新:
我使用下面的代码,它工作正常:
如果我运行 python 脚本,我首先运行“az login”,然后运行:
from azure.keyvault.secrets import SecretClient
from azure.identity import AzureCliCredential
from azure.storage.blob import BlobServiceClient
def az_upload_blob(KVUri,secretName, container_name, file_name, data):
try:
AZURE_STORAGE_CONNECTION_STRING = az_kv_getsecret(KVUri, secretName)
blob_service_client = BlobServiceClient.from_connection_string(AZURE_STORAGE_CONNECTION_STRING)
blob_client = blob_service_client.get_blob_client(container=container_name, blob=file_name)
blob_client.upload_blob(data)
except Exception as ex:
print("Some Exception.")
def az_kv_getsecret(KVUri,secretName):
credential = AzureCliCredential()
client = SecretClient(vault_url=KVUri, credential=credential)
retrieved_secret = client.get_secret(secretName)
print(retrieved_secret.value)
return retrieved_secret.value
KVUri = "https://bowmantest.vault.azure.net/"
secretName = "STR"
container_name = "test"
file_name = "0505test.txt"
data = "This is 0505test.txt"
az_upload_blob(KVUri=KVUri,secretName=secretName,container_name=container_name,file_name=file_name,data=data)
print("This is OK.")
如果我在 azure 函数中使用它们,我只需将凭据更改为“ManagedIdentityCredential”并将函数应用程序的访问策略添加到 azure key vault(在我这边,我提供完全访问权限。)。
from azure.keyvault.secrets import SecretClient
from azure.identity import ManagedIdentityCredential
from azure.storage.blob import BlobServiceClient
def az_upload_blob(KVUri,secretName, container_name, file_name, data):
try:
AZURE_STORAGE_CONNECTION_STRING = az_kv_getsecret(KVUri, secretName)
blob_service_client = BlobServiceClient.from_connection_string(AZURE_STORAGE_CONNECTION_STRING)
blob_client = blob_service_client.get_blob_client(container=container_name, blob=file_name)
blob_client.upload_blob(data)
except Exception as ex:
print("Some Exception.")
def az_kv_getsecret(KVUri,secretName):
credential = ManagedIdentityCredential()
client = SecretClient(vault_url=KVUri, credential=credential)
retrieved_secret = client.get_secret(secretName)
print(retrieved_secret.value)
return retrieved_secret.value
KVUri = "https://bowmantest.vault.azure.net/"
secretName = "STR"
container_name = "test"
file_name = "0505test.txt"
data = "This is 0505test.txt"
az_upload_blob(KVUri=KVUri,secretName=secretName,container_name=container_name,file_name=file_name,data=data)
print("This is OK.")
原答案:
你能显示 az_kv_getsecre 吗?事实上,我们总是只是从 azure blob 存储中复制连接字符串。
格式应如下所示:
DefaultEndpointsProtocol=https;AccountName=youraccountname;AccountKey=xxxxxx;EndpointSuffix=core.windows.net
推荐阅读
- c - 在 C 中打印自定义对象
- reactjs - 尝试导入错误:“事件”未从“d3”导出
- python - 返回两个numpy数组中的哪些元素都等于X值?
- javascript - 有没有什么短的方法可以在不循环的情况下从头到尾对数组进行切片?
- linux - 重新上传:有人可以检查addresssanitizer结果吗?
- stripe-payments - 获取 Stripe 客户的默认付款来源(默认卡)?
- c - 找到无限系列的总和。为什么它发出零数量?
- javascript - Js:最后一个值设置和获取
- javascript - 我对 React Reconciliation 的理解正确吗?
- reactjs - 当 React 上的令牌无效时重定向到登录页面