gpt4 book ai didi

api - Blob 服务 REST API - 设置 x-ms-blob-content-type 时,Put Blob 返回 403 Forbidden

转载 作者:行者123 更新时间:2023-12-03 05:08:50 24 4
gpt4 key购买 nike

我已成功在容器中创建 Blob,但是当我尝试设置 x-ms-blob-content-type header 时,没有创建 Blob,并且收到 403 错误。这是我的代码:

    var authorizationHeader = CreateAuthorizationHeader(stringToSign);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(blobEndpoint + urlPath);
request.Method = requestMethod;
request.Headers["x-ms-blob-type"] = blobType;
request.Headers["x-ms-date"] = dateInRfc1123Format;
request.Headers["x-ms-version"] = storageServiceVersion;
request.Headers["Authorization"] = authorizationHeader;
request.ContentLength = blobLength;

try
{
using (Stream requestStream = await request.GetRequestStreamAsync())
{
requestStream.Write(blobContent, 0, blobLength);
}

using(HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
{
var eTag = response.Headers["ETag"];
}
}

但是每当我尝试添加这些行中的任何一行时,我都会收到 403:

request.Headers["x-ms-blob-content-type"] = "image/jpeg";
//or
request.ContentType = "image/jpeg";

大家有遇到过这种情况吗?感谢任何反馈,谢谢!

更新:添加下面的 stringToSign 代码。

var canonicalizedHeaders = String.Format(
"x-ms-blob-type:{0}\nx-ms-date:{1}\nx-ms-version:{2}",
blobType,
dateInRfc1123Format,
storageServiceVersion);
var canonicalizedResource = String.Format("/{0}/{1}", account, urlPath);
var stringToSign = String.Format(
"{0}\n\n\n{1}\n\n\n\n\n\n\n\n\n{2}\n{3}",
requestMethod,
blobLength,
canonicalizedHeaders,
canonicalizedResource);

最佳答案

您是否在 canonicalizedHeaders请求 header 中添加了 x-ms-blob-content-type ?将其添加到 canonicalizedHeaders 时,请确保这是第一个条目,因为需要对 header 进行排序。我拿了你的代码并添加了标题,它工作得很好。这是我的代码:

    private static async Task UploadBlob()
{
var blobType = "BlockBlob";
var dateInRfc1123Format = DateTime.UtcNow.ToString("R");
var storageServiceVersion = "2014-02-14";
var blobContentType = "image/png";
var canonicalizedHeaders = String.Format("x-ms-blob-content-type:{0}\nx-ms-blob-type:{1}\nx-ms-date:{2}\nx-ms-version:{3}", blobContentType, blobType, dateInRfc1123Format, storageServiceVersion);
var urlPath = "test-test/AlarmClock1.png";
var canonicalizedResource = String.Format("/{0}/{1}", accountName, urlPath);
var requestMethod = "PUT";
var fileContents = File.ReadAllBytes(@"D:\images\images\AlarmClock1.png");
var blobLength = fileContents.Length;
var stringToSign = String.Format("{0}\n\n\n{1}\n\n\n\n\n\n\n\n\n{2}\n{3}", requestMethod, blobLength, canonicalizedHeaders, canonicalizedResource);
var authorizationHeader = SignThis(stringToSign);
var blobEndpoint = "https://myaccountname.blob.core.windows.net/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(blobEndpoint + urlPath);
request.Method = requestMethod;
request.Headers["x-ms-blob-content-type"] = blobContentType;
request.Headers["x-ms-blob-type"] = blobType;
request.Headers["x-ms-date"] = dateInRfc1123Format;
request.Headers["x-ms-version"] = storageServiceVersion;
request.Headers["Authorization"] = authorizationHeader;
request.ContentLength = blobLength;
try
{
using (Stream requestStream = await request.GetRequestStreamAsync())
{
requestStream.Write(fileContents, 0, blobLength);
}

using(HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
{
var eTag = response.Headers["ETag"];
}
}
catch (Exception excep)
{

}
}

关于api - Blob 服务 REST API - 设置 x-ms-blob-content-type 时,Put Blob 返回 403 Forbidden,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29341220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com