【问题标题】:Using RestSharp (C#) to make requests to aws使用 RestSharp (C#) 向 aws 发出请求
【发布时间】:2013-12-09 16:56:54
【问题描述】:

我正在尝试将 AWS S3 的 REST 接口用于 Web 服务,该服务以 git 的类似方式存储和检索文件片段(通过哈希和基于它的目录系统)。我正在使用 RestSharp 客户端库进行这些调用,因为 AWS 开发工具包是不可能的(Web 服务实际上需要与类似 AWS 的商店(如 Hitachi HDS)一起使用),一般来说,更多的存储平台将是补充说,人们认为一种标准化的方法将是执行无线通信的最佳选择。

问题在于 RestSharp 可能会添加一些额外的有效负载,因为 S3 急需保存多个数据元素。

以下代码是核心存储逻辑,需要注意的是我使用Ninject来处理任何依赖。

public bool PutBytesInStore(string piecehash, byte[] data)
        {
            string method = "POST";
            string hash;
            using (var sha1 = new SHA1CryptoServiceProvider())
            {
                hash = Convert.ToBase64String(sha1.ComputeHash(data));
            }

            string contentType = "application/octet-stream";
            string date = new DateTime().ToString("{EEE, dd MMM yyyy HH:mm:ss}");
            string file = string.Format("pieces/{0}/{1}/{2}", piecehash.Substring(0, 2), piecehash.Substring(0, 6),
                piecehash);

            //Creating signature
            var sBuilder = new StringBuilder();
            sBuilder.Append(method).Append("\n");
            sBuilder.Append(contentType).Append("\n");
            sBuilder.Append(date).Append("\n");
            sBuilder.Append(hash).Append("\n");
            sBuilder.Append(file).Append("\n");
            var signature = Convert.ToBase64String(new HMACSHA1(Encoding.UTF8.GetBytes(_password)).ComputeHash(Encoding.UTF8.GetBytes(sBuilder.ToString())));

            _request.Method = Method.POST;
            _request.AddFile(piecehash, data, piecehash);
            _request.AddHeader("Date", date);
            _request.AddHeader("Content-MD5", hash);
            _request.AddHeader("Authorisation", string.Format("AWS {0}:{1}", _identifier, signature));

            var response = _client.Execute(_request);

            //Check responses for any errors
            var xmlResponse = XDocument.Parse(response.Content);
            switch (response.StatusCode)
            {
                case HttpStatusCode.Forbidden:
                    ErrorCodeHandler(xmlResponse);
                    break;
                case HttpStatusCode.BadRequest:
                    ErrorCodeHandler(xmlResponse);
                    break;
                case HttpStatusCode.Accepted:
                    return true;
                default:
                    return false;
            }
            return false;
        }

问题在于发送的响应,内容为;

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>InvalidArgument</Code>
    <Message>POST requires exactly one file upload per request.</Message>      
    <ArgumentValue>0</ArgumentValue>
    <ArgumentName>file</ArgumentName>
    <RequestId>SomeRequest</RequestId 
    <HostId>SomeID</HostId>
</Error>

AWS API 在这条消息上似乎是pretty sparse,我似乎无法弄清楚为什么 RestSharp 客户端会向有效负载添加两个以上的文件。

非常感谢任何帮助。

【问题讨论】:

    标签: c# rest amazon-s3 restsharp


    【解决方案1】:

    这是因为 webkitboundary。请在 postman 上试试你的东西 - webkitboundary 在上传时非常重要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多