将MemoryStream文件存储到Azure Blob

16

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

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

我已确保我的 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()));

to

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

但是我将把"Gaurav Mantri"的回复标记为正确答案。如果不是他的洞见,我的图像就不会上传到Blob。

2个回答

42

尝试将内存流的位置设置为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);
}

啊!我实际上收到了以下错误消息:“服务器无法验证请求。请确保Authorization标头的值正确形成,包括签名。” - SurinderBhomra
1
这通常意味着您在配置文件中存储的帐户名称/密钥存在问题。您可以尝试执行一个简单的操作,比如创建一个 Blob 容器,看看是否会出现错误。在您上面的代码中创建 CloudBlobContainer 实例后,您可以简单地执行 container.CreateIfNotExists()。 - Gaurav Mantri
1
感谢你的帮助,Gaurav。我发现问题是由于CloudStorageAccount连接字符串引起的。但是,由于它有助于上传文件问题,我已将你的回复标记为正确答案。 - SurinderBhomra

3

我有同样的问题,代码可以运行,但是pdf文件大小总是为0,我不得不在下面加上MyMemoryStream.Position = 0; 这是我使用的代码。希望能对某些人有所帮助。

using (pdfStream)
{
    pdfStream.Position = 0;
    blob.UploadFromStream(pdfStream);                            
    _model.ConvertedPdfUrl = blob.Uri.AbsoluteUri;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接