gpt4 book ai didi

c# - 上传 Azure Blob 时出现问题 : ReadTimeout' threw an exception of type 'System. InvalidOperationException

转载 作者:行者123 更新时间:2023-12-03 03:56:57 25 4
gpt4 key购买 nike

我是 Azure 新手。我正在使用以下部分代码将文件上传到 Azure Blob。

public async Task<byte[]> UploadResultFile(string fileName, byte[] data)
{

if (StringUtilities.isBlankOrNull(fileName))
{
throw new EmptyStringException("File name cannot be empty or null");
}
// Creates a BlobServiceClient object which will be used to create a container client
BlobServiceClient blobServiceClient = new BlobServiceClient(config.StorageConnectionString);

// Create the container and return a container client object
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(config.ResultContainer);

// Create a local file in the ./data/ directory for uploading and downloading
string localFilePath = Path.Combine(Experiment.DataFolder, fileName);

// Write text to the file
// Adding a check to write a data in a file only if data is not equal to null
// This is important as we need to re-use this method to upload a file in which data has already been written
if (data != null)
{
File.WriteAllBytes(localFilePath, data);
}

// Get a reference to a blob
BlobClient blobClient = containerClient.GetBlobClient(fileName);

// Open the file and upload its data
// FileStream uploadFileStream = File.OpenRead(localFilePath);
using FileStream uploadFileStream = File.OpenRead(localFilePath);
await blobClient.UploadAsync(uploadFileStream, true);
uploadFileStream.Close();
return Encoding.ASCII.GetBytes(blobClient.Uri.ToString());
}
}

但它在 uploadFileStream 上引发了一个问题,如下所示:

<小时/>

uploadFilestream.ReadOut 抛出“System.InvalidOAperationException”类型的异常

uploadFilestream.WriteOut 抛出“System.InvalidOAperationException”类型的异常

<小时/>

随后,控制台抛出以下异常:

<小时/>

由于线程退出或应用程序请求,I/O 操作已中止

捕获异常:尝试 6 次后重试失败。 (操作被取消。) (操作被取消。) (操作被取消。) (操作被取消。) (操作被取消。) (操作被取消。)System.AggregateException: 6 次尝试后重试失败。 (操作被取消。) (操作被取消。) (操作被取消。) (操作被取消。) (操作被取消。) (操作被取消。)---> System.Threading.Tasks.TaskCanceledException:操作已取消。---> System.Net.Http.HttpRequestException:将内容复制到流时出错。---> System.IO.IOException: 无法从传输连接读取数据: 由于线程退出或应用程序请求,I/O 操作已中止。---> System.Net.Sockets.SocketException (995): 由于线程退出或应用程序请求,I/O 操作已中止。

<小时/>

对于识别和解决问题的任何帮助,我们将不胜感激。

最佳答案

这就是我上传的方式,它对我有用。并不是说你的方法不起作用......无法确认或否认,除非我复制你的代码并在我的机器上创建一个应用程序。

using System;
using System.IO;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.RetryPolicies;

namespace My.Repositories
{
public class BlobStorageRepository
{
private readonly CloudBlobContainer _cloudContainer;

public BlobStorageRepository(string containerName, string connectionStringForStorageAccount)
{
CloudStorageAccount storageAccount;

storageAccount = CloudStorageAccount.Parse(connectionStringForStorageAccount);
var blobClient = storageAccount.CreateCloudBlobClient();
blobClient.DefaultRequestOptions = new BlobRequestOptions
{
// below timeout you can change to your needs
MaximumExecutionTime = TimeSpan.FromSeconds(30),
LocationMode = LocationMode.PrimaryThenSecondary
};

_cloudContainer = blobClient.GetContainerReference(containerName);
}

public int Save<T>(string blobName, byte[] contentBytes) where T : class
{
var bytes = contentBytes;
var blockBlob = _cloudContainer.GetBlockBlobReference($"{blobName}.json");
blockBlob.Properties.ContentType = "application/json";
using (var memoryStream = new MemoryStream(bytes))
{
blockBlob.UploadFromStream(memoryStream);
}
return bytes.Length; // returning the number of bytes uploaded.
}
}
}

关于c# - 上传 Azure Blob 时出现问题 : ReadTimeout' threw an exception of type 'System. InvalidOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64228095/

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