如何在 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'))

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


烙印99
浏览 138回答 2
2回答

MYYA

在我这边,我没有问题。你可以看看我的代码:__init__.pyimport loggingimport azure.functions as funcdef main(req: func.HttpRequest,outputblob: func.Out[func.InputStream],) -> func.HttpResponse:&nbsp; &nbsp; logging.info('This code is to upload a string to a blob.')&nbsp; &nbsp; s_3 = "x"*24518001&nbsp; &nbsp; outputblob.set(s_3)&nbsp; &nbsp; return func.HttpResponse(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "The string already been uploaded to a blob.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; status_code=200&nbsp; &nbsp; )function.json{&nbsp; "scriptFile": "__init__.py",&nbsp; "bindings": [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "authLevel": "anonymous",&nbsp; &nbsp; &nbsp; "type": "httpTrigger",&nbsp; &nbsp; &nbsp; "direction": "in",&nbsp; &nbsp; &nbsp; "name": "req",&nbsp; &nbsp; &nbsp; "route": "{test}",&nbsp; &nbsp; &nbsp; "methods": [&nbsp; &nbsp; &nbsp; &nbsp; "get",&nbsp; &nbsp; &nbsp; &nbsp; "post"&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "type": "http",&nbsp; &nbsp; &nbsp; "direction": "out",&nbsp; &nbsp; &nbsp; "name": "$return"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "name": "outputblob",&nbsp; &nbsp; &nbsp; "type": "blob",&nbsp; &nbsp; &nbsp; "path": "test1/{test}.txt",&nbsp; &nbsp; &nbsp; "connection": "str",&nbsp; &nbsp; &nbsp; "direction": "out"&nbsp; &nbsp; }&nbsp; ]}local.settings.json{&nbsp; "IsEncrypted": false,&nbsp; "Values": {&nbsp; &nbsp; "AzureWebJobsStorage": "",&nbsp; &nbsp; "FUNCTIONS_WORKER_RUNTIME": "python",&nbsp; &nbsp; "str":"DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx==;EndpointSuffix=core.windows.net"&nbsp; }}然后我点击端点http://localhost:7071/api/bowman,它将字符串上传到 blob 并且没有超时错误:所以,我认为问题与您使用的方法有关。

斯蒂芬大帝

就我而言,在我将功能部署到云端后问题就消失了。似乎使用 Visual Studio 代码调试时出现问题。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python