【问题标题】:Image broken when uploaded to Azure blobs上传到 Azure Blob 时图像损坏
【发布时间】:2020-01-09 11:46:18
【问题描述】:

我正在使用以下代码将图像上传到 Azure blob 存储。图像已上传到存储,但已损坏。以下是我的代码。 Base64字符串是正确的,我已经确认了。

string match_val = "z5FuyuNiJDbbb...................";

System.Drawing.Bitmap img = Custom.ConertBase64ToFile(match_val);

var bytes = Convert.FromBase64String(match_val);

StorageCredentials creden = new StorageCredentials(accountname, accesskey);
CloudStorageAccount acc = new CloudStorageAccount(creden, useHttps: true);
CloudBlobClient client = acc.CreateCloudBlobClient();

CloudBlobContainer cont = client.GetContainerReference(container);
 await cont.CreateIfNotExistsAsync();

await cont.SetPermissionsAsync(new BlobContainerPermissions
{
    PublicAccess = BlobContainerPublicAccessType.Blob
});

CloudBlockBlob cblob = cont.GetBlockBlobReference(Path.Combine(ProfileSignaturePath, string.Concat(fileName, ".", FileFormat.png)).Replace(@"\","/"));

cblob.Properties.ContentType = "image/png";

using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(bytes), true, true))
{
    using (MemoryStream memoryStream = new MemoryStream())
    {
        image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
        await cblob.UploadFromStreamAsync(memoryStream);
        blobUrl = cblob.Uri.AbsoluteUri;
    }
}

string resUrl = blobUrl;

这是浏览器显示的内容

Blob 图片网址是https://mspmatefilestorage.blob.core.windows.net/production/Resources/ProfilePictures/Signatures/signature_16.png

我的图片上传代码有问题吗?

【问题讨论】:

  • 您是否尝试过将内存流保存到文件中,只是为了检查数据在此之前是否正常?
  • 另外,上传前尝试将内存流的位置重置为0。
  • 您能否检查一下我的回答是否有帮助?谢谢!

标签: c# azure asp.net-core asp.net-web-api azure-blob-storage


【解决方案1】:

我可以知道 match_val 是什么吗?它是 png 文件内容的 base64 编码字符串吗?

我很快创建了一个控制台项目,一切正常。这是我的示例:

    class Program
    {
        static string storageAccountName = "storagetest789";
        static string storageAccountKey = "G36mc********Zup3h9kzj1w==";
        static void Main(string[] args)
        {
            string match_val  = null;
            using(FileStream fileStream = File.Open(@"D:\User\Pictures\test.png",FileMode.Open))
            using(MemoryStream memoryStream = new MemoryStream()){
                fileStream.CopyTo(memoryStream);
                match_val  = Convert.ToBase64String(memoryStream.ToArray());
            }

            StorageCredentials storageCredentials = new StorageCredentials(storageAccountName,storageAccountKey);
            CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(storageCredentials, true);
            CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("pub"); // For convenience, it is a public container, so I do not need to set access permission
            cloudBlobContainer.CreateIfNotExists();
            CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("test2.png");

            using(MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(match_val)))
            using(Image image = Image.FromStream(memoryStream,true,true))
            using(CloudBlobStream cloudBlobStream = cloudBlockBlob.OpenWrite()){
                image.Save(cloudBlobStream, System.Drawing.Imaging.ImageFormat.Png);
            }

            Console.WriteLine(cloudBlockBlob.Uri.AbsoluteUri);
            Console.ReadLine();
        }
    }

我没有 base64 编码的字符串。所以我在代码中生成了一个。不同之处在于:

  1. 我刚刚打开了cloudBlockBlob.OpenWrite() 的输出流
  2. 然后我使用 image.Save 方法将图像内容直接保存到 blob 流中。

最后,我得到了一个网址:https://storagetest789.blob.core.windows.net/pub/test2.png

我能够在浏览器中查看图像:

【讨论】:

  • 我不确定那里的区别。但是,肯定有区别,您的回答对我有用。
猜你喜欢
  • 2014-12-21
  • 1970-01-01
  • 1970-01-01
  • 2021-11-04
  • 1970-01-01
  • 2022-08-13
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
相关资源
最近更新 更多