Azure - C# - 使用 Rest API 上传图像时出错
Azure - C# - Error while uploading image using Rest API
我正在尝试将图像上传到 Azure 存储容器。
使用 UploadBlobWithRestAPI,我正在尝试调用其余 PUT API 并使用 AuthorizationHeader,我正在尝试为 API 调用创建授权。
我正在获取在容器中创建的图像。但是,由于某种原因,图像不是可读格式。从容器下载图像并尝试在资源管理器中打开时,出现“看来我们不支持这种文件格式”。有什么想法吗?
public void UploadBlobWithRestAPI(string MachineName,string ImageName)
{
string storageKey = "Storagekey";
string storageAccount = "Account";
string containerName = "test-container";
string blobName = "test2.jpg";
string method = "PUT";
Byte[] imageContentBytes = System.IO.File.ReadAllBytes(@"C:\Test2.jpg");
int imageContentLength = (Encoding.UTF8.GetString(imageContentBytes)).Length;
string requestUri = $"https://{storageAccount}.blob.core.windows.net/{containerName}/{blobName}";
System.Net.HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
string now = DateTime.UtcNow.ToString("R");
request.Method = method;
request.ContentType = "application/octet-stream";
request.ContentLength = imageContentLength;
request.Headers.Add("x-ms-version", "2015-12-11");
request.Headers.Add("x-ms-date", now);
request.Headers.Add("x-ms-blob-type", "BlockBlob");
request.Headers.Add("Authorization", AuthorizationHeader(method, now, request, storageAccount, storageKey, containerName, blobName));
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(imageContentBytes)), 0, imageContentLength);
}
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
{
MessageBox.Show(resp.StatusCode.ToString());
}
}
public string AuthorizationHeader(string method, string now, HttpWebRequest request, string storageAccount, string storageKey, string containerName, string blobName)
{
string headerResource = $"x-ms-blob-type:BlockBlob\nx-ms-date:{now}\nx-ms-version:2015-12-11";
string urlResource = $"/{storageAccount}/{containerName}/{blobName}";
string stringToSign = $"{method}\n\n\n{request.ContentLength}\n\n{request.ContentType}\n\n\n\n\n\n\n{headerResource}\n{urlResource}";
HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(storageKey));
string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
String AuthorizationHeader = String.Format("{0} {1}:{2}", "SharedKey", storageAccount, signature);
return AuthorizationHeader;
}
我认为问题来了,因为您将图像(二进制内容)作为字符串上传,这会破坏数据。
请尝试更改以下内容:
int imageContentLength = (Encoding.UTF8.GetString(imageContentBytes)).Length;
到
int imageContentLength = imageContentBytes.Length;
和
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(imageContentBytes)), 0, imageContentLength);
}
到
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(imageContentBytes, 0, imageContentLength);
}
另外,请为内容类型设置正确的值。考虑到您上传的文件是 JPEG 图片,请将内容类型设置为 image/jpeg
而不是 application/octet-stream
。这将确保图像在浏览器中加载时正确显示。
我正在尝试将图像上传到 Azure 存储容器。
使用 UploadBlobWithRestAPI,我正在尝试调用其余 PUT API 并使用 AuthorizationHeader,我正在尝试为 API 调用创建授权。
我正在获取在容器中创建的图像。但是,由于某种原因,图像不是可读格式。从容器下载图像并尝试在资源管理器中打开时,出现“看来我们不支持这种文件格式”。有什么想法吗?
public void UploadBlobWithRestAPI(string MachineName,string ImageName)
{
string storageKey = "Storagekey";
string storageAccount = "Account";
string containerName = "test-container";
string blobName = "test2.jpg";
string method = "PUT";
Byte[] imageContentBytes = System.IO.File.ReadAllBytes(@"C:\Test2.jpg");
int imageContentLength = (Encoding.UTF8.GetString(imageContentBytes)).Length;
string requestUri = $"https://{storageAccount}.blob.core.windows.net/{containerName}/{blobName}";
System.Net.HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
string now = DateTime.UtcNow.ToString("R");
request.Method = method;
request.ContentType = "application/octet-stream";
request.ContentLength = imageContentLength;
request.Headers.Add("x-ms-version", "2015-12-11");
request.Headers.Add("x-ms-date", now);
request.Headers.Add("x-ms-blob-type", "BlockBlob");
request.Headers.Add("Authorization", AuthorizationHeader(method, now, request, storageAccount, storageKey, containerName, blobName));
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(imageContentBytes)), 0, imageContentLength);
}
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
{
MessageBox.Show(resp.StatusCode.ToString());
}
}
public string AuthorizationHeader(string method, string now, HttpWebRequest request, string storageAccount, string storageKey, string containerName, string blobName)
{
string headerResource = $"x-ms-blob-type:BlockBlob\nx-ms-date:{now}\nx-ms-version:2015-12-11";
string urlResource = $"/{storageAccount}/{containerName}/{blobName}";
string stringToSign = $"{method}\n\n\n{request.ContentLength}\n\n{request.ContentType}\n\n\n\n\n\n\n{headerResource}\n{urlResource}";
HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(storageKey));
string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
String AuthorizationHeader = String.Format("{0} {1}:{2}", "SharedKey", storageAccount, signature);
return AuthorizationHeader;
}
我认为问题来了,因为您将图像(二进制内容)作为字符串上传,这会破坏数据。
请尝试更改以下内容:
int imageContentLength = (Encoding.UTF8.GetString(imageContentBytes)).Length;
到
int imageContentLength = imageContentBytes.Length;
和
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(imageContentBytes)), 0, imageContentLength);
}
到
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(imageContentBytes, 0, imageContentLength);
}
另外,请为内容类型设置正确的值。考虑到您上传的文件是 JPEG 图片,请将内容类型设置为 image/jpeg
而不是 application/octet-stream
。这将确保图像在浏览器中加载时正确显示。