我有一个nodejs应用程序,它生成一个用于将文件上传到AWS S3的签名URL。我想使用这个签名的URL从C#winforms应用程序上传文件到S3。
在我的C#代码中,当我使用我从nodejs服务器获取的签名网址上传文件到S3时,我收到此错误:
The connection was forcibly closed by the remote host
但如果我使用C#生成签名网址,则文件会成功上传。我尝试编码和解码我从nodejs得到的网址没有运气。 C#和nodejs服务器都在我的localhost中工作,因此应该没有时间差异。
由c#(工作)和nodejs(不工作)生成的示例url:
// c# generated, works
https://mybucket.s3-eu-west-1.amazonaws.com/folder/547dac915711b69f18241920.flv?AWSAccessKeyId=AKIAILNOHZMPD62PBJGQ&Expires=1417970769&Signature=8ePjR1%2FrQb35YU2AR%2B6480vjiuM%3D
// nodejs generated, does not work
https://mybucket.s3-eu-west-1.amazonaws.com/folder/547dac915711b69f18241920.flv?AWSAccessKeyId=AKIAILNOHZMPD62PBJGQ&Expires=1417967527&Signature=rIbJ2zuknWwFG92Lh7VGAhqYE0I%3D
生成签名网址的nodejs代码:
// this signed url does not work
var params = { Bucket: bucket, Key: folder + "/" + filename, Expires: 3600 };
s3.getSignedUrl('getObject', params, callback);
生成签名网址的c#代码:
// this signed url works
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
BucketName = "mybucket",
Key = "folder/" + _id + ".flv",
Verb = HttpVerb.PUT,
Expires = DateTime.Now.AddMinutes(5)
};
string url = null;
url = s3Client.GetPreSignedURL(request);
上传文件的C#代码:
HttpWebRequest httpRequest = WebRequest.Create(url) as HttpWebRequest;
httpRequest.Method = "PUT";
using (Stream dataStream = httpRequest.GetRequestStream())
{
byte[] buffer = new byte[8000];
using (FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
dataStream.Write(buffer, 0, bytesRead);
}
}
}
HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse;
为什么我的nodejs生成的url不起作用,但我的C#生成的url有效?如何让我的nodejs生成签名网址?
答案 0 :(得分:1)
我解决了这个问题。问题出在nodejs代码的这一行:
s3.getSignedUrl('getObject', params, callback);
应该是
s3.getSignedUrl('putObject', params, callback);
代替。