gpt4 book ai didi

c# - 使用 azure-sdk-for-net 将大型 blob 上传到 azure 超时

转载 作者:行者123 更新时间:2023-12-03 01:20:11 26 4
gpt4 key购买 nike

我正在尝试使用 C# 和 Azure.Storage.Blobs.Specialized.BlockBlobClient 上传大文件。我遇到超时的情况。我需要一个演示如何上传大文件(大约 300MB)的示例。我一直找不到一个好的例子。

using Azure.Storage.Blobs.Specialized;
...
using (var fs = fileInfo.Open(FileMode.Open,FileAccess.Read))
{
var blockBlobClient = new BlockBlobClient(
connectionString,
"ore",
fileInfo.Name);

await blockBlobClient.UploadAsync(fs);
}

最佳答案

我必须调整网络超时。

var uploadManager = new UploadManager();
await uploadManager.UploadBlob(connectionString, container, fileInfo);

此类处理大型上传,增加网络超时并提供进度输出。

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Uploader
{
public class UploadManager
{
System.Collections.Concurrent.ConcurrentBag<long> progressBag = null;
Progress<long> progressHandler = null;

public UploadManager()
{
progressBag = new System.Collections.Concurrent.ConcurrentBag<long>();
progressHandler = new Progress<long>(progress => progressBag.Add(progress));
progressHandler.ProgressChanged += ProgressHandler_ProgressChanged;

}
public async Task UploadBlob(string connectionString, string container, FileInfo fileInfo)
{

using (var fs = fileInfo.Open(FileMode.Open, FileAccess.Read))
{
var clientOptions = new BlobClientOptions();
clientOptions.Retry.NetworkTimeout = new TimeSpan(0, 0, 600);
var blockBlobClient = new BlockBlobClient(connectionString, container, fileInfo.Name, clientOptions);

var uploadOptions = new BlobUploadOptions();
uploadOptions.ProgressHandler = progressHandler;
await blockBlobClient.UploadAsync(fs, uploadOptions);
}
}

private static void ProgressHandler_ProgressChanged(object sender, long e)
{
Debug.WriteLine($"Progress:{(e / 1000).ToString()} MB");
}

}
}

关于c# - 使用 azure-sdk-for-net 将大型 blob 上传到 azure 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71152149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com