Azure Storage Blob Rest Api Headers

本文关键字:Api Headers Rest Blob Storage Azure | 更新日期: 2023-09-27 18:01:21

我无法通过REST API上传图像到我的azure存储。这是我的代码:

        using (var client = new HttpClient())
        {
            using (var fileStream = await _eventPhoto.OpenStreamForReadAsync())
            {
                var content = new StreamContent(fileStream);
                content.Headers.Add("Content-Type", _eventPhoto.ContentType);
                content.Headers.Add("x-ms-blob-type", "BlockBlob");
                var uploadResponse = await client.PutAsync(new Uri("https://myservice.blob.core.windows.net/photos/newblob"),content);
                int x = 2;
            }
        }

我得到关于缺少参数的错误。可能授权。

1)如何添加缺失的标题?

2)有人有工作样品吗?

3)如何添加Authorization?

Azure Storage Blob Rest Api Headers

您有理由不使用已经为您完成此操作的客户端库吗?如果你有一个理由,你真的想要使用HttpClient,那么你需要a.)实现每个规范的签名,或者b.)在你的URI中使用共享访问签名。

using System;
using System.Globalization;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
namespace AzureSharedKey
{
    class Program
    {
        private const string blobStorageAccount = "your_account";
        private const string blobStorageAccessKey = "your_access_key";
        private const string blobPath = "blob_path";
        private const string blobContainer = "your_container";
        static void Main(string[] args)
        {
            Console.WriteLine("Program Initiated..");
            CreateContainer();
            Console.ReadLine();
        }
        private async static void CreateContainer()
        {
            string requestMethod = "GET";
            string msVersion = "2016-05-31";
            string date = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
            string clientRequestId = Guid.NewGuid().ToString();
            string canHeaders = string.Format("x-ms-client-request-id:{0}'nx-ms-date:{1}'nx-ms-version:{2}", clientRequestId, date, msVersion);
            string canResource = string.Format("/{0}/{1}/{2}", blobStorageAccount, blobContainer, blobPath);
            string SignStr = string.Format("{0}'n'n'n'n'n'n'n'n'n'n'n'n{1}'n{2}", requestMethod, canHeaders, canResource);
            string auth = CreateAuthString(SignStr);
            string urlPath = string.Format("https://{0}.blob.core.windows.net/{1}/{2}", blobStorageAccount, blobContainer, blobPath);
            Uri uri = new Uri(urlPath);
            Console.WriteLine("urlPath  "+ urlPath);
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Add("x-ms-date", date);
            client.DefaultRequestHeaders.Add("x-ms-version", "2016-05-31");
            client.DefaultRequestHeaders.Add("x-ms-client-request-id", clientRequestId);
            client.DefaultRequestHeaders.Add("Authorization", auth);
            HttpResponseMessage response = client.GetAsync(uri).Result;
            if (response.IsSuccessStatusCode)
            {
                string actualFilename= Path.GetFileName(blobPath);
                string physicaPath = "D:/Others/AZ/";//change path to where you want to write the file
                HttpContent content = response.Content;
                string pathName = Path.GetFullPath(physicaPath + actualFilename);
                FileStream fileStream = null;
                try
                {
                    fileStream = new FileStream(pathName, FileMode.Create, FileAccess.Write, FileShare.None);
                    await content.CopyToAsync(fileStream).ContinueWith(
                       (copyTask) =>
                       {
                           fileStream.Close();
                       });
                    Console.WriteLine("FileName: "+actualFilename);
                    Console.WriteLine("File Saved Successfully..");
                }
                catch
                {
                    if (fileStream != null) fileStream.Close();
                    throw;
                }
            }
            else
            {
                throw new FileNotFoundException();
            }
        }
        private static string CreateAuthString(string SignStr)
        {
            string signature = string.Empty;
            byte[] unicodeKey = Convert.FromBase64String(blobStorageAccessKey);
            using (HMACSHA256 hmacSha256 = new HMACSHA256(unicodeKey))
            {
                byte[] dataToHmac = System.Text.Encoding.UTF8.GetBytes(SignStr);
                signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
            }
            string authorizationHeader = string.Format(
                  CultureInfo.InvariantCulture,
                  "{0} {1}:{2}",
                  "SharedKey",
                  blobStorageAccount,
                  signature);
            return authorizationHeader;
        }
    }
}

this code for me

看一下这个例子。

http://www.codeninjango.com/programming/azure/azure-blob-storage-rest-api/

它给出了一个循序渐进的指南,使用Azure Blob Storage REST API上传带有SAS url的块文件。