3

我正在尝试使用 WebClient 将新的 blob 简单地上传到 Azure 存储计数器,如下所示:

var sas = "[a new generated sas with Read, Write, List & Delete permissions]";
var sData = "This is a test!";
var sEndPoint = "http://myaccount.blob.core.windows.net/mycontainer/MyTest.txt" + sas;

var clt = new WebClient();
var res = await clt.UploadStringTaskAsync(sEndPoint, "PUT", sData);

这给了我一个“(400)错误请求”。错误。我在这里做错什么了吗?

谢谢

(顺便说一句,我需要使用 REST 而不是 Client API,因为我在 Silverlight 项目中)

4

2 回答 2

8

您需要x-ms-blob-type为 blob 类型定义请求标头 ( ) 并将其值设置为BlockBlob. 同样对于Put请求,您还需要定义Content-Length请求标头。我写了一篇关于共享访问签名的博客文章并使用它(使用 REST API 和存储客户端库)执行一些 blob 操作,您可以在此处阅读:http: //gauravmantri.com/2013/02/13/revisiting-windows-天蓝色共享访问签名/

这是上传 blob 的帖子中的代码。它使用 HttpWebRequest/HttpWebResponse 而不是 WebClient:

static void UploadBlobWithRestAPISasPermissionOnBlobContainer(string blobContainerSasUri)
{
    string blobName = "sample.txt";
    string sampleContent = "This is sample text.";
    int contentLength = Encoding.UTF8.GetByteCount(sampleContent);
    string queryString = (new Uri(blobContainerSasUri)).Query;
    string blobContainerUri = blobContainerSasUri.Substring(0, blobContainerSasUri.Length - queryString.Length);
    string requestUri = string.Format(CultureInfo.InvariantCulture, "{0}/{1}{2}", blobContainerUri, blobName, queryString);
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
    request.Method = "PUT";
    request.Headers.Add("x-ms-blob-type", "BlockBlob");
    request.ContentLength = contentLength;
    using (Stream requestStream = request.GetRequestStream())
    {
        requestStream.Write(Encoding.UTF8.GetBytes(sampleContent), 0, contentLength);
    }
    using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
    {

    }
}
于 2013-04-23T03:18:07.043 回答
1

在针对 blob 模拟器进行测试时,这是我需要让它工作的代码:

        var connection = ConfigurationManager.AppSettings["AzureStorageConnectionString"];
        var storageAccount = CloudStorageAccount.Parse(connection);

        var client = new WebClient();
        client.Headers.Add("x-ms-blob-type", "BlockBlob");
        client.Headers.Add("x-ms-version", "2012-02-12");
        client.UploadData(string.Format(@"{0}/$root/{1}{2}", storageAccount.BlobEndpoint, myFileName, sharedAccessSignature), "PUT", _content);
于 2013-05-17T00:32:53.650 回答