Search code examples
.net.net-coregoogle-cloud-storageasp.net-core-3.1app-engine-flexible

Cloud storage bucket does not save two files at a time in production


In development the following code successfully uploads an image file and it's resized thumbnail

private string uploadFile(IFormFile file, FileType fileType, UserDTO userDTO)
        {
            try
            {
                var storageObject = storageClient.UploadObjectAsync(
                    bucket: bucketId,
                    objectName: getUniqueFileName(userDTO.UserId, getFileType(fileType)),
                    contentType: file.ContentType,
                    source: file.OpenReadStream(),
                    options: new UploadObjectOptions { PredefinedAcl = PredefinedObjectAcl.PublicRead }
                );

                return storageObject.Result.MediaLink;
            }
            catch (Exception ex)
            {
                logger.LogError("An error ocurred while uploading file, message: " + ex.Message);
                return "";
            }
        }

    private string uploadImageThumbnail(IFormFile file, FileType fileType, UserDTO userDTO)
    {
        try
        {
            using var resourceImage = file.OpenReadStream();

            Image image = Image.FromStream(resourceImage);
            Image thumb = image.GetThumbnailImage(96, 96, () => false, IntPtr.Zero);

            thumb.Save(resourceImage, ImageFormat.Png);

            var storageObject = storageClient.UploadObjectAsync(
                bucket: bucketId,
                objectName: getUniqueFileName(userDTO.UserId, getFileType(fileType)),
                contentType: file.ContentType,
                source: resourceImage,
                options: new UploadObjectOptions { PredefinedAcl = PredefinedObjectAcl.PublicRead }
            );

            return storageObject.Result.MediaLink;
        }
        catch (Exception ex)
        {
            logger.LogError("An error ocurred while uploading file, message: " + ex.Message);
            return "";
        }
    }

But for some reason I can't make this to work in production environment. In the 'production' bucket only the first file using uploadFile(file, fileType, userDTO) is successfully saved but the thumbnail is not.

Here you can see both buckets information, as you can see they're identical. Buckets

Infrastructure in Production: .NET Core 3.1 MVC App running in App Engine Flex

Infrastructure in Development: .NET Core 3.1 MVC App running in IISExpress bundled in Visual Studio

Perhaps there is something I'm using wrong or App Engine Flexible is not capable of doing these requests for some reason?

Maybe the code can be improved of course, but what I don't understand is why in development this works but in production it does not. 🤔


Solution

  • For those that seek for this in the future, I opted for using https://github.com/SixLabors/ImageSharp instead of System.Drawing.Common package. It seems like some libraries were missing for the GDI+ (I wasn't sure in how to configure App Engine's Flex environment and download some GDI+ required libraries in the server, although you can do it using a Dockerfile) and ImageSharp just takes care of that without installing anything. I just downloaded it from NuGet using Visual Studio.

    From GitHub:

    ImageSharp is a new, fully featured, fully managed, cross-platform, 2D graphics library. Designed to simplify image processing, ImageSharp brings you an incredibly powerful yet beautifully simple API.