question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GetObjectAsync object stream couldn't use on the new FileStreamResult

See original GitHub issue

I try use GetObjectAsync (Downloads an object as a stream.) and I would like to use stream on FileStreamResult. I can set it but when GetObjectAsync method finish my FileStreamResult.FileStream contentbytes go 0. You can follow the attaches. GetObjectAsync hasn’t a return type.

My old response and return value with S3.Model

            var file = await _storage.Download(bucket, key);

            var result  = new FileStreamResult(file.ResponseStream, file.Headers.ContentType)
            {
                FileDownloadName = temp.Name + type,
                LastModified = file.LastModified,
                EntityTag = new EntityTagHeaderValue(file.ETag)
            };

MinioClient

When I use with this code, i get that

ObjectDisposedException: Cannot access a closed Stream.

            using (Stream strm = new MemoryStream())
            {
                await _storage.Client1.GetObjectAsync(bucket, key, async stream => { await stream.CopyToAsync(strm); });
              
                fileStreamResult = new FileStreamResult(strm, file.Headers.ContentType)
                {
                    FileDownloadName = temp.Name + type,
                    LastModified = DateTimeOffset.Now,
                    EntityTag = new EntityTagHeaderValue(file.ETag)
                };     
           
                return fileStreamResult;
            }

then i try to it with this code,


           await _storage.Client1.GetObjectAsync(bucket, key, stream =>
           {
               fileStreamResult = new FileStreamResult(stream, file.Headers.ContentType)
               {
                   FileDownloadName = temp.Name + type,
                   LastModified = DateTimeOffset.Now,
                   EntityTag = new EntityTagHeaderValue(file.ETag)
               };
           });
            
            return fileStreamResult;

It sets to file stream and when finish the GetObjectAsync method, it set to fileStreamResult.FileStream contentBytes as 0 automaticly.

How i can fix this.

Thank you.

1 2 Void

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ebozdumancommented, Jan 7, 2023

you can use stream.CopyToAsync(ms), but when minio calls back, the user response stream has ended, and the http channel has been closed.

@hyfree, The issue was with the async lambda function (Action<Stream>) used in GetObjectAsync callback. So, Func<Stream, CancellationToken, Task> support is added to properly await async methods, like Stream’s CopyToAsync.

@onurbos, @hyfree, Please follow PR#730, which is currently in the code review process. You can also check the functional test GetObject_AsyncCallback_Test1 in Minio.Functional.Tests/FunctionalTest.cs. The fix will be available as soon as the PR is merged and a new MinIO release is created for nuget.org. I anticipate it’ll be available in the next couple of weeks.

1reaction
harshavardhanacommented, Mar 14, 2022

Please upgrade to the latest release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stream S3 file via .NET FileStreamResult and Web API
1 Answer. Seems FileStreamResult will handle disposing of the stream internally.
Read more >
Get an object from an Amazon S3 bucket using an AWS SDK
Get an object from an Amazon S3 bucket using an AWS SDK. ... GetObjectAsync(request); try { // Save object to local file await...
Read more >
Asynchronously processing GetObjectAsync stream #420
For the time being, I came up with a workaround using TaskCompletionSource, although I cannot test if it is truly asynchronous or blocking....
Read more >
FileStreamResult does not indicate download start
Well, the simple answer seems to be: The download start & progress are not visible in browser, because the content length is not...
Read more >
FileStreamResult Class (Microsoft.AspNetCore.Mvc)
Represents an ActionResult that when executed will write a file from a stream to the response.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found