簡體   English   中英

無法使用http put將圖像上傳到Amazon s3

[英]Cannot upload image to amazon s3 using http put

我正在開發一個Android應用,用戶可以在其中創建帶有圖像的帖子。 圖像正在上傳到Amazon S3。 因此,每個用戶如何上傳圖像,我不使用Amazon AWS開發工具包,因為其方法需要密鑰。 我嘗試使用HTTP put請求上傳圖像。 我向后端發送了第一個請求,它向我發送了用於上傳圖像的簽名和其他參數。 不幸的是,亞馬遜的第二個回應包含錯誤。

這是第一個請求和響應:

POST /upload HTTP/1.1
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Content-Length: 53
Host: wrum-wrum.ru
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)

file_name=1439474973659_crp.jpg&file_type=image%2Fjpg


HTTP/1.1 200 OK
Server: nginx
Date: Thu, 13 Aug 2015 14:09:46 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Keep-Alive: timeout=60
Vary: X-HTTP-Method-Override
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Download-Options: noopen
set-cookie: connect.sid=s%3AY0SlTYzkavxu34-pcuMIEGDPuRSDlKuM.%2FTRySP3UVyIIatnqLSGE8%2Bx%2BP239TL9rjPQxjVRbzD8; Path=/; HttpOnly

11b
{"signed_request":"https://babadoo.s3.amazonaws.com/1439474973659_crp.jpg?AWSAccessKeyId=AKIAJMXM46BCWD5WDGTQ&Content-Type=image%2Fjpg&Expires=1439475046&Signature=ZsqF%2BaCCeZmTTPkiTWLG5Ckix1U%3D&x-amz-acl=public-read","url":"https://babadoo.s3.amazonaws.com/1439474973659_crp.jpg"}
0

這是第二個請求和響應:

PUT /1439474973659_crp.jpg?AWSAccessKeyId=AKIAJMXM46BCWD5WDGTQ&Content-Type=image%2Fjpg&Expires=1439475046&Signature=ZsqF%2BaCCeZmTTPkiTWLG5Ckix1U%3D&x-amz-acl=public-read HTTP/1.1
Content-Type: image/jpeg
User-Agent: Apache-HttpClient/UNAVALIABLE (java 1.4)
Content-Length: 1242599
Host: babadoo.s3.amazonaws.com
Connection: Keep-Alive
Expect: 100-continue

HTTP/1.1 403 Forbidden
x-amz-request-id: 36D4D3E461AF0850
x-amz-id-2:     +/LpGcZD6ZMdbZDq31Zt5ablP5CZIbBElaD1pm6of7MrndAwXe6ortO3uJNVgn8sGQ03o2waie0=
Content-Type: application/xml
Transfer-Encoding: chunked
Date: Thu, 13 Aug 2015 14:10:00 GMT
Connection: close
Server: AmazonS3

357
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><AWSAccessKeyId>AKIAJMXM46BCWD5WDGTQ</AWSAccessKeyId><StringToSign>PUT

image/jpeg
1439475046
x-amz-acl:public-read
/babadoo/1439474973659_crp.jpg</StringToSign><SignatureProvided>ZsqF+aCCeZmTTPkiTWLG5Ckix1U=</SignatureProvided><StringToSignBytes>50 55 54 0a 0a 69 6d 61 67 65 2f 6a 70 65 67 0a 31 34 33 39 34 37 35 30 34 36 0a 78 2d 61 6d 7a 2d 61 63 6c 3a 70 75 62 6c 69 63 2d 72 65 61 64 0a 2f 62 61 62 61 64 6f 6f 2f 31 34 33 39 34 37 34 39 37 33 36 35 39 5f 63 72 70 2e 6a 70 67</StringToSignBytes><RequestId>36D4D3E461AF0850</RequestId><HostId>+/LpGcZD6ZMdbZDq31Zt5ablP5CZIbBElaD1pm6of7MrndAwXe6ortO3uJNVgn8sGQ03o2waie0=</HostId></Error>
0

碼:

public String uploadPhoto(final File file) {


    Runnable runnable = new Runnable() {
        public void run() {


            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(uploadURL);

            httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            String result = null;

            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("file_name", file.getName()));
                nameValuePairs.add(new BasicNameValuePair("file_type", "image/jpg"));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity httpEntity = response.getEntity();
                result = EntityUtils.toString(httpEntity);
                int i = 1;

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
            if (result != null) {
                int putURLpos = result.indexOf(parseStringFragment);
                if (putURLpos != -1) {
                    putURL = result.substring(putURLpos + parseStringFragment.length(),
                            result.indexOf("\"", putURLpos + parseStringFragment.length()));

                    try {

                        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
                        trustStore.load(null, null);

                        SSLSocketFactory sf = (new MySSLSocketFactory(trustStore));
                        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

                        BasicHttpParams httpParams = new BasicHttpParams();
                        HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
                        HttpProtocolParams.setContentCharset(httpParams, HTTP.DEFAULT_CONTENT_CHARSET);
                        HttpProtocolParams.setUseExpectContinue(httpParams, true);

                        SchemeRegistry sr = new SchemeRegistry();
                        sr.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
                        sr.register(new Scheme("https", sf, 443));
                        ThreadSafeClientConnManager ccManager = new ThreadSafeClientConnManager(httpParams, sr);

                        HttpClient client = new DefaultHttpClient(ccManager, httpParams);

                        URI uri = new URI(putURL);
                        HttpPut put = new HttpPut(uri);
                        put.setHeader("Content-Type", "image/jpeg");
                        put.setHeader("User-Agent", "Apache-HttpClient/UNAVALIABLE (java 1.4)");

                        EntityBuilder eb = EntityBuilder.create();
                        eb.setFile(file);
                        put.setEntity(eb.build());
                        HttpResponse response = client.execute(put);


                        HttpEntity resEntity = response.getEntity();
                        String res = EntityUtils.toString(resEntity);

                        int i = 1;
                        i++;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    };
    Thread thread = new Thread(runnable);
    thread.start();
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return uploadedPhotoURL;
}

所以我找到了解決方案。 是我的錯,我發了

nameValuePairs.add(new BasicNameValuePair("file_type", "image/jpg"));

對於第一個請求,並且

Content-Type: image/jpeg

在第二個請求中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM