Can I get a blob by using its url?
Asked Answered
C

4

19

I am storing the Azure blob url into my database.Can I get the blob by using that url? Actually I need to update the blob and while doing that I need validations. So I need to convert that database entity model to my local model and apply the validations for them.But in my local model I am having Id,Name,HttpPostedFileBase file.When I am inserting the blob I am getting the blob url and saving it in database.But how to retrieve that blob while updating? This is my local Model

public class BlobAppModel
    {
        public int Id { get; set; }
        [Required(ErrorMessage="Please enter the name of the image")]
        [Remote("IsNameAvailable","Home",HttpMethod="POST",ErrorMessage="Name Already Exists")]
        public string Name { set; get; }
         [Required(ErrorMessage="Please select an image file")]
        public HttpPostedFileBase File { set; get; }

    } 

An My entitymodel is this one

public partial class BlobApp
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Uri { get; set; }
    }

when I am Editing it I need to get the blob ..I am stuck here..Can anyone help me out?

public ActionResult Edit(string Id)
        {
            var data=BlobManager.GetBlob(Convert.ToInt32(Id));
            BlobStorageServices _blobstorageservice = new BlobStorageServices();
            CloudBlobContainer container = _blobstorageservice.GetCloudBlobContainer();
            CloudBlockBlob blob = container.GetBlockBlobReference(data.Uri.ToString());

            BlobAppModel model = new BlobAppModel { Id = data.Id, Name = data.Name, File =//This is where I need to get the file//};
            return View("Edit",BlobManager.GetBlob(Convert.ToInt32(Id)));
        }
Chromosphere answered 1/11, 2013 at 8:57 Comment(0)
E
6

The best way to access a blob is by accessing the storage with the container name and the blob reference, as explained here: https://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/#download-blobs In your code you need to change the blob reference to the name you set when uploading, no to the uri.

CloudBlockBlob blob = container.GetBlockBlobReference(data.Uri.ToString());

use this instead:

CloudBlockBlob blob = container.GetBlockBlobReference("yourfile.jpg");

If you have the blob url, and the container is set to public access you can get the data by simply downloading it using a regular http client.

Ellamaeellan answered 2/11, 2013 at 20:33 Comment(1)
what if we dont know the container without looking at the absolute URI?Palatalized
B
29
   CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);

    CloudBlockBlob blob = new CloudBlockBlob(new Uri(imgUrl),storageAccount.Credentials);
Bosanquet answered 4/6, 2014 at 11:48 Comment(2)
Personally - This is what helped me. Thanks!Alicia
what if we dont know the container without looking at the absolute URI? is there a shortcut to getting the container frmo the uri?Palatalized
P
11

As Cristian also mentioned, you can use GetBlockBlobReference if you have the name of your blob. Otherwise, if you would like to use the full URL, you can simply create a new CloudBlockBlob object by using one of its constructors that take a Uri and a StorageCredentials object. You may not even need the StorageCredentials object if the Uri you have contains SAS credentials or the blob is public.

Plaided answered 4/11, 2013 at 23:19 Comment(1)
Upvoted because this solution answers the question as asked without putting requirement to get other input or configuration on the server side which may well not be an option to the client. The URL for the doc is broken however, the current one is learn.microsoft.com/en-us/dotnet/api/…Venom
E
6

The best way to access a blob is by accessing the storage with the container name and the blob reference, as explained here: https://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/#download-blobs In your code you need to change the blob reference to the name you set when uploading, no to the uri.

CloudBlockBlob blob = container.GetBlockBlobReference(data.Uri.ToString());

use this instead:

CloudBlockBlob blob = container.GetBlockBlobReference("yourfile.jpg");

If you have the blob url, and the container is set to public access you can get the data by simply downloading it using a regular http client.

Ellamaeellan answered 2/11, 2013 at 20:33 Comment(1)
what if we dont know the container without looking at the absolute URI?Palatalized
E
3

it is possible to get blob reference using blob Uri and container. For example if you need to update or delete blob and you already have container with credentials setup:

var blob = container.ServiceClient.GetBlobReferenceFromServer(blobUri);
Eyesore answered 21/1, 2016 at 14:18 Comment(1)
what if we dont know the container without looking at the absolute URI?Palatalized

© 2022 - 2024 — McMap. All rights reserved.