예제 #1
0
파일: B2Http.cs 프로젝트: Digital3D/b2.net
        /// <summary>
        /// Call the B2 'Upload File' API (see https://www.backblaze.com/b2/docs/b2_upload_file.html)
        /// </summary>
        /// <param name="uploadUrl"></param>
        /// <param name="authorizationToken"></param>
        /// <param name="fileName"></param>
        /// <param name="contentType"></param>
        /// <param name="contentLength"></param>
        /// <param name="contentSha1"></param>
        /// <param name="fileInfo"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public async Task <UploadFileResponse> UploadFile(string uploadUrl, string authorizationToken, string fileName, string contentType, long contentLength, string contentSha1, Dictionary <string, string> attributes, Stream content)
        {
            if (attributes == null)
            {
                throw new ArgumentNullException("attributes");
            }

            Trace(() => $"UploadFile: uploadUrl={uploadUrl}, authorizationToken={authorizationToken}, fileName={fileName}, contentType={contentType}, contentLength={contentLength}, contentSha1={contentSha1}, attributes={ToString(attributes)}");

            var headers = (attributes.ToDictionary(a => $"X-Bz-Info-{a.Key}", a => a.Value));

            headers["X-Bz-File-Name"]    = B2UrlEncoder.Encode(fileName.Replace('\\', '/'));
            headers["Content-Type"]      = contentType;
            headers["Content-Length"]    = contentLength.ToString();
            headers["X-Bz-Content-Sha1"] = contentSha1;

            var request = new HttpRequestMessage(HttpMethod.Post, uploadUrl)
                          .WithAuthorization(authorizationToken)
                          .WithContent(content)
                          .WithContentHeaders(headers);

            var response = await httpClient.SendAsync(request).ConfigureAwait(false);

            var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

            ThrowIfFailure(response, responseStream);

            Trace(() => "UploadFile completed");
            return(UploadFileResponse.FromJson(responseStream));
        }
예제 #2
0
 public void UrlEncode()
 {
     foreach (var testCase in testData.testCases)
     {
         var encoded             = B2UrlEncoder.Encode(testCase.s);
         var acceptableEncodings = new[] { testCase.minimallyEncoded, testCase.fullyEncoded };
         Assert.Contains(encoded, acceptableEncodings);
     }
 }