If using ImageResizer with Azure blobs do I need the AzureReader2 plugin?

青春壹個敷衍的年華 提交于 2019-12-01 08:01:33

With some trepidation, I'm going to disagree with astaykov here. I believe you CAN use ImageResizer with Azure WITHOUT needing AzureReader2. Maybe I should qualify that by saying 'It works on my setup' :)

I'm using ImageResizer in an MVC 3 application. I have a standard Azure account with an images container.

Here's my test code for the view:

@using (Html.BeginForm( "UploadPhoto", "BasicProfile", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file" />
    <input type="submit" value="OK" />
}

And here's the corresponding code in the Post Action method:

// This action handles the form POST and the upload
[HttpPost]
public ActionResult UploadPhoto(HttpPostedFileBase file)
{
    // Verify that the user selected a file
    if (file != null && file.ContentLength > 0)
    {
        string newGuid = Guid.NewGuid().ToString();

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);

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

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

        // Retrieve reference to the blob we want to create            
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(newGuid + ".jpg");

        // Populate our blob with contents from the uploaded file.
        using (var ms = new MemoryStream())
        {
            ImageResizer.ImageJob i = new ImageResizer.ImageJob(file.InputStream,
                    ms, new ImageResizer.ResizeSettings("width=800;height=600;format=jpg;mode=max"));
            i.Build();

            blockBlob.Properties.ContentType = "image/jpeg";
            ms.Seek(0, SeekOrigin.Begin);
            blockBlob.UploadFromStream(ms);
        }
    }

    // redirect back to the index action to show the form once again
    return RedirectToAction("UploadPhoto");
}

This is 'rough and ready' code to test the theory and could certainly stand improvement but, it does work both locally and when deployed on Azure. I can also view the images I've uploaded, which are correctly re-sized.

Hope this helps someone.

The answer to the concrete question:

If using ImageResizer with Azure blobs do I need the AzureReader2 plugin?

is YES. And as described in the Image Resizer's documentation - that plugin is used to read/process/serve images out of Blob Storage. So there is no doubt - if you are going to use Image Resizer, AzureReader2 is your needed plugin to make things right. It will take care of Blob uploads/serve.

Although I question Image Resizer's team competency on Windows Azure, since they are referencing Azure SDK v.2, while the most current version for Azure SDK is 1.8. What they mean is the Azure Storage Client Library, which has versions 1.7 and 2.x. Whereas version 2.x is recommended one to use and comes with Azure SDK 1.8. So, do not search for Azure SDK 2.0, install the latest one, which is 1.8. And by the way, use the Nuget Package Manager to install the Azure Storage Library v. 2.0.x.

You can also upload resized versions to azure. So, you first upload the original image as a blob, say with the name /original/xxx.jpg; then you create a resize of the image and upload that to azure with the name say /thumbnail/xxx.jpg. If you want to create the resized versions on the fly or on a separate thread, you may need to temporarily save the original to disk.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!