How to upload a large string in an Azure Blob?

梦想的初衷 提交于 2021-01-28 08:16:37

问题


Right now I'm trying to figure out how to work with Azure, and now I'm stuck in a problem while storing my data in the storage account. I have three strings and want to store each of them in a separate blob. With the first two, my code works fine, but the third one causes some retries and ends with a timeout. My code is running within an Azure function.

Here is a minimal example:

from azure.storage.blob import BlobClient

blob_client = BlobClient.from_connection_string(
    conn_str.      = '<STORAGE_ACCOUNT_CONNECTION_STRING>',
    container_name = '<CONTAINER_NAME>',
    blob_name.     = '<NAME_OF_BLOB>',
)

dic_blob_props = blob_client.upload_blob(
    data      = '<INFORMATION_THAT_SHOULD_GO_TO_THE_BLOB>', 
    blob_type = "BlockBlob", 
    overwrite = True,
)

The for the first two strings everything works fine but the third fails. The strings have the following length:

len(s_1) = 1246209
len(s_2) = 8794086
len(s_3) = 24518001

Most likely it is because the third string is too long, but there must be a way to save it, right? I have already tried to set the timeout time within the .upload_blob method by timeout=600, but this has not changed the result at all, nor the time until a new attempt to write is made.

The error is:

Exception: ServiceResponseError: ('Connection aborted.', timeout('The write operation timed out'))

If you have any ideas on the problem pleast let me know :-)


回答1:


On my side, I dont have the problem. You can have a look of my code:

__init__.py

import logging

import azure.functions as func


def main(req: func.HttpRequest,outputblob: func.Out[func.InputStream],) -> func.HttpResponse:
    logging.info('This code is to upload a string to a blob.')
    s_3 = "x"*24518001
    outputblob.set(s_3)
    return func.HttpResponse(
            "The string already been uploaded to a blob.",
            status_code=200
    )

function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "route": "{test}",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    },
    {
      "name": "outputblob",
      "type": "blob",
      "path": "test1/{test}.txt",
      "connection": "str",
      "direction": "out"
    }
  ]
}

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "str":"DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx==;EndpointSuffix=core.windows.net"
  }
}

Then I hit the endpoint http://localhost:7071/api/bowman, it upload the string to blob and dont have time out error:

So, I think the problem is related with the method you use.




回答2:


In my case, the problem disappeared after I deployed the function in the cloud. It seems that there was a problem debugging with Visual Studio code.



来源:https://stackoverflow.com/questions/63579293/how-to-upload-a-large-string-in-an-azure-blob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!