azure - 如何使用文件服务将二进制文件 (PDF) 上传到 Azure
问题描述
我正在尝试使用 C# 中的服务将二进制文件(PDF 或 Word)上传到 Azure。我仅限于使用 REST API,因此我不能使用 SDK。
我已经设法创建了文件,但是我应该使用哪个 REST API 来上传内容。据我所见, PUT RANGE 仅适用于文本(不适用于二进制内容)。我应该使用哪个文件服务 REST API?或者文件服务 API 不是为此而设计的,我应该使用 Blob API 吗?
最好的问候,米歇尔
string endpoint_putrange = "https://" + ssStorageAccount + ".file.core.windows.net/" + ssShareNaam + "/" + ssBestandsNaam + "?comp=range";
Uri _endpoint_putrange = new Uri(endpoint_putrange);
HttpRequestMessage _requestMessage = new HttpRequestMessage(HttpMethod.Put, _endpoint_putrange);
HttpClient httpClient = new HttpClient();
httpClient.Timeout = new TimeSpan(20000 * 10000); // 20sec in ticks
httpClient.BaseAddress = _endpoint_putrange;
string RequestDateString = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
// First set the header and construct the canonicalHeader string. See
// These must be in alpabetical order !!!!
string canonicalHeaders = string.Empty;
canonicalHeaders += setHeader(httpClient, "Content-Length", ssBestandsBinary.Length.ToString());
canonicalHeaders += setHeader(httpClient, "x-ms-date", RequestDateString);
canonicalHeaders += setHeader(httpClient, "x-ms-range", "bytes=0-"); // https://docs.microsoft.com/en-us/rest/api/storageservices/specifying-the-range-header-for-file-service-operations
canonicalHeaders += setHeader(httpClient, "x-ms-version", "2020-04-08"); // https://docs.microsoft.com/nl-nl/rest/api/storageservices/versioning-for-the-azure-storage-services
canonicalHeaders += setHeader(httpClient, "x-ms-write", "Update");
// Construct the canonicalResource string
string canonicalResource = "/" + ssStorageAccount + "/" + ssShareNaam + "/" + ssBestandsNaam;
// Construct the string to Sign
string stingToSign = "PUT\n" + /*HTTP Verb*/
"\n" + /*Content-Encoding*/
"\n" + /*Content-Language*/
ssBestandsBinary.Length.ToString() + "\n" + /*Content-Length (empty string when zero)*/
"\n" + /*Content-MD5*/
"\n" + /*Content-Type*/
"\n" + /*Date*/
"\n" + /*If-Modified-Since */
"\n" + /*If-Match*/
"\n" + /*If-None-Match*/
"\n" + /*If-Unmodified-Since*/
"bytes=0-" + "\n" + /*Range*/
canonicalHeaders + /*CanonicalizedHeaders*/
canonicalResource; /*CanonicalizedResource*/
// Sign it
HMACSHA256 hmac = new HMACSHA256();
byte[] dataToHmac = Encoding.UTF8.GetBytes(stingToSign);
string signature = Convert.ToBase64String(hmac.ComputeHash(dataToHmac));
// Add the authorization header
string authorizationHeader = ssStorageAccount + ":" + signature;
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("SharedKey", authorizationHeader);
// How to specify the binary content of the _requestmessage/httpClient
/// ?????????????????????????
// Do the (synchronous) call and collect the response
HttpResponseMessage httpResponse = httpClient.SendAsync(_requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None).GetAwaiter().GetResult();
..... etc etc
解决方案
请尝试通过设置Content
您的_requestMessage
. 我假设该变量ssBestandsBinary
是一个字节数组,因此您可以将Content
属性设置为ByteArrayContent
.
您的代码可能类似于(虽然未经测试):
_requestMessage.Content = new ByteArrayContent(ssBestandsBinary);
HttpResponseMessage httpResponse = httpClient.SendAsync(_requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None).GetAwaiter().GetResult();
推荐阅读
- html - 如何查看 HTML 页面中的选项卡是否存在,如果存在则链接到它以抓取数据?
- oauth-2.0 - JWT 访问令牌:矛盾?
- c++ - 重载解决方案:是否首选直接转换运算符(作为复制省略的结果)?
- c# - WPF 动态添加带有详细信息的 ListViewItem
- python - 如何使用客户端 ID 和密码访问 spotify 的 Web api?
- node.js - 通过 Tedious 调用一个简单的存储过程需要这么长时间是否有原因?
- python - 熊猫“面具”减慢 PowerBI 查询?
- dax - 按 DateDiff 细分的 DAX 分组
- php - 如何在 PHP 中压缩整个文件夹,甚至是空文件夹?
- javascript - SweetAlert2 表单验证不返回