如何使用amazon sdk为虚拟域生成预签名的Amazon S3url?
我有一个名为foo.example.com的s3存储桶,它正确地都是CNAMEd。
我正在切换到最新的AWS .net SDK。
我希望生成预先签名的url:
http://foo.example.com/myfile.txt?s3_params_here
注意那里的虚荣名称。
我有:
string bucketName = "foo.example.com"; AmazonS3Client s3Client = new AmazonS3Client("bar", "xxx", new AmazonS3Config { ServiceURL = bucketName, CommunicationProtocol = Protocol.HTTP }); string key = "myfile.txt"; GetPreSignedUrlRequest request = new GetPreSignedUrlRequest() .WithBucketName(bucketName) .WithKey(key) .WithExpires(DateTime.Now.AddMinutes(5)) .WithProtocol(Protocol.HTTP); string url = s3Client.GetPreSignedURL(request);
我得到的url是这样的:
http://foo.example.com.foo.example.com/myfile.txt?AWSAccessKeyId=bar&Expires=1331069777&Signature=234KoUUvfE1nCcs2vLj9RQUhqF8%3D
这显然是错的。
我已经尝试了ServiceURL,bucketname等不同的变种,但似乎没有任何效果。
我找不到任何好的文档 – 这样做的正确方法是什么?
谢谢。
更新[解决方法]
我同时解决了我的矛盾测试结果,这些测试结果分别源于非系统测试和URL操作。 以下解决方法为我提供了技巧(即经过测试和重现),只需从您的解决方案开始:
string bucketName = "foo.example.com"; // [...] GetPreSignedUrlRequest request = new GetPreSignedUrlRequest() .WithBucketName(bucketName) .WithKey(key) .WithExpires(DateTime.Now.AddMinutes(32)) .WithProtocol(Protocol.HTTP);
现在这会产生一个错误的URL,其中包含重复的域名,即http://foo.example.com.foo.example.com/myfile.txt?[...]
可以简单地删除副本,例如:
string url = s3Client.GetPreSignedURL(request); // KLUDGE: remove duplicate domain name. url = url.Replace(bucketName + "." + bucketName, bucketName);
通过解决有关下面概述的所需方法的遇到的限制,这为我提供了适当的工作预签名URL(即http://sofzh.miximages.com/c%23/abc.png此类资源的关键是x / y / z / abc.png>而不是/x/y/z/abc.png
如果确保了上述内容,那么从返回的URL对象获取查询参数>来自URL对象url.getQuery()将返回包含>签名信息的查询参数,只需将其与原始awsURL一起后缀即可使用>输出任何编码问题。
希望这可以帮助..
基本上你需要在返回的url对象上使用url.getQuery,而不是简单地将它粘贴到你的桶的末尾。
https://forums.aws.amazon.com/thread.jspa?threadID=70521