在 Windows Azure 存储客户端中授权 blob 上传操作的正确方法是什么?
What is the proper way to authorize blob upload operation in Windows Azure Storage Client?
我需要创建一个库,允许我将 blob 数据上传到特定的 Azure 帐户。以下参数将由外部网络服务动态提供:
x-ms-date, x-ms-version, Content-MD5, x-ms-blob-type, Content-Type, Authorization
根据这里的例子:
我应该只能通过 URL 和 SasToken 上传 blob 数据,例如:
Uri blobUri = new Uri("http://127.0.0.1:10000/devstoreaccount1/sascontainer/myblob.txt");
// Create credentials with the SAS token. The SAS token was created in previous example.
StorageCredentials credentials = new StorageCredentials(sasToken);
// Create a new blob.
CloudBlockBlob blob = new CloudBlockBlob(blobUri, credentials);
// Upload the blob.
// If the blob does not yet exist, it will be created.
// If the blob does exist, its existing content will be overwritten.
using (var fileStream = System.IO.File.OpenRead(@"c:\Temp\myblob.txt"))
{
blob.UploadFromStream(fileStream);
}
但是,上面假设 sasToken 看起来类似于:
?sv=2015-07-08&sr=c&si=mypolicy&sig=FQctgR3waFrWpblkNJf6HYXAVa0%2BkxFUbP8Fr2op%2F%2FI%3D
虽然我的服务只会提供这个:
{
"Key": "Authorization",
"Value": "SharedKey storageAccountName:Tz7EqAl6OszIxGjBUk2qcxs82Af4Xq9CxyFx6u34LEI="
}
我在这里找到了一些例子:
https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx
关于如何使用 REST API:
Request Syntax:
PUT https://myaccount.blob.core.windows.net/mycontainer/myblockblob HTTP/1.1
Request Headers:
x-ms-version: 2015-02-21
x-ms-date: <date>
Content-Type: text/plain; charset=UTF-8
x-ms-blob-content-disposition: attachment; filename="fname.ext"
x-ms-blob-type: BlockBlob
x-ms-meta-m1: v1
x-ms-meta-m2: v2
Authorization: SharedKey myaccount:YhuFJjN4fAR8/AmBrqBz7MG2uFinQ4rkh4dscbj598g=
Content-Length: 11
Request Body:
hello world
但是由于我是 Azure 的新手,我不知道如何使用存储客户端执行上述操作...我是否必须以某种方式手动 "map" 提供 header 参数查询字符串参数还是有 better/other 方式?或者这可能不受存储客户端支持,我应该使用 REST 方法?
编辑 1:
明确一点:我 - 或多或少 - 知道如何使用 sas 令牌上传 blob,只要它具有以下形式:
问题是 - 不会提供此令牌 - 相反,我们尝试与之集成的网站将发布一个网络服务,而这个网络服务 (我们无法控制) 将提供以下五个参数(下面的示例数据):
{
"Key": "x-ms-date",
"Value": "Mon, 13 Jun 2016 10:22:05 GMT"
},
{
"Key": "x-ms-version",
"Value": "2015-04-05"
},
{
"Key": "Content-MD5",
"Value": "dnF5x6K/8ZZRzpfSlMMM+w=="
},
{
"Key": "x-ms-blob-type",
"Value": "BlockBlob"
},
{
"Key": "Content-Type",
"Value": "application/octet-stream"
},
{
"Key": "Authorization",
"Value": "SharedKey taxdocumentstorage09tst:sImBLJPWACNPzi94eJEDRU4Bt5hz9sPURjwy46KixmM="
}
您应该将 SAS 令牌附加到您的 URL,此处不需要 header "Authorization"。例如:
But since I'm new to Azure, I can't figure out how to do the above
with storage client... Do I have to somehow manually "map" provided
header parameters to query string parameters or is there a
better/other way? Or perhaps this is not supported by Storage Client,
and I should use REST approach?
您不能使用 Storage Client 库,因为它会计算其中一些参数,然后调用 REST API。您需要做的是直接使用 REST API。由于您获得了所需的所有值,您只需使用 HttpWebRequest
/WebClient
进行 REST API 调用,包括必要的 headers ,发送请求并相应地处理响应。
重要
请要求 Web 服务在他们发送的参数中也包含请求 URL,因为它用于计算授权 header。
我需要创建一个库,允许我将 blob 数据上传到特定的 Azure 帐户。以下参数将由外部网络服务动态提供:
x-ms-date, x-ms-version, Content-MD5, x-ms-blob-type, Content-Type, Authorization
根据这里的例子:
我应该只能通过 URL 和 SasToken 上传 blob 数据,例如:
Uri blobUri = new Uri("http://127.0.0.1:10000/devstoreaccount1/sascontainer/myblob.txt");
// Create credentials with the SAS token. The SAS token was created in previous example.
StorageCredentials credentials = new StorageCredentials(sasToken);
// Create a new blob.
CloudBlockBlob blob = new CloudBlockBlob(blobUri, credentials);
// Upload the blob.
// If the blob does not yet exist, it will be created.
// If the blob does exist, its existing content will be overwritten.
using (var fileStream = System.IO.File.OpenRead(@"c:\Temp\myblob.txt"))
{
blob.UploadFromStream(fileStream);
}
但是,上面假设 sasToken 看起来类似于:
?sv=2015-07-08&sr=c&si=mypolicy&sig=FQctgR3waFrWpblkNJf6HYXAVa0%2BkxFUbP8Fr2op%2F%2FI%3D
虽然我的服务只会提供这个:
{
"Key": "Authorization",
"Value": "SharedKey storageAccountName:Tz7EqAl6OszIxGjBUk2qcxs82Af4Xq9CxyFx6u34LEI="
}
我在这里找到了一些例子:
https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx
关于如何使用 REST API:
Request Syntax:
PUT https://myaccount.blob.core.windows.net/mycontainer/myblockblob HTTP/1.1
Request Headers:
x-ms-version: 2015-02-21
x-ms-date: <date>
Content-Type: text/plain; charset=UTF-8
x-ms-blob-content-disposition: attachment; filename="fname.ext"
x-ms-blob-type: BlockBlob
x-ms-meta-m1: v1
x-ms-meta-m2: v2
Authorization: SharedKey myaccount:YhuFJjN4fAR8/AmBrqBz7MG2uFinQ4rkh4dscbj598g=
Content-Length: 11
Request Body:
hello world
但是由于我是 Azure 的新手,我不知道如何使用存储客户端执行上述操作...我是否必须以某种方式手动 "map" 提供 header 参数查询字符串参数还是有 better/other 方式?或者这可能不受存储客户端支持,我应该使用 REST 方法?
编辑 1:
明确一点:我 - 或多或少 - 知道如何使用 sas 令牌上传 blob,只要它具有以下形式:
问题是 - 不会提供此令牌 - 相反,我们尝试与之集成的网站将发布一个网络服务,而这个网络服务 (我们无法控制) 将提供以下五个参数(下面的示例数据):
{
"Key": "x-ms-date",
"Value": "Mon, 13 Jun 2016 10:22:05 GMT"
},
{
"Key": "x-ms-version",
"Value": "2015-04-05"
},
{
"Key": "Content-MD5",
"Value": "dnF5x6K/8ZZRzpfSlMMM+w=="
},
{
"Key": "x-ms-blob-type",
"Value": "BlockBlob"
},
{
"Key": "Content-Type",
"Value": "application/octet-stream"
},
{
"Key": "Authorization",
"Value": "SharedKey taxdocumentstorage09tst:sImBLJPWACNPzi94eJEDRU4Bt5hz9sPURjwy46KixmM="
}
您应该将 SAS 令牌附加到您的 URL,此处不需要 header "Authorization"。例如:
But since I'm new to Azure, I can't figure out how to do the above with storage client... Do I have to somehow manually "map" provided header parameters to query string parameters or is there a better/other way? Or perhaps this is not supported by Storage Client, and I should use REST approach?
您不能使用 Storage Client 库,因为它会计算其中一些参数,然后调用 REST API。您需要做的是直接使用 REST API。由于您获得了所需的所有值,您只需使用 HttpWebRequest
/WebClient
进行 REST API 调用,包括必要的 headers ,发送请求并相应地处理响应。
重要
请要求 Web 服务在他们发送的参数中也包含请求 URL,因为它用于计算授权 header。