通过REST API上载Azure Blob时无法设置contentType

时间:2016-10-12 11:11:15

标签: c# asp.net azure azure-storage-blobs

我有以下代码,可以将图像成功上传到Azure Blob存储容器中。但是,默认情况下,它会将存储文件的ContentType设置为application/octet-stream我想将其更改为image/jpg

为此,我写下了以下评论中的行。他们从Azure documentation中了解了所需的内容类型标头,但现在请求会产生403 Unauthorized响应,而不是200

private static void PutBlob(string filenameToSave)
{
    var requestMethod = "PUT";
    var urlPath = _storageContainer + "/" + filenameToSave;
    var storageServiceVersion = "2015-12-11";
    var date = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
    var blobType = "BlockBlob";
    var imageBytes = GetImageBytes();
    var canonicalizedResource = "/" + _storageAccount + "/" + urlPath;

    // DOESN'T WORK:
    var canonicalizedHeaders = "x-ms-blob-type:" + blobType + "\nx-ms-blob-content-type:image/jpeg\nx-ms-date:" + date + "\nx-ms-version:" + storageServiceVersion;
    // WORKS:
    //var canonicalizedHeaders = "x-ms-blob-type:" + blobType + "\nx-ms-date:" + date + "\nx-ms-version:" + storageServiceVersion;

    // DOESN'T WORK:
    string stringToSign = requestMethod + "\nimage/jpeg\n\n" + imageBytes.Length + "\n\n\n\n\n\n\n\n\n" + canonicalizedHeaders + "\n" + canonicalizedResource;
    // WORKS:
    //string stringToSign = requestMethod + "\n\n\n" + imageBytes.Length + "\n\n\n\n\n\n\n\n\n" + canonicalizedHeaders + "\n" + canonicalizedResource;

    string authorizationHeader = GenerateSharedKey(stringToSign, _storageKey, _storageAccount);

    var uri = $"https://{_storageAccount}.blob.core.windows.net/{urlPath}";
    var request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = requestMethod;
    request.ContentType = "image/jpeg"; // DOESN'T WORK

    request.Headers.Add("x-ms-blob-content-type", "image/jpeg"); // DOESN'T WORK
    request.Headers.Add("x-ms-blob-type", blobType);
    request.Headers.Add("x-ms-date", date);
    request.Headers.Add("x-ms-version", storageServiceVersion);
    request.Headers.Add("Authorization", authorizationHeader);

    var stream = request.GetRequestStream();
    stream.Write(imageBytes, 0, imageBytes.Length);

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        // check the response status here, 200/201 means it worked

        Console.WriteLine("File uploaded");
    }
}

private static string GenerateSharedKey(string stringToSign, string key, string account)
{
    string signature;
    var unicodeKey = Convert.FromBase64String(key);
    using (var hmacSha256 = new HMACSHA256(unicodeKey))
    {
        var dataToHmac = Encoding.UTF8.GetBytes(stringToSign);
        signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
    }
    return string.Format(CultureInfo.InvariantCulture, "{0} {1}:{2}", "SharedKey", account, signature);
}

我可以看到问题出在生成的authorizationHeader上,但我不明白为什么我遵循指南。任何人都可以帮我解释一下。

另请注意,我必须通过REST API而不是Microsoft.WindowsAzure.Storage库来执行此操作(令人讨厌的是,据我所知,这是以这种方式完成的代码的一小部分)。

感谢。

1 个答案:

答案 0 :(得分:1)

我在您的代码中发现了一个问题:

// DOESN'T WORK:
    string stringToSign = requestMethod + "\nimage/jpeg\n\n" + imageBytes.Length + "\n\n\n\n\n\n\n\n\n" + canonicalizedHeaders + "\n" + canonicalizedResource;

基本上ContentType不应该是第二个参数。根据{{​​3}}

