Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpResponseMessage Redirect to Private Azure Blob Storage

Developing an API using asp.net

Is it possible to redirect a user to a private azure blob storage? Can i do this using SAS keys or the azure blob SDK?

For example I want to do something like this:

var response = Request.CreateResponse(HttpStatusCode.Moved);
response.Headers.Location = new Uri(bloburl);
return response;

Is it possible to access a private blob by putting a key in the URL? Obviously i dont want to put the master key though.

like image 481
Jaked222 Avatar asked Feb 10 '17 15:02

Jaked222


People also ask

How do I access private Azure blob storage?

If you own the access key, then you must generate a SAS token using it, and then access the file yourself using the SAS token. There are libraries to do a lot of the heavy lifting for you. In that case, you can simply use that: docs.microsoft.com/en-us/azure/storage/blobs/….

Can Azure storage blob access policy be set to private?

Azure Storage supports optional anonymous public read access for containers and blobs. By default, anonymous access to your data is never permitted. Unless you explicitly enable anonymous access, all requests to a container and its blobs must be authorized.

How do I access blob storage by URL?

By default, the URL for accessing the Blob service in a storage account is https://<your account name>. blob.core.windows.net. You can map your own domain or subdomain to the Blob service for your storage account so that users can reach it using the custom domain or subdomain.

Can you access Azure blob storage from browser?

HTTP Request. You can also retrieve a blob using an HTTPS/HTTP request. One way to find the URL of the blob is by using the Azure portal by going to Home > Storage Account > Container > Blob > Properties. However, probably the easiest way is to find the blob in the Storage Explorer, right-click, then select 'Copy URL'.


Video Answer


1 Answers

Is it possible to redirect a user to a private azure blob storage? Can i do this using SAS keys or the azure blob SDK?

Yes, it is entirely possible to redirect a user to a private blob. You would need to create a Shared Access Signature (SAS) with at least Read permission and append that SAS token to your blob URL and do a redirect to that URL.

Your code would look something like this:

        var cred = new StorageCredentials(accountName, accountKey);
        var account = new CloudStorageAccount(cred, true);
        var client = account.CreateCloudBlobClient();
        var container = client.GetContainerReference("container-name");
        var blob = container.GetBlockBlobReference("blob-name");
        var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
        {
            Permissions = SharedAccessBlobPermissions.Read,
            SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1)//Assuming you want the link to expire after 1 hour
        });
        var blobUrl = string.Format("{0}{1}", blob.Uri.AbsoluteUri, sasToken);
        var response = Request.CreateResponse(HttpStatusCode.Moved);
        response.Headers.Location = new Uri(bloburl);
        return response;
like image 145
Gaurav Mantri Avatar answered Nov 14 '22 23:11

Gaurav Mantri