4

我需要一个针对所有事务使用 SOAP 协议的 Amazon S3 Win 客户端。据我所知,大多数解决方案都是基于 REST 而不是 SOAP。有任何想法吗?

编辑:

只是想澄清一下:请不要建议使用 REST。我完全清楚这两种协议能做什么或不能做什么。因此,如果我要求这个特定的解决方案,这是有原因的。

我需要的是一个适用于 Win 平台的工作软件,该软件将 SOAP 用于 Amazon S3,而不是建议如何完成我的工作。谢谢你。

4

1 回答 1

3
  1. 启动 Visual Studio 2008,创建一个新的 C# Windows 控制台应用程序。

  2. 添加 S3 WSDL 作为服务引用。在解决方案资源管理器中,右键单击引用,选择添加服务引用。在地址框中输入 S3 WSDL 地址:http: //s3.amazonaws.com/doc/2006-03-01/AmazonS3.wsdl。单击“前往”。“AmazonS3”应显示在服务框中。输入命名空间。我进入了 Amazon.S3。单击确定。

  3. 将 Program.cs 修改为如下所示:


using System;
using System.Globalization;
using System.Text;
using System.Security.Cryptography;
using ConsoleApplication1.Amazon.S3;

namespace ConsoleApplication1 {
    class Program {
        private const string accessKeyId     = "YOURACCESSKEYIDHERE0";
        private const string secretAccessKey = "YOURSECRETACCESSKEYHEREANDYESITSTHATLONG";

        public static DateTime LocalNow() {
            DateTime now = DateTime.Now;
            return new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, now.Millisecond, DateTimeKind.Local);
        }

       public static string SignRequest(string secret, string operation, DateTime timestamp) {
            HMACSHA1 hmac         = new HMACSHA1(Encoding.UTF8.GetBytes(secret));
            string   isoTimeStamp = timestamp.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ", CultureInfo.InvariantCulture);
            string   signMe       = "AmazonS3" + operation + isoTimeStamp;
            string   signature    = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(signMe)));
            return signature;
        }

        static void Main(string[] args) {
            DateTime       now    = LocalNow();
            AmazonS3Client client = new AmazonS3Client();

            var result = client.ListAllMyBuckets(
                accessKeyId,
                now,
                SignRequest(secretAccessKey, "ListAllMyBuckets", now));

            foreach (var bucket in result.Buckets) {
                Console.WriteLine(bucket.Name);
            }
        }
    }
}

如果您现在将访问密钥 ID 和秘密访问密钥插入适当的位置并运行程序,您应该会获得 S3 存储桶的列表。

AmazonS3Client 类具有所有可用作实例方法的 SOAP 操作。

Amazon 网站在http://developer.amazonwebservices.com/connect/entry.jspa?externalID=129&categoryID=47上有一个较旧的 (VS2005 + WSE) C#/SOAP 示例。

编辑:在http://flyingpies.wordpress.com/2009/08/04/the-shortest-ever-s3-csoapwcf-client/发布了一个视觉工作室解决方案。

于 2009-08-05T06:55:03.917 回答