我需要一个针对所有事务使用 SOAP 协议(protocol)的 Amazon S3 的 Win 客户端。据我所知,大多数解决方案都是基于 REST 而不是 SOAP。有任何想法吗?
编辑:
只是想澄清一下:请不要建议使用 REST。我完全清楚这两种协议(protocol)能做什么或不能做什么。因此,如果我要求这个特定的解决方案,这是有原因的。
我需要的是一个适用于 Win 平台的工作软件,该软件将 SOAP 用于 Amazon S3,而不是建议如何完成我的工作。谢谢你。
最佳答案
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);
}
}
}
}
如果您现在将访问 key ID 和 secret 访问 key 插入适当的位置并运行程序,您应该会获得 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/ 发布了一个 Visual Studio 解决方案.
关于soap - 基于 SOAP 的 Amazon S3 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1229659/