问题描述
我是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.
}
}
}