【问题标题】:Azure REST WebClient PUT BlobAzure REST WebClient PUT Blob
【发布时间】:2013-04-16 02:50:37
【问题描述】:

我正在尝试使用 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 项目中)

【问题讨论】:

    标签: rest azure webclient azure-storage azure-blob-storage


    【解决方案1】:

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

    这是上传 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())
        {
    
        }
    }
    

    【讨论】:

    • 谢谢!这就像一个魅力。但我想用 WebClient 来做,所以我可以等待这个功能。有没有办法做到这一点?我试图从 WebClient 设置 Content-Length,但我读到这是 WebClient 的受限参数。有什么办法可以解决吗?
    • 好的,我终于明白了。它在不指定 Content-Length 和 Content-Type 的情况下工作(使用 WebClient)。我只是在我的代码中缺少“x-ms-blob-type”=“BlockBlob” :) 谢谢
    • 这就是我要说的 :) 使用 WebClient 时不需要指定 Content-Length。
    【解决方案2】:

    当针对 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);
    

    【讨论】:

      猜你喜欢
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 1970-01-01
      • 2021-10-30
      • 2017-02-22
      • 1970-01-01
      相关资源
      最近更新 更多