我有一个通过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。
尝试将内存流的位置设置为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);
}
我有同样的问题,代码可以工作,但是pdf文件的大小始终为0,我必须使MyMemoryStream.Position = 0;以下是我使用的代码。希望它能帮助到别人。
`using (pdfStream)
{
pdfStream.Position = 0;
blob.UploadFromStream(pdfStream);
_model.ConvertedPdfUrl = blob.Uri.AbsoluteUri;
}`