Http Response Entity on upload to S3 is not empty but is acting like it

729 views Asked by At

I'm trying to upload a file to my Amazon S3 bucket with authorization. I know that my signature works: using the same url, headers, and signature with hurl.it leads to a successful upload.

I also know that regular file upload with my program works: if I turn off S3 security and remove the headers, the upload works fine.

HttpClient client = new DefaultHttpClient();
url = "http://mybucket.s3.amazonaws.com/test/" + filename;

HttpPut put = new HttpPut(url);
put.setEntity(entity);
put.addHeader("Date", formattedDate);
put.addHeader("Authorization",
              "AWS " + c.getString(R.string.AWSAccessKeyId) +
              ":" + signature);
put.addHeader("Content-Type",contentType);


HttpResponse httpResponse = client.execute(put);
Log.e(TAG, allHeaders(httpResponse));

HttpEntity responseEntity = httpResponse.getEntity();
InputStream content = responseEntity.getContent();
InputStreamReader is = new InputStreamReader(content);
BufferedReader br = new BufferedReader(is,2000);

String read = br.readLine();
Log.e("AUTH", ">>>"+read+"<<<");
String response = "";
while (read != null){
    Log.e("AUTH", read);
    read = br.readLine();
    response += read + "\n";
}

Log.e("Code:", ""+httpResponse.getStatusLine().getStatusCode());

It's also worth noting that I'm doing this asynchronously on an Android emulator.

The resulting headers (displayed using Log.e(TAG, allHeaders(httpResponse))) are:

Date: Thu, 31 May 2012 05:58:08 GMT
Connection: close
Server: AmazonS3
Transfer-Encoding: chunked

The response status code is 400: Bad Request.

So what's weird is that I get these headers, but the content stream from responseEntity.getContent() is completely empty - I do not enter the while loop.

I also tried switching to an HttpGet targeted towards www.google.com and this worked completely fine.

Am I doing something wrong with the chunked encoding? Any obvious mistakes?

1

There are 1 answers

0
Koof On

I have no idea why this worked, but somehow my signature encoding was adding some hidden character that completely screwed with the whole http model. I just used the substring of the signature without that last character, and I was able to read the chunked stream.