1

我无法通过 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)如何添加授权?

4

3 回答 3

1

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

于 2013-02-27T19:56:08.667 回答
0
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;
        }
    }
}

这段代码对我有用

于 2019-06-26T18:11:39.767 回答
0

看看这个例子。

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

它提供了使用 Azure Blob 存储 REST API 上传带有 SAS url 的分块文件的分步指南。

于 2019-10-14T05:54:00.783 回答