I am new to Azure. I am using the following part of the code to upload a file to 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());
}
}
But it is throwing an issue on uploadFileStream as following :
uploadFilestream.ReadOut threw an exception of type 'System.InvalidOAperationException'
uploadFilestream.WriteOut threw an exception of type 'System.InvalidOAperationException'
Subsequently, the console is throwing the following exception :
The I/O operation has been aborted because of either a thread exit or an application request
Caught an exception: Retry failed after 6 tries. (The operation was canceled.) (The operation was canceled.) (The operation was canceled.) (The operation was canceled.) (The operation was canceled.) (The operation was canceled.) System.AggregateException: Retry failed after 6 tries. (The operation was canceled.) (The operation was canceled.) (The operation was canceled.) (The operation was canceled.) (The operation was canceled.) (The operation was canceled.) ---> System.Threading.Tasks.TaskCanceledException: The operation was canceled. ---> System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request.. ---> System.Net.Sockets.SocketException (995): The I/O operation has been aborted because of either a thread exit or an application request.
Any help in identifying and solving the issue will be highly appreciated.
This is how I am uploading and it works for me. Not saying your method won't work... can't confirm or deny unless I copy your code and create an app on my machine.
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.
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.