簡體   English   中英

如何使用文件服務將二進制文件 (PDF) 上傳到 Azure

[英]How to upload a binary file (PDF) to Azure using File Services

我正在嘗試使用 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

請嘗試設置您的_requestMessageContent屬性。 我假設變量ssBestandsBinary是一個字節數組,因此您可以將Content屬性設置為ByteArrayContent

您的代碼可能類似於(雖然未經測試):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM