简体   繁体   English

使用应用服务和 function 应用将大 blob 上传到 Azure 存储容器

[英]Upload large blob to Azure storage container using App service and function app

I am working on a project to allow users to upload blob into blob container in our storage account.我正在开发一个项目,允许用户将 blob 上传到我们存储帐户中的 blob 容器中。 I developed a simple UI (flask) using Azure App Service to allow user choose files to upload, and then want to upload these files to the blob container.我使用 Azure App Service 开发了一个简单的 UI(flask),允许用户选择要上传的文件,然后想将这些文件上传到 blob 容器。

My original design is UI -> Blob Container by Python Storage SDK:我的原始设计是 UI -> Blob Container by Python Storage SDK:

containerClient.upload_blob(filename, file)

But I am facing the timeout issue due to Azure App Service when uploading large files.但是上传大文件时,由于 Azure 应用服务,我面临超时问题。

So I change the upload UI with dropzone.js, and enable uploading in chunk, so that the server can consistently receive response to prevent timeout.所以我用 dropzone.js 更改了上传 UI,并启用了分块上传,以便服务器可以持续接收响应以防止超时。

And another issue coming up is that upload process is executed for every piece of chunk, and blob container only receives the last chunk of the data that I upload.另一个问题是每个块都执行上传过程,而blob容器只接收我上传的最后一块数据。 (From the document, I know that the chunking is automatically used in blob upload, I wonder if we are able to track the progress of the upload??? if so, I probably don't need to use dropzone.js for uploading in chunk). (从文档我知道blob上传会自动使用分块,不知道我们是否可以跟踪上传的进度???如果可以,我可能不需要使用dropzone.js进行上传块)。

I also tried another approach by creating Azure App Function (HTTPS trigger), and then send an http trigger to that endpoint to start the blob upload.我还尝试了另一种方法,即创建 Azure 应用程序 Function(HTTPS 触发器),然后将 http 触发器发送到该端点以启动 blob 上传。

for file in files:
    fileToSend = {'file': (f.filename, f.stream, f.content_type, f.headers)}
    r = requests.post('https://myazurefunctionapp.azurewebsites.net/api/funcName', files=fileToSend)

In the azure function, I use Python Storage SDK to connect to container and then upload blob在 azure function 中,我使用 Python 存储 ZF20E3C5E54C0AB3D376D 到连接 b66

container = ContainerClient.from_connection_string(conn_str, container_name)
for k, f in req.files.items():
    container.upload_blob(f.filename, f)

But I notice that the function is triggered by piece of chunk (request), and I also end up with only receiving the last chunk of data in the container.但我注意到 function 是由一块块(请求)触发的,我也最终只接收到容器中的最后一块数据。

I wonder what would be the better workflow?我想知道更好的工作流程是什么? or if there any way that makes sure the upload is completed (in azure function) and then start the upload to blob container.或者如果有任何方法可以确保上传完成(在 azure 函数中),然后开始上传到 blob 容器。

Many Thanks,非常感谢,

• Storage clients default to a 32 MB maximum single block upload. • 存储客户端默认为最大 32 MB 的单块上传。 When a block blob upload is larger than the value in 'SingleBlobUploadThresholdInBytes' property, storage clients break the file into blocks of maximum allowed size and try to upload it.当块 blob 上传大于“SingleBlobUploadThresholdInBytes”属性中的值时,存储客户端会将文件分成最大允许大小的块并尝试上传。 Since the block blob size that you are trying to upload is greater than 32 MB, it throws an exception and breaks the file into allowed smaller chunks .由于您尝试上传的块 blob 大小大于 32 MB,因此它会引发异常并将文件分成允许的较小块 Also, you might not be using the correct 'Blob service client' which interacts with the resources, ie, storage account, blob storage containers and blobs .此外,您可能没有使用与资源交互的正确“Blob 服务客户端”,即存储帐户、blob 存储容器和 blob

Below is an example of the code for client object creation which requires a storage account's blob service account URL and a credential that allows you to access a storage account : -下面是客户端 object 创建的代码示例,它需要存储帐户的 blob 服务帐户 URL 和允许您访问存储帐户的凭据:-

 from azure.storage.blob import BlobServiceClient
 service = BlobServiceClient(account_url="https://<my-storage-account-name>.blob.core.windows.net/", credential=credential)

• Thus, similarly, as you are using the above code in python to create a blob service client for interacting with storage accounts, kindly refer to the below documentation link that describes in detail as in how to develop a python code to integrate it with blob storage for storing massive amounts of unstructured data, such as text or binary data . • 因此,类似地,当您在 python 中使用上述代码来创建用于与存储帐户交互的 blob 服务客户端时,请参阅以下文档链接,该链接详细描述了如何开发python 代码以将其与 blob 集成用于存储大量非结构化数据的存储,例如文本或二进制数据

https://docs.microsoft.com/en-us/python/api/overview/azure/storage-blob-readme?view=azure-python https://docs.microsoft.com/en-us/python/api/overview/azure/storage-blob-readme?view=azure-python

You can deploy this code in your app service or function and set the trigger accordingly for uploading and downloading blobs from the storage account .您可以在您的应用服务或 function 中部署此代码,并相应地设置触发器以从存储帐户上传和下载 blob It also describes as in how you can configure authentication for this process to ensure that the correct user and files are being given access .它还描述了如何为此过程配置身份验证以确保正确的用户和文件被授予访问权限

And refer to the documentation link for details on how to configure a blob trigger function in Azure for various interactions with the storage account when any users initiate any transaction through it .有关如何在 Azure 中配置 Blob 触发器 function 的详细信息,请参阅文档链接,以便在任何用户通过存储帐户启动任何事务时与存储帐户进行各种交互

https://docs.microsoft.com/en-us/azure/storage/blobs/blob-upload-function-trigger?tabs=azure-portal https://docs.microsoft.com/en-us/azure/storage/blob/blob-upload-function-trigger?tabs=azure-portal

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

相关问题 如何使用 azure Function app 将文件从桌面上传到 Azure Blob 存储 - how to Upload a files from Desktop to Azure blob storage using azure Function app 使用 python 将图像上传到 azure blob 存储 - Upload image to azure blob storage using python 如何使用 python sdk 将 blob 上传到带有子目录的 azure 存储容器中? - How to upload a blob into azure storage container with sub directories using the python sdk? Azure:使用容器创建存储帐户并将 Blob 上传到 Python 中 - Azure: create storage account with container and upload blob to it in Python 如何在 Python 中使用 Azure Functions 的 Azure Blob 存储绑定将 JSON 数据上传到 Azure 存储 blob - How to upload JSON data to Azure storage blob using Azure Blob storage bindings for Azure Functions in Python 如何使用 Python 和 Azure 函数在 Azure 存储容器中创建 blob - How to create a blob in an Azure Storage Container using Python & Azure Functions Azure Blob 存储触发器 function 使用 Docker - Azure Blob storage trigger function using Docker 使用 python 在函数应用中绑定来自 Azure blob 的输入 - Binding input from Azure blob in function app using python Azure function 和 Azure Blob 存储 - Azure function and Azure Blob Storage 使用Python获取Azure Blob存储中的容器大小 - Get container sizes in Azure Blob Storage using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM