如何在 Azure Blob 中上传大字符串?

How to upload a large string in an 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

多半是因为第三串太长了,不过一定有办法挽救吧? 我已经尝试通过 timeout=600.upload_blob 方法中设置超时时间,但这根本没有改变结果,也没有改变新尝试写入的时间。

错误是:

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

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

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

__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 并且没有超时错误:

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

就我而言,在我将功能部署到云端后问题就消失了。调试 Visual Studio 代码似乎有问题。