【问题标题】:Cloud storage bucket does not save two files at a time in production云存储桶在生产中不会一次保存两个文件
【发布时间】:2020-08-20 04:08:05
【问题描述】:

在开发中,下面的代码成功上传了一个图片文件并调整了缩略图的大小

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 "";
        }
    }

但由于某种原因,我无法使其在生产环境中工作。在“生产”存储桶中,只有第一个使用 uploadFile(file, fileType, userDTO) 的文件被成功保存,但缩略图没有。

在这里您可以看到两个存储桶信息,因为您可以看到它们是相同的。

生产基础设施: 在 App Engine Flex 中运行的 .NET Core 3.1 MVC 应用

发展中的基础设施: 在 IISExpress 中运行的 .NET Core 3.1 MVC 应用程序捆绑在 Visual Studio 中

也许是我用错了东西,或者 App Engine Flexible 出于某种原因无法执行这些请求?

也许代码当然可以改进,但我不明白为什么在开发中它可以工作,但在生产中却不行。 ????

【问题讨论】:

  • 您在 Stackdriver 上收到任何错误吗?
  • fail: FileUploadServiceDefault[0] An error ocurred while uploading file, message: The type initializer for 'Gdip' threw an exception. 刚看到,可能是与后台运行 App Engine Flex 的操作系统有关吗?我正在使用 System.Drawing.Image 类来调整图像的大小。
  • 听起来很合理,谷歌没有为 Net core 3.1 提供优化的运行时,作为替代方案,如果您想生成存储在存储桶上的图像的缩略图,您可以使用云功能按照answer 的建议,监听存储桶上的变化并生成调整大小的图像
  • 谢谢,我去看看

标签: .net .net-core google-cloud-storage asp.net-core-3.1 app-engine-flexible


【解决方案1】:

对于那些将来寻求此功能的人,我选择使用https://github.com/SixLabors/ImageSharp 而不是 System.Drawing.Common 包。 GDI+ 似乎缺少一些库(我不确定如何配置 App Engine 的 Flex 环境并在服务器中下载一些 GDI+ 所需的库,尽管您可以使用 Dockerfile 来完成)并且 ImageSharp 只是负责无需安装任何东西。我刚刚使用 Visual Studio 从 NuGet 下载了它。

来自 GitHub:

ImageSharp 是一种全新的、功能齐全、完全托管的、跨平台的 2D 图形库。 ImageSharp 旨在简化图像处理 为您带来令人难以置信的强大而简单的 API。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-28
    • 2020-01-13
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 2013-11-01
    相关资源
    最近更新 更多