首页 > 解决方案 > 如何使用文件服务将二进制文件 (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

标签: azurefileservicebinary

解决方案


请尝试通过设置Content您的_requestMessage. 我假设该变量ssBestandsBinary是一个字节数组,因此您可以将Content属性设置为ByteArrayContent.

您的代码可能类似于(虽然未经测试):

_requestMessage.Content = new ByteArrayContent(ssBestandsBinary);
HttpResponseMessage httpResponse = httpClient.SendAsync(_requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None).GetAwaiter().GetResult();

推荐阅读