[英]HttpResponseMessage Redirect to Private Azure Blob Storage
使用 asp.net 開發 API
是否可以將用戶重定向到私有的 azure blob 存儲? 我可以使用 SAS 密鑰或 azure blob SDK 執行此操作嗎?
例如,我想做這樣的事情:
var response = Request.CreateResponse(HttpStatusCode.Moved);
response.Headers.Location = new Uri(bloburl);
return response;
是否可以通過在 URL 中放置密鑰來訪問私有 blob? 顯然,我不想放置主密鑰。
是否可以將用戶重定向到私有的 azure blob 存儲? 我可以使用 SAS 密鑰或 azure blob SDK 執行此操作嗎?
是的,完全可以將用戶重定向到私有 blob。 您需要創建一個至少具有Read
權限的Shared Access Signature (SAS)
,並將該 SAS 令牌附加到您的 blob URL 並重定向到該 URL。
您的代碼如下所示:
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.