当前位置: 首页 > 知识库问答 >
问题:

将MemoryStream文件存储到Azure Blob

荣俊
2023-03-14

我有一个通过System.Drawing动态生成的图像。然后,我将生成的图像输出到memorystream以存储到我的Azure blob中。

但我似乎无法将我的文件存储在我选择的blob中。没有发生错误,并且我的图像成功地保存到MemoryStream。不出所料,我的blob是空的。

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue("CMSAzureAccountName").ToString(), Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue("CMSAzureSharedKey").ToString()));

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("myimagecontainer");

// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("test.jpg");
            
//Output image
ImageCodecInfo[] Info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
EncoderParameters Params = new System.Drawing.Imaging.EncoderParameters(1);
Params.Param[0] = new EncoderParameter(Encoder.Quality, 100L);

System.IO.MemoryStream msImage = new System.IO.MemoryStream();
GenerateImage.Render(imageOutput).Save(msImage, Info[1], Params); //GenerateImage.Render() method creates a custom image and returns a Bitmap
            
// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = msImage)
{
    blockBlob.UploadFromStream(fileStream);
}
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue("CMSAzureAccountName").ToString(), Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue("CMSAzureSharedKey").ToString()));

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DiagnosticsConnectionString"));

但我会标记“Gaurav Mantri”的回应是正确的。如果不是他的洞察力,我的图像就不会上传到Blob上。

共有1个答案

邵文乐
2023-03-14

尝试将内存流的位置设置为0,看看是否有帮助。类似下面的代码:

msImage.Position = 0;//Move the pointer to the start of stream.

// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = msImage)
{
    blockBlob.UploadFromStream(fileStream);
}
 类似资料:
  • 我相当愚蠢地上传了一个vhd到Azure文件存储,以为我可以从它创建一个虚拟机,却发现它真的需要在Blob存储中。 我知道我可以再上传一次,但它非常大,我的上传速度非常慢。 我的问题是-我可以将文件从文件存储移动到blob存储,而无需再次下载/上传吗?也就是说,Azure门户UI中是否有任何东西可以执行此操作,甚至是PowerShell命令?

  • 我尝试用以下代码保存从internet下载的文件 但在运行时,我得到的错误如下 03-04 20:42:51.080 8972-8972/com.example.me.demo2 E/BitmapFactory:无法解码流:java.io.FileNotFoundExcoop: /storage/emulated/0/.tanks/4a100abb-0e55-4062-8c37-f11f4189e

  • 我试图使用Adobe reader读取从服务器下载的pdf文件,但问题是当我将其存储在内部存储时,其他应用程序无法读取该文件。现在我想知道如何将此文件复制到外部存储(/sdcard/)中,以便pdf查看器可以查看。 由于安全原因,我将文件存储在内部存储,然后删除外部存储的文件。 我的问题是如何复制保存在内部存储中的文件,而不使用raw或将其放入InputStream中的资产。

  • 问题内容: 我的hangman程序有问题。我真的认为我需要做的事超出了我对Java的了解。这是我的代码 我能够使程序读取文件,然后打印到屏幕上,但是我不知道如何将文件中的单词存储到数组中。我一点都没有进步,所以请尝试并做到透彻。 问题答案: 您需要将读取的行保存在一个对象中,并将其分配给数组的某个字段。例如: 这会将值赋给数组的第一个字段。

  • 我想通过运行在Azure VM上的FTP服务器与用户共享Azure Blob存储中的文件。 据我所知,您不能在VM上挂载Blob存储,但可以使用“网络使用”挂载Azure文件共享。 Blob存储上的文件将以增量方式上载,因此理想情况下,我希望在上载时将其复制到Azure文件,Azure功能似乎是理想的方式,因为它们很容易为我设置和处理Blob存储上的触发器。 我如何使用Azure功能将文件从Blo