Android的HttpURLConnection类PUT到Amazon AWS S3 403错误 [英] Android HttpURLConnection PUT to Amazon AWS S3 403 error

查看:908
本文介绍了Android的HttpURLConnection类PUT到Amazon AWS S3 403错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上传文件到Amazon S3使用具有签名,过期和ACCESSKEY presigned网址,使用以下code我能使用普通的Java code,但同样的$ C $上传文件c。在安卓给了我403错误。 presigned网址是生成使用Amazon SDK

Uploading a file to Amazon S3 using presigned URL which has Signature, Expiration and AccessKey, using the following code I'm able to upload the file using normal java code but same code in Android gives me 403 error. Presigned URL is generate using Amazon SDK

我已经阅读 http://developer.android.com/reference/java /net/HttpURLConnection.html http://android-developers.blogspot.com/2011/ 09 /机器人-HTTP-clients.html ,但无法弄清楚头参数我应该用什么,我想在Android中它被设置头,其中服务器拒绝请求

I have read http://developer.android.com/reference/java/net/HttpURLConnection.html and http://android-developers.blogspot.com/2011/09/androids-http-clients.html but not able to figure out what header param should i use, I guess in android it is setting headers in request which server rejects

    HttpURLConnection connection=(HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("PUT"); 
    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
    out.write("This text uploaded as object.");
    out.close();
    int responseCode = connection.getResponseCode();

例外:403;签名不匹配:-o

Exception: 403; Signature don't match :-o

有没有人碰到这个问题? 以上信息为这头参数添加落后于Android的库中的场景?

Has anyone come across this issue? Or more details into which Header-parameters are added behind the scenes from android library?

推荐答案

所以经过一些试验/错误/搜索发现了问题:

So after some trial/error/search found out the issue:

在创建一个presigned网址是很重要的指定内容类型(基于数据),否则你会不断收到403签名不匹配,您在此处指定的contentType应在HttpURLConnection的连接被提及对象

While creating a presigned URL is it important to specify Content-Type (based on your data) or else you will keep getting 403 Signature don't match, the contentType that you specify here should be mentioned in HttpURLConnection connection object

    string s3url = s3Client.GetPreSignedURL(new GetPreSignedUrlRequest()
                   .WithBucketName(bucketName)
                   .WithKey(keyName)
                   .WithContentType("application/octet-stream") // IMPORTANT
                   .WithVerb(HttpVerb.PUT)
                   .WithExpires(<YOUR EXPIRATION TIME>);

在你的connection..add这对code问题后(PUT)

Inside your connection..add this to the code in question after ("PUT")

    connection.setFixedLengthStreamingMode(< YOUR DATA.LENGTH ?>);        
    connection.setRequestProperty("Content-Type","application/octet-stream");

勾选此错误,最新的SDK应该让你设置内容 - 类型

Check this bug, latest SDK should let you set Content-Type

这篇关于Android的HttpURLConnection类PUT到Amazon AWS S3 403错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