,第二个参数为Content-Encoding
StringToSign = VERB + "\n" +
               Content-Encoding + "\n" +
               Content-Language + "\n" +
               Content-Length + "\n" +
               Content-MD5 + "\n" +
               Content-Type + "\n" +
               Date + "\n" +
               If-Modified-Since + "\n" +
               If-Match + "\n" +
               If-None-Match + "\n" +
               If-Unmodified-Since + "\n" +
               Range + "\n" +
               CanonicalizedHeaders + 
               CanonicalizedResource;

因此,一旦您更改了此代码,您的代码就可以正常运行:

        string stringToSign = requestMethod + "\n" +
            "\n" + //Content Encoding
            "\n" + //Content Language
            imageBytes.Length + "\n" + //Content Length
            "\n" + //Content MD5
            "image/jpeg" + "\n" + //Content Type
            "\n" + //Date
            "\n" + //If - Modified - Since
            "\n" + //If - Match
            "\n" + //If - None - Match
            "\n" + //If - Unmodified - Since
            "\n" + //Range +
           canonicalizedHeaders + "\n" +
           canonicalizedResource;

此外,您不需要同时指定Content-Typex-ms-blob-content-type。如果您定义x-ms-blob-content-type,那么它应该包含在canonicalizedHeaders

以下是我用来测试此代码的代码:

    private static void PutBlob(string filenameToSave)
    {
        var requestMethod = "PUT";
        var urlPath = "<container-name>" + "/" + filenameToSave;
        var storageServiceVersion = "2015-12-11";
        var date = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
        var blobType = "BlockBlob";
        var imageBytes = File.ReadAllBytes(@"File Path");
        var canonicalizedResource = "/" + accountName + "/" + urlPath;

        // DOESN'T WORK:
        //var canonicalizedHeaders = "x-ms-blob-type:" + blobType + "\nx-ms-blob-content-type:image/jpeg\nx-ms-date:" + date + "\nx-ms-version:" + storageServiceVersion;
        // WORKS:
        var canonicalizedHeaders = "x-ms-blob-type:" + blobType + "\nx-ms-date:" + date + "\nx-ms-version:" + storageServiceVersion + "\n";

        // DOESN'T WORK:
        //string stringToSign = requestMethod + "\nimage/jpeg\n\n" + imageBytes.Length + "\n\n\n\n\n\n\n\n\n" + canonicalizedHeaders + "\n" + canonicalizedResource;
        // WORKS:
        //string stringToSign = requestMethod + "\n\n\n" + imageBytes.Length + "\n\n\n\n\n\n\n\n\n" + canonicalizedHeaders + "\n" + canonicalizedResource;
        string stringToSign = requestMethod + "\n" +
            "\n" + //Content Encoding
            "\n" + //Content Language
            imageBytes.Length + "\n" + //Content Length
            "\n" + //Content MD5
            "image/jpeg" + "\n" + //Content Type
            "\n" + //Date
            "\n" + //If - Modified - Since
            "\n" + //If - Match
            "\n" + //If - None - Match
            "\n" + //If - Unmodified - Since
            "\n" + //Range +
           canonicalizedHeaders +
           canonicalizedResource;
        string authorizationHeader = GenerateSharedKey(stringToSign, accountKey, accountName);

        var uri = "https://" + accountName + ".blob.core.windows.net/" + urlPath;

        var request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = requestMethod;
        request.ContentType = "image/jpeg"; // DOESN'T WORK

        //request.Headers.Add("x-ms-blob-content-type", "image/jpeg"); // DOESN'T WORK
        request.Headers.Add("x-ms-blob-type", blobType);
        request.Headers.Add("x-ms-date", date);
        request.Headers.Add("x-ms-version", storageServiceVersion);
        request.Headers.Add("Authorization", authorizationHeader);

        var stream = request.GetRequestStream();
        stream.Write(imageBytes, 0, imageBytes.Length);

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            // check the response status here, 200/201 means it worked

            Console.WriteLine("File uploaded");
        }
    }