【问题标题】:Cannot upload to s3 with nodejs generated signed url无法使用 nodejs 生成的签名 url 上传到 s3
【发布时间】:2015-02-05 07:25:59
【问题描述】:

我有一个 nodejs 应用程序,它正在生成一个用于将文件上传到 AWS S3 的签名 url。我想使用这个签名的 url 将文件从 C# winforms 应用程序上传到 S3。

在我的 C# 代码中,当我使用从我的 nodejs 服务器获取的签名 URL 将文件上传到 S3 时,我收到此错误:

The connection was forcibly closed by the remote host

但如果我使用 C# 生成签名的 url,文件会成功上传。我尝试对从 nodejs 获得的 url 进行编码和解码,但没有成功。 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

生成签名url的nodejs代码:

// this signed url does not work
var params = { Bucket: bucket, Key: folder + "/" + filename, Expires: 3600 };
s3.getSignedUrl('getObject', params, callback);

生成签名url的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 生成的签名 url 工作?

【问题讨论】:

    标签: c# node.js file-upload amazon-s3


    【解决方案1】:

    我解决了这个问题。问题出在nodejs代码的这一行:

    s3.getSignedUrl('getObject', params, callback);
    

    应该是

    s3.getSignedUrl('putObject', params, callback);
    

    改为。

    【讨论】:

      猜你喜欢
      • 2016-12-14
      • 2019-01-30
      • 2020-06-06
      • 2021-09-29
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 2021-08-26
      相关资源
      最近更新 更多