HttpResponseMessage Redirect to Private Azure Blob Storage

♀尐吖头ヾ 提交于 2019-12-10 10:54:05

问题


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.


回答1:


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;


来源:https://stackoverflow.com/questions/42162759/httpresponsemessage-redirect-to-private-azure-blob-storage

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