HttpResponseMessage 重定向到私有 Azure Blob 存储

Jak*_*222 6 c# asp.net azure 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;
Run Code Online (Sandbox Code Playgroud)

是否可以通过将密钥放入 URL 来访问私有 blob?显然我不想放主钥匙。

Gau*_*tri 7

是否可以将用户重定向到私有 azure blob 存储?我可以使用 SAS 密钥或 azure blob SDK 执行此操作吗?

是的,完全有可能将用户重定向到私有 blob。您需要创建一个Shared Access Signature (SAS)至少Read具有权限的 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;
Run Code Online (Sandbox Code Playgroud)