AWS签名的URL在除Android之外的任何地方都可以使用



我正在使用AWS Lambda为我的移动产品创建一个简单的上传服务。

在服务器上,我使用以下代码生成一个预签名的URL

var params = {
    Bucket: targetS3Bucket,
    Key: key,
    Body: '',
    ContentType: event.contentType,
    Expires: 60
};
s3.getSignedUrl('putObject', params, function (err, url){
    context.done(null, {
        'oneTimeUploadUrl': url,
        'resultUrl': urlPrefix + key
    });
});

其中,targetS3Bucket是S3上文件夹的路径,key是文件本身的名称,urlPrefix是S3(即:s3.amazonaws.com/some-folder/)上文件的HTTP位置的根

将此代码与内置的HTTP库(也就是说,不使用任何aws SDK)一起使用在PC和iOS上不会失败,但在Android上不会失败。

最新版本的Android客户端代码如下所示:

uri = new URL(oneTimeUploadUrl);
// Setup Connection
HttpsURLConnection http = (HttpsURLConnection) uri.openConnection();
http.setDoOutput(true);
http.setRequestMethod("PUT");
​
// Write Data
OutputStream os = http.getOutputStream();
os.write(_bytes);
os.flush();
os.close(); // request gets sent off to the server

此操作始终失败,代码为400。我尝试了一些方法,比如更改编码,使用非https版本的HttpsURLConnection和其他一些方法,但都没有用。

我宁愿避免引入AWS SDK,因为我只需要这一个函数就可以工作,而使用这个lambada端解决方案可以在除android之外的所有平台上实现这一点。

这是从AWS返回的XML。返回的消息令人困惑,因为客户端从不更改令牌,并且在其他设备上也会成功执行相同的过程。

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>InvalidToken</Code>
    <Message>The provided token is malformed or otherwise invalid.</Message>
    <Token-0>{Token-0}</Token-0>
    <RequestId>{RequestId}</RequestId>
    <HostId>{HostId}</HostId>
</Error>

问题是HttpURLConnection静默地将Content-Type: application/x-www-form-urlencoded添加到请求中。这很烦人,因为很难确定HttpURLConnection对象的请求中有哪些标头。

不管怎样。这是正确的代码

uri = new URL(oneTimeUploadUrl);
// Setup Connection
HttpsURLConnection http = (HttpsURLConnection) uri.openConnection();
http.setDoOutput(true);
http.setRequestMethod("PUT");
http.setRequestProperty("Content-Type"," "); // remove Content-Type header
// Write Data
OutputStream os = http.getOutputStream();
os.write(_bytes);
os.flush();
os.close(); // request gets sent off to the server

另请参阅:HttpURLConnection PUT to Google Cloud Storage给出错误403

相关内容

最新更新