Teoman shipahi
Teoman shipahi

Reputation: 23132

HttpRequestException while executing BlobClient.UploadAsync

I have following code to upload files to my Azure storage:

Read File Stream like below:

   FileStream fileStream = File.OpenRead(filePath);

and pass to the function =>

 public async Task<Response<BlobContentInfo>> UploadBlob(string containerName, string blobName, Stream stream, string contentType,
            IDictionary<string, string> metadata = null)
        {
            IDictionary<string, string> metadataEncoded = new Dictionary<string, string>();
            if (metadata != null)
            {
                foreach (KeyValuePair<string, string> keyValuePair in metadata)
                {
                    string encodedValue = TextUtilities.Base64Encode(keyValuePair.Value);
                    metadataEncoded.Add(keyValuePair.Key, encodedValue);
                }
            }
            await using (stream)
            {
                BlobClient blobClient = GetBlobClient(containerName, blobName);
                BlobHttpHeaders httpHeaders = new BlobHttpHeaders { ContentType = contentType };
                BlobRequestConditions conditions = new BlobRequestConditions();
                Response<BlobContentInfo> uploadAsync = await blobClient.UploadAsync(stream, httpHeaders, metadataEncoded, conditions);
                stream.Close();
                return uploadAsync;
            }
        }

If I try to upload arbitrary files like this sample pdf => http://www.africau.edu/images/default/sample.pdf it works fine. But for some reason if I try to upload pdf files like in this page =>
https://learn.microsoft.com/en-us/dotnet/core/
Dot.NET Core Documentation PDF

I am receiving following exception:

Inner Exception 1:
HttpRequestException: Error while copying content to a stream.

Inner Exception 2:
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..

Inner Exception 3:
SocketException: The I/O operation has been aborted because of either a thread exit or an application request.

I don't think this is related to file size, because I also tried some other .pdf files with larges sizes, and they got pushed to Azure Storage.

Am I missing something specific here? I also downloaded the .pdf from https://learn.microsoft.com/en-us/azure/sql-database/ and received same error from the line await blobClient.UploadAsync()

Note: As a reference I found this open GitHub issue, but not resolved yet. https://github.com/Azure/azure-sdk-for-net/issues/9212

Upvotes: 0

Views: 2528

Answers (1)

Deepak Mitawa - AIS
Deepak Mitawa - AIS

Reputation: 151

I have also stuck with this issue. Its solution is very simple, just set your stream position as 0.

stream.Position = 0;

All set, it will work.

Upvotes: 6

Related Questions