首页 > 解决方案 > 如何在 Azure Blob 中上传大字符串?

问题描述

现在我正试图弄清楚如何使用 Azure,现在我在将我的数据存储在存储帐户中时遇到了一个问题。我有三个字符串,并希望将它们中的每一个存储在一个单独的 blob 中。对于前两个,我的代码工作正常,但第三个导致一些重试并以超时结束。我的代码在 Azure 函数中运行。

这是一个最小的例子:

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,
)

前两个字符串一切正常,但第三个失败。字符串具有以下长度:

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

很可能是因为第三个字符串太长了,但一定有办法保存它,对吧?我已经尝试在.upload_blob方法中设置超时时间 by timeout=600,但这根​​本没有改变结果,也没有改变直到重新尝试写入的时间。

错误是:

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

如果您对此问题有任何想法,请告诉我:-)

标签: pythontimeoutazure-functionsazure-blob-storage

解决方案


在我这边,我没有问题。你可以看看我的代码:

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

然后我点击端点http://localhost:7071/api/bowman,它将字符串上传到 blob 并且没有超时错误:

在此处输入图像描述

所以,我认为问题与您使用的方法有关。


推荐阅读