\$\begingroup\$
\$\endgroup\$
3
I am currently writing a program that takes image files from my data base and uploads them to Azure storage here is what I have written:
public async Task UploadAssetsAsync(Func<GridFSFileInfo, string> prefixSelector, List<GridFSFileInfo> files, Func<GridFSFileInfo, Task<Stream>> streamOpener, Func<string, Task> progressAction)
{
if (flyersContainerClient == null)
throw new Exception("Container client not initialized. Please initialize before doing blob operations.");
var q = new Queue<Task<Response<BlobContentInfo>>>();
progressAction?.Invoke($"{files.Count}");
foreach (var f in files)
{
var pathPrefix = prefixSelector(f);
var blobClient = flyersContainerClient.GetBlobClient($"{pathPrefix}/{f.Filename.Replace("_copy", "")}");
IDictionary<string, string> metadata = new Dictionary<string, string>();
var blobhttpheader = new BlobHttpHeaders();
if (f.Filename.EndsWith("svg"))
{
blobhttpheader.ContentType = "image/svg+xml";
}
var stream = await streamOpener(f);
if (pathPrefix == "thumbnails")
{
var format = ImageFormat.Jpeg;
Bitmap cropped = null;
using (Image image = Image.FromStream(stream))
{
format = image.RawFormat;
Rectangle rect = new Rectangle(0, 0, image.Width, image.Width);
cropped = new Bitmap(image.Width, image.Width);
using (Graphics g = Graphics.FromImage(cropped))
{
g.DrawImage(image, new Rectangle(0, 0, cropped.Width, cropped.Height), rect, GraphicsUnit.Pixel);
}
}
stream.Dispose();
stream = new MemoryStream();
cropped.Save(stream, format);
stream.Position = 0;
}
q.Enqueue(blobClient.UploadAsync(stream, new BlobUploadOptions { HttpHeaders = blobhttpheader, TransferOptions = new Azure.Storage.StorageTransferOptions { MaximumConcurrency = 8, InitialTransferSize = 50 * 1024 * 1024 } }));
}
await Task.WhenAll(q);
}
This is the fastest way I think it could run but it still seems sluggish as it can be uploading up to 2000-3000 images. What am I missing?
Peter Csala
10.7k1 gold badge16 silver badges36 bronze badges
lang-cs
q
,g
,f
, etc. </irony> \$\endgroup\$metadata
is defined but never used. Naming doesn't follow Microsoft's rules (e.g.blobhttpheader
). ABitmap
needs to be disposed. \$\endgroup\$