0

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

2
  • Will it be possible to include a link to the PDF that you're not able to upload? Your 2nd link just points to the .Net Core documentation page. Commented Jan 11, 2020 at 12:47
  • The is a download link on the left bottom of the page. Commented Jan 11, 2020 at 14:37

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Deepak, welcome to SO! Please read up on writing a good answer before you answer your next question! Enjoy your stay :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.