Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try seek blob stream position to begin #10445

Merged
merged 4 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ public override async Task<Stream> GetOrNullAsync(BlobProviderGetArgs args)
return null;
}
var result = ossClient.GetObject(containerName, blobName);
var memoryStream = new MemoryStream();
await result.Content.CopyToAsync(memoryStream);
return memoryStream;
return await TryCopyToMemoryStreamAsync(result.Content, args.CancellationToken);
}

protected virtual string GetContainerName(BlobProviderArgs args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ public override async Task<Stream> GetOrNullAsync(BlobProviderGetArgs args)
Key = blobName
});

var memoryStream = new MemoryStream();
await response.ResponseStream.CopyToAsync(memoryStream);
return memoryStream;
return await TryCopyToMemoryStreamAsync(response.ResponseStream, args.CancellationToken);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ public override async Task<Stream> GetOrNullAsync(BlobProviderGetArgs args)

var blobClient = GetBlobClient(args, blobName);
var download = await blobClient.DownloadAsync();
var memoryStream = new MemoryStream();
await download.Value.Content.CopyToAsync(memoryStream);
return memoryStream;
return await TryCopyToMemoryStreamAsync(download.Value.Content, args.CancellationToken);
}

protected virtual BlobClient GetBlobClient(BlobProviderArgs args, string blobName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ public override async Task<Stream> GetOrNullAsync(BlobProviderGetArgs args)
{
using (var fileStream = File.OpenRead(filePath))
{
var memoryStream = new MemoryStream();
await fileStream.CopyToAsync(memoryStream, args.CancellationToken);
return memoryStream;
return await TryCopyToMemoryStreamAsync(fileStream, args.CancellationToken);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,13 @@ public override async Task<Stream> GetOrNullAsync(BlobProviderGetArgs args)
return null;
}

var memoryStream = new MemoryStream();
Stream blobStream = null;
await client.GetObjectAsync(containerName, blobName, (stream) =>
{
if (stream != null)
{
stream.CopyTo(memoryStream);
}
else
{
memoryStream = null;
}
blobStream = stream;
});

return memoryStream;
return await TryCopyToMemoryStreamAsync(blobStream, args.CancellationToken);
}

protected virtual MinioClient GetMinioClient(BlobProviderArgs args)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Volo.Abp.BlobStoring
Expand All @@ -12,5 +13,18 @@ public abstract class BlobProviderBase : IBlobProvider
public abstract Task<bool> ExistsAsync(BlobProviderExistsArgs args);

public abstract Task<Stream> GetOrNullAsync(BlobProviderGetArgs args);

protected virtual async Task<Stream> TryCopyToMemoryStreamAsync(Stream stream, CancellationToken cancellationToken = default)
{
if(stream == null)
{
return null;
}

var memoryStream = new MemoryStream();
await stream.CopyToAsync(memoryStream, cancellationToken);
memoryStream.Seek(0, SeekOrigin.Begin);
return memoryStream;
}
}
}