我有一个通过 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()));
到
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DiagnosticsConnectionString"));
但我会将“Gaurav Mantri”回复标记为正确。如果不是他的洞察力,我的图像不会上传到 blob。