简体   繁体   English

无法使用 python 代码从存储容器上传/下载文件

[英]Not able to upload/download files from Storage containers using python code

Here is the python code that we used,这是我们使用的 python 代码,

The files will be accessed and then uploaded to the storage container.这些文件将被访问,然后上传到存储容器。

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}")

Error Message:错误信息:

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

Please help on the missing items.请帮助解决丢失的项目。 Thank you!谢谢!

Update:更新:

I use below code and it works fine:我使用下面的代码,它工作正常:

If I run python script, I first run 'az login' and then run:如果我运行 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.")

If I use them in azure function, I just change the credential to 'ManagedIdentityCredential' and add the access policy of the function app to azure key vault(On my side, I give the full access.). If I use them in azure function, I just change the credential to 'ManagedIdentityCredential' and add the access policy of the function app to azure key vault(On my side, I give the full access.).

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.")

在此处输入图像描述

在此处输入图像描述

Original Answer:原答案:

Can you show the az_kv_getsecre?你能显示 az_kv_getsecre 吗? In fact, we always just copy the connection string from azure blob storage.事实上,我们总是只是从 azure blob 存储中复制连接字符串。

The format should like below:格式应如下所示:

DefaultEndpointsProtocol=https;AccountName=youraccountname;AccountKey=xxxxxx;EndpointSuffix=core.windows.net

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 API 下载文件并使用 python 将其保存/上传到 Google 存储 - Download file from API and save/upload it to Google Storage using python 使用 Python 从 azure blob 存储下载文件(csv、excel) - Download files (csv, excel) from azure blob storage using Python 使用 python 从 firebase 存储中下载所有文件 - Download all files from firebase storage using python 使用 python 从 Firebase 存储下载文件链接 - Download files link from Firebase storage with python 使用适用于 Python 的 Azure 存储 SDK 将多个文件从文件夹上传到 Azure Blob 存储 - Upload multiple files from folder to Azure Blob storage using Azure Storage SDK for Python 使用 Python 从 Blob 存储下载 Blob - Download Blob From Blob Storage Using Python 如何使用 Python 从给定 SAS URI 和容器名称的 Azure Blob 存储下载文件列表? - How to download a list of files from Azure Blob Storage given SAS URI and container name using Python? 如何使用 Python 从 Windows 共享网络驱动器获取文件并上传到 Azure Data Lake Storage 位置? - How to fetch files from Windows Shared Network Drive and upload to Azure Data Lake Storage location using Python? 使用 Python API 从 Google Cloud Datalab 上传文件到 Google Cloud Storage Bucket - Upload files to Google Cloud Storage Bucket from Google Cloud Datalab using Python API 如何使用Python从Jupyter Notebook将本地文件上传到Firebase存储 - How to upload local files to Firebase storage from Jupyter Notebook using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM