2016-09-30 182 views
0

我試圖把文件上傳到使用.net SDK S3存儲,和我看到下面的錯誤響應PUTing由AWS提供的預先簽訂URL時:AWS S3 PreSignedUrl上傳出錯

你的水桶嘗試訪問必須使用指定的端點尋址

這裏是.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

這是最終造成的,因爲該URL通過調用返回:

var uri = Uri(_amazonS3Client.GetPreSignedURL(request)); 

當時的格式:

https://s3-us-west-2.amazonaws.com/.... 

但AWS重定向這:

https://s3.amazonaws.com/.... 

通過使用後者的URL我的「預先簽名的URL」的開始,這些文件現在可以正確上傳。