简体   繁体   中英

Uploading ResponseStream to Blob Storage

unfortunately, I am pretty much stuck in the following scenario:

I am using .NET Core 3.1 and fetching data via DI with the HttpClient (Typed Client) from an url. This data is a csv file which shall be uploaded to an Azure Blob Storage. At the point, where I am trying to upload it to the Blob Storage, it seems to fail with the following exception:

 System.Net.Http: The operation was canceled. System.Net.Http: Error while copying content to a stream. 
 System.Net.Sockets: 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.. 
 The I/O operation has been aborted because of either a thread exit or an application request.

I have currently no clue why the upload is not working. Other uploads from other streams seem to work without any error. Does anyone of you have a clue for me?

My classes

public class Web<T> where T : BlobStorageContext
{

    private HttpClient _client { get; }
    private ILogger<Web> _logger;
    private Csv<T> _csv { get; }

    public Web(HttpClient client, Csv<T> csv, ILogger<Web> logger)
    {
        _client = client;
        _logger = logger;

        _csv = csv;
    }

    public async Task AccessWebFileAndSaveToAzureAsync()
    {
        ...

        var request = new HttpRequestMessage(HttpMethod.Get,
            url);
        
        using (var response = await _client.SendAsync(request))
        {
           
            await using (var str = await response.Content.ReadAsStreamAsync())
            {
                await _csv.WriteFileToAzureAsync(str);
            }
        }
    }
}


public class Csv<T> where T : BlobStorageContext
{
    private BlobServiceClient _blobServiceClient { get; set; }
    private ILogger<Csv<T>> _logger { get; set; }

    public Csv(IOptions<BlobStorageConfiguration<T>> configuration, ILogger<Csv<T>> logger)
    {
        _blobServiceClient = new BlobServiceClient(configuration.Value.ConnectionString());
        _logger = logger;
    }

    public async Task WriteFileToAzureAsync(Stream str)
    {
      
        var container = _blobServiceClient.GetBlobContainerClient("container);
        var blob = container.GetBlobClient(blobPath);
        await blob.UploadAsync(str, true);
    }
}

Copy the connection string and assign to the storageAccount_connectionString variable in the upload_ToBlob() method

storageAccount_connectionString = "your Azure storage account connection string"  

Following statements are used to create the objects for the Storage Account, Blob client and Blob container.

CloudStorageAccount mycloudStorageAccount = CloudStorageAccount.Parse(storageAccount_connectionString);  
CloudBlobClient blobClient = mycloudStorageAccount.CreateCloudBlobClient(); 
CloudBlobContainer container = blobClient.GetContainerReference(azure_ContainerName);   

This code is for checking whether the specified container exists in storage account or not.

If it exists, the application will use the existing container. Otherwise, it will create a container inside storage account with specified name.

if (container.CreateIfNotExists())
{      
    container.SetPermissionsAsync(new    
    BlobContainerPermissions    
   {    
       PublicAccess =BlobContainerPublicAccessType.Blob    
   });
} 

And following statement is used to create a Block blob object using the file name with extension

CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(filename_withExtension);  

cloudBlockBlob.UploadFromStreamAsync(file) statement is used to upload the file to the blob storage.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM