0
\$\begingroup\$

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?

\$\endgroup\$
3
  • 2
    \$\begingroup\$ <irony>I always love to review those code fragments where we have variable names like q, g, f, etc. </irony> \$\endgroup\$ Commented Nov 18, 2022 at 9:05
  • \$\begingroup\$ Have you performed some kind of profiling? What did it say which part of your method is the bottleneck from performance perspective? \$\endgroup\$ Commented Nov 18, 2022 at 9:06
  • 1
    \$\begingroup\$ metadata is defined but never used. Naming doesn't follow Microsoft's rules (e.g. blobhttpheader). A Bitmap needs to be disposed. \$\endgroup\$ Commented Nov 18, 2022 at 17:03

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.