none
create and write html file on blob storage of windows azure

    Question

  • Currently, i am not able to create and write the html file on the windowsAzure blob storage service, 
    because when i provide path to the streamwriter class parameter, it gives me an error. 
    System.IO.StreamWriter sw = new System.IO.StreamWriter(pHtmlFileNameWithFullPath, false);

    path="https://xyz.blob.core.windows.net/2-messagefiles/Message_161.html";

    error: "The given path's format is not supported."


    Brijesh Vaidya India

    Monday, August 19, 2013 5:15 AM

Answers

  • As I mentioned in my previous response, you can't write blobs like that. You would have to use storage client library for that. CloudBlockBlob has a method called "UploadFromStream" which would need to use. First, you would need to read your image to a stream and then pass that stream to UploadFromStream method to save the blob.

    Hope this helps.

    • Marked as answer by Akshar_brahma Tuesday, August 27, 2013 11:43 AM
    Saturday, August 24, 2013 7:34 AM
  • Hi Brijesh,

    this is because that's not how you upload to blob storage, please follow this link to get started with Blob Storage

    http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/

    upload to blob

    I hope this sample helps

    ----------------------------------------------

    Please mark as answered if it helped


    Please mark as answered if it helped Vishal Narayan Saxena http://twitter.com/vishalishere http://www.ogleogle.com/vishal/

    Monday, August 19, 2013 5:28 AM
  • finally i resolved the issue, thx gaurav!!!

    here is my code:

    private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                    WebBrowser webBrowser = (WebBrowser)sender;
                    webBrowser.ClientSize = new Size(this.width, this.height);
                    webBrowser.ScrollBarsEnabled = false;
                    bmp = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
                    webBrowser.BringToFront();
                    webBrowser.DrawToBitmap(bmp, webBrowser.Bounds);
                    Image img = bmp.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
    
                    var ms = new MemoryStream();
                    img.Save(ms, ImageFormat.Png);
    
                    // If you're going to read from the stream, you may need to reset the position to the start
                    ms.Position = 0;
    
                    string fileName = url.Replace(".html", "").Substring(url.LastIndexOf("/")).Replace("/", "") + ".jpg";
                    fileName = System.Web.HttpUtility.UrlEncode(fileName);
                    if (absolutePath != null && !File.Exists(absolutePath + fileName))
                    {
                     
                        CloudBlobClient objCBClient; // for azure
                        CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
                        {
                            configSetter(Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(configName));
                        });
                        // Get the storage account reference
                        CloudStorageAccount objStorage = CloudStorageAccount.FromConfigurationSetting("BlobConnectionString");
    
                        // get the Client reference using storage blobend point
                        objCBClient = new CloudBlobClient(objStorage.BlobEndpoint, objStorage.Credentials);
                        string blobpath = GetBlobContentPath("2", "BlobConnectionString");
                        string msgFullPath = string.Empty;
                        if (!string.IsNullOrEmpty(blobpath))
                        {
                            msgFullPath = blobpath + "-messagefiles";
                        }
    
                        // Get Container reference
                        CloudBlobContainer objContainer = objCBClient.GetContainerReference(msgFullPath.ToLower());
    
                        // Get blob reference
                        objContainer.CreateIfNotExist();
                        var permission = objContainer.GetPermissions();
                        permission.PublicAccess = Microsoft.WindowsAzure.StorageClient.BlobContainerPublicAccessType.Container;
                        objContainer.SetPermissions(permission);
    
                        string strFilePath = GetBlobContentPath(msgFullPath, "BlobConnectionString") + "/" + fileName;
                        CloudBlob objCBlob = objContainer.GetBlockBlobReference(strFilePath);
    
                        // Set meta values
                        objCBlob.Metadata["MetaName"] = "meta";
    
                        // Open a stream using the cloud object
                        BlobStream blobstream = objCBlob.OpenWrite();
                        Byte[] bytContent = ConvertImageToByteArray(img);
    
                        long filelen = bytContent.Length;
    
                        // Write the stream to the blob database
                        blobstream.Write(bytContent, 0, Convert.ToInt32(filelen));
                        blobstream.Close();
                    }
                    bmp = (Bitmap)img;
                    webBrowser.Dispose();
                    if (mre != null)
                        mre.Set();
                }
            }
    

    thank you very much.


    Brijesh Vaidya India

    • Marked as answer by Akshar_brahma Tuesday, August 27, 2013 11:44 AM
    Tuesday, August 27, 2013 11:43 AM

All replies

  • Hi Brijesh,

    this is because that's not how you upload to blob storage, please follow this link to get started with Blob Storage

    http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/

    upload to blob

    I hope this sample helps

    ----------------------------------------------

    Please mark as answered if it helped


    Please mark as answered if it helped Vishal Narayan Saxena http://twitter.com/vishalishere http://www.ogleogle.com/vishal/

    Monday, August 19, 2013 5:28 AM
  • You can't create blob files like this. To create a blob, you would need to use storage client library. You would do something like this:

                CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
    
                string blobFileContents = "<html>This is sample html</html>";
    
                CloudBlockBlob blob = new CloudBlockBlob(new Uri("your blob URL"), storageAccount.Credentials);
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(blobFileContents)))
                {
                    blob.UploadFromStream(ms);
                }
    
    Hope this helps.

    Monday, August 19, 2013 5:29 AM
  • thank you Gaurav,

    now i am able to write the html file on blob, but

    i need to convert this html file into an image (using websitethumbnail.cs class) using webbrowser to capture html page and convert into image,

    upto this i am able to convert the html to image,

    but when i want to save that image onto the blob, i got following error:

    private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                WebBrowser webBrowser = (WebBrowser)sender;
                webBrowser.ClientSize = new Size(this.width, this.height);
                webBrowser.ScrollBarsEnabled = false;
                bmp = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
                webBrowser.BringToFront();
                webBrowser.DrawToBitmap(bmp, webBrowser.Bounds);
                Image img = bmp.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
    
    
                string fileName = url.Replace(".html", "").Substring(url.LastIndexOf("/")).Replace("/", "") + ".jpg"; 
                fileName = System.Web.HttpUtility.UrlEncode(fileName);
                if (absolutePath != null && !File.Exists(absolutePath + fileName))
                {
                    absolutePath = "https://xyz.blob.core.windows.net/2-messagefiles";
                    string p = Path.Combine(absolutePath, fileName);
    		img.Save(p);  //here i got the error:  the given path's format is not supported
    
                    //img.Save(absolutePath, fileName);
                    //img.Save("xyz.jpg");
    
                }
                bmp = (Bitmap)img;
                webBrowser.Dispose();
                if (mre != null)
                    mre.Set();
            }

    can you please help me?


    Brijesh Vaidya India

    Saturday, August 24, 2013 6:23 AM
  • As I mentioned in my previous response, you can't write blobs like that. You would have to use storage client library for that. CloudBlockBlob has a method called "UploadFromStream" which would need to use. First, you would need to read your image to a stream and then pass that stream to UploadFromStream method to save the blob.

    Hope this helps.

    • Marked as answer by Akshar_brahma Tuesday, August 27, 2013 11:43 AM
    Saturday, August 24, 2013 7:34 AM
  • Hi,

    i implemented as per your instruction, but following error occured,

     private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                try
                {
    
                    webBrowser.ClientSize = new Size(this.width, this.height);
                    webBrowser.ScrollBarsEnabled = false;
                    bmp = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
                    webBrowser.BringToFront();
                    webBrowser.DrawToBitmap(bmp, webBrowser.Bounds);
                    Image img = bmp.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
    
                    var ms = new MemoryStream();
                    img.Save(ms, ImageFormat.Png);
    
    string fileName = url.Replace(".html", "").Substring(url.LastIndexOf("/")).Replace("/", "") + ".jpg"; 
    
                    fileName = System.Web.HttpUtility.UrlEncode(fileName);
                    if (absolutePath != null && !File.Exists(absolutePath + fileName))
                    {
    
                        string p = Path.Combine(absolutePath, fileName);
    
                        // Retrieve storage account from connection string.
                        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                            CloudConfigurationManager.GetSetting("BlobConnectionString"));
    
                        // Create the blob client.
                        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    
                        // Retrieve a reference to a container. 
                        CloudBlobContainer container = blobClient.GetContainerReference(absolutePath);
    
                        // Create the container if it doesn't already exist.
                        container.CreateIfNotExist();
    
                        container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
    
    
                        CloudBlockBlob blockBlob = container.GetBlockBlobReference(absolutePath);
    
                       blockBlob.UploadFromStream(ms);
    

    but i got StorageClientException, when uploadfromstream called,

    "The requested URI does not represent any resource on the server."

    and inner exception is:
    {"The remote server returned an error: (400) Bad Request."}


    Brijesh Vaidya India

    Monday, August 26, 2013 1:27 PM
  • First thing I would check is the name of the blob container (absolutePath in your code). Another thing I noticed in your code is that you're using absolutePath for both blob container name and blob name. See these two lines of code:

    CloudBlobContainer container = blobClient.GetContainerReference(absolutePath);

    CloudBlockBlob blockBlob = container.GetBlockBlobReference(absolutePath);

    You may want to check that. Also if you're running this code locally, I would recommend running Fiddler along with it. Fiddler will give you more details about the 400 error you're getting.

    Monday, August 26, 2013 1:34 PM
  • i correct the code and now i will take image into stream and in the blobstream, i can read it and write on to the blob like below:

    private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                try
                {
    
                    WebBrowser webBrowser = (WebBrowser)sender;
    
                    webBrowser.ClientSize = new Size(this.width, this.height);
                    webBrowser.ScrollBarsEnabled = false;
                    bmp = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
                    webBrowser.BringToFront();
                    webBrowser.DrawToBitmap(bmp, webBrowser.Bounds);
                    Image img = bmp.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
    
                    var ms = new MemoryStream();
                    img.Save(ms, ImageFormat.Png);
    
                    // If you're going to read from the stream, you may need to reset the position to the start
                   ms.Position = 0;
    
                    string fileName = url.Replace(".html", "").Substring(url.LastIndexOf("/")).Replace("/", "") + ".jpg"; //19-aug
    
                    fileName = System.Web.HttpUtility.UrlEncode(fileName);
                    if (absolutePath != null && !File.Exists(absolutePath + fileName))
                    {
    
                       CloudBlobClient objCBClient; // for azure
    
                        CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
                        {
                            configSetter(Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(configName));
                        });
    
    
                        // Get the storage account reference
                        CloudStorageAccount objStorage = CloudStorageAccount.FromConfigurationSetting("BlobConnectionString");
    
                        // get the Client reference using storage blobend point
                        objCBClient = new CloudBlobClient(objStorage.BlobEndpoint, objStorage.Credentials);
                        string blobpath = GetBlobContentPath("2", "BlobConnectionString");
                        string msgFullPath = string.Empty;
                        if (!string.IsNullOrEmpty(blobpath))
                        {
                            msgFullPath = blobpath + "-messagefiles";
                        }
    
                        // Get Container reference
                        CloudBlobContainer objContainer = objCBClient.GetContainerReference(msgFullPath.ToLower());
    
                       // Get blob reference
                        objContainer.CreateIfNotExist();
                        var permission = objContainer.GetPermissions();
                        permission.PublicAccess = Microsoft.WindowsAzure.StorageClient.BlobContainerPublicAccessType.Container;
                        objContainer.SetPermissions(permission);
    
                        string strFilePath = GetBlobContentPath(msgFullPath, "BlobConnectionString") + "/image1.jpg";
                        CloudBlob objCBlob = objContainer.GetBlockBlobReference(strFilePath);
    
                        // Set meta values
                        objCBlob.Metadata["MetaName"] = "meta";
                        // Open a stream using the cloud object
                    BlobStream blobstream = objCBlob.OpenWrite();
    
                    Byte[] bytContent = ConvertImageToByteArray(img);
    
                    long filelen = bytContent.Length;
    
    
                    blobstream.Read(bytContent, 0, Convert.ToInt32(filelen)); // here i got error: 
    
                    // Write the stream to the blob database
                    blobstream.Write(bytContent, 0, Convert.ToInt32(filelen));
                    blobstream.Close();
    }


    'blobstream.ReadAheadSize' threw an exception of type 'System.NotSupportedException'


    -        base    {"Specified method is not supported."}    System.SystemException {System.NotSupportedException}

    this error occured..


    Brijesh Vaidya India

    Tuesday, August 27, 2013 7:26 AM
  • finally i resolved the issue, thx gaurav!!!

    here is my code:

    private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                    WebBrowser webBrowser = (WebBrowser)sender;
                    webBrowser.ClientSize = new Size(this.width, this.height);
                    webBrowser.ScrollBarsEnabled = false;
                    bmp = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
                    webBrowser.BringToFront();
                    webBrowser.DrawToBitmap(bmp, webBrowser.Bounds);
                    Image img = bmp.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
    
                    var ms = new MemoryStream();
                    img.Save(ms, ImageFormat.Png);
    
                    // If you're going to read from the stream, you may need to reset the position to the start
                    ms.Position = 0;
    
                    string fileName = url.Replace(".html", "").Substring(url.LastIndexOf("/")).Replace("/", "") + ".jpg";
                    fileName = System.Web.HttpUtility.UrlEncode(fileName);
                    if (absolutePath != null && !File.Exists(absolutePath + fileName))
                    {
                     
                        CloudBlobClient objCBClient; // for azure
                        CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
                        {
                            configSetter(Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(configName));
                        });
                        // Get the storage account reference
                        CloudStorageAccount objStorage = CloudStorageAccount.FromConfigurationSetting("BlobConnectionString");
    
                        // get the Client reference using storage blobend point
                        objCBClient = new CloudBlobClient(objStorage.BlobEndpoint, objStorage.Credentials);
                        string blobpath = GetBlobContentPath("2", "BlobConnectionString");
                        string msgFullPath = string.Empty;
                        if (!string.IsNullOrEmpty(blobpath))
                        {
                            msgFullPath = blobpath + "-messagefiles";
                        }
    
                        // Get Container reference
                        CloudBlobContainer objContainer = objCBClient.GetContainerReference(msgFullPath.ToLower());
    
                        // Get blob reference
                        objContainer.CreateIfNotExist();
                        var permission = objContainer.GetPermissions();
                        permission.PublicAccess = Microsoft.WindowsAzure.StorageClient.BlobContainerPublicAccessType.Container;
                        objContainer.SetPermissions(permission);
    
                        string strFilePath = GetBlobContentPath(msgFullPath, "BlobConnectionString") + "/" + fileName;
                        CloudBlob objCBlob = objContainer.GetBlockBlobReference(strFilePath);
    
                        // Set meta values
                        objCBlob.Metadata["MetaName"] = "meta";
    
                        // Open a stream using the cloud object
                        BlobStream blobstream = objCBlob.OpenWrite();
                        Byte[] bytContent = ConvertImageToByteArray(img);
    
                        long filelen = bytContent.Length;
    
                        // Write the stream to the blob database
                        blobstream.Write(bytContent, 0, Convert.ToInt32(filelen));
                        blobstream.Close();
                    }
                    bmp = (Bitmap)img;
                    webBrowser.Dispose();
                    if (mre != null)
                        mre.Set();
                }
            }
    

    thank you very much.


    Brijesh Vaidya India

    • Marked as answer by Akshar_brahma Tuesday, August 27, 2013 11:44 AM
    Tuesday, August 27, 2013 11:43 AM