python - 使用 SAS 将文件上传到 Azure

标签 python azure

我有几个关于使用 Python 上传到 SAS 的问题。我有客户提供的 SAS,其形式为:

https://<company_name>.blob.core.windows.net/<container_name>?sp<long_string>

我尝试遵循以下代码:Uploading csv files to azure container using SAS URI in python?

from azure.storage.blob import BlobClient

upload_file_path="d:\\a11.csv"
sas_url="https://xxx.blob.core.windows.net/test5/a11.csv?sastoken"

client = BlobClient.from_blob_url(sas_url)

with open(upload_file_path,'rb') as data:
    client.upload_blob(data)

print("**file uploaded**")

我收到以下错误:

azure.core.exceptions.ResourceExistsError: Public access is not permitted on this storage account.
RequestId:946bd6ea-e01e-0040-3932-ee6a4e000000
Time:2021-12-11T01:58:51.0010075Z
ErrorCode:PublicAccessNotPermitted

Azure SDK 提到使用了我没有的帐户名,所以这是不行的(我可以使用 Azure 存储资源管理器上传文件,但这对于我的需要来说很慢,但我知道 SAS 正在工作)。我是否使用了错误的上传代码?另外,不清楚如何告诉代码将文件上传到 blob 容器中的何处?例如。如果我想上传文件image.jpg2021-12-11/dataset_1/ ,我应该把它放在代码中的什么位置?

最佳答案

我尝试使用从容器生成的 SAS URL 上传文件,但无法上传文件。 不要使用容器的 SAS URL,而是使用您的存储帐户 SAS URL,这对我来说效果很好,使用您提供的相同代码。

  • 要为存储帐户生成 SAS URL,请执行以下步骤:

enter image description here

  • 添加了存储帐户的 SAS URL 并运行以下 cmd 以下是输出屏幕截图:

enter image description here

enter image description here

欲了解更多信息,请参阅此MS DOC & & Github样本

关于python - 使用 SAS 将文件上传到 Azure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70312111/

相关文章:

Python 偏写读写锁

Python 使用 Pandas 从数据框中逐行检查现有值

python - 向 Dynamics CRM Web API 发出请求

linux - “git: command not found” 运行时 “pm2 deploy production setup”

azure - 运行 Terraform Apply 时 Azure Key Vault 访问策略出现问题

python - 我应该在 UiPath 中使用 Python 的示例案例有哪些?

python - PyQt:在 QListView 中左右对齐文本

python - 如何将 celery beat 作为守护进程运行?

json - 如何在 matchCondition 中路由所有内容?

azure - .NET Core 版本重写后将一项 Azure 应用服务的代码库切换为不同的代码库