我正在尝试使用.Net SDK将文件上传到S3存储,并且当PUT到AWS提供的预签名URL时看到以下错误响应:
您尝试访问的存储区必须使用指定的端点
进行寻址这是.Net代码 - 我已经确认我的存储桶位于USWest2。
var amazonS3Client = new AmazonS3Client(accessKey, secretKey, RegionEndpoint.USWest2);
// Generate a pre-signed URL.
string folderName = "MyFolder";
string key = "TestKey";
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
BucketName = _bucketName,
Key = folderName + "/" + key,
Verb = HttpVerb.PUT,
Expires = DateTime.Now.AddMinutes(5)
};
var uri = Uri(_amazonS3Client.GetPreSignedURL(request));
// Upload a file using the pre-signed URL.
Stream stream = ... // This is set elsewhere, contains a data stream.
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
HttpWebRequest httpRequest = WebRequest.Create(uri) as HttpWebRequest;
httpRequest.Method = "PUT";
using (Stream dataStream = httpRequest.GetRequestStream())
{
byte[] buffer = new byte[8000];
int bytesRead = 0;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
dataStream.Write(buffer, 0, bytesRead);
}
}
HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse;
if(response.StatusCode != HttpStatusCode.OK)
throw new Exception("Error uploading to AWS: " + response.StatusDescription);
答案 0 :(得分:0)
这最终是因为调用返回的URL:
var uri = Uri(_amazonS3Client.GetPreSignedURL(request));
格式为:
https://s3-us-west-2.amazonaws.com/....
但AWS将此重定向到:
https://s3.amazonaws.com/....
通过使用后一个URL作为我的“预签名网址”的开头,文件现在可以正确上传。