简体   繁体   中英

how to upload image to Amazon S3

I am developing an Android app that uploads image (jpg) to AWS S3. The image is being uploaded but I am unable to open it using web browser: the image is either invalid or opened as a string.

//skipping error handling for simplicity

InputStream is = Utils.streamFromUri(this, uri);
byte[] buffer = new byte[FileUtils.getInputSize(is)];
...
//is is restored at this point
is.read(buffer);
String data = new String(buffer);
S3.createObjectForBucket(bucketName, objectName, data)

Can you please share a working code for upload or give some directions on how to resolve this?

Thanks

Two things.

In amazone S3. You need to set the privileges on the image to Everyone: read. I am using Firefox Plugin S3FOX, and then you right click, press Edit ACL and give everyone Read acces.

You might have to set a custome header. Content-Type: image/jpeg

This depends a bit on how you upload the file, normaly this is handeled by automagic.

The files you linked is ruined. Try a different tool to upload the images to S3 if you know the images is good on your computer.

http://www.s3fox.net/

First file is just 0 filled. Second file is not a jpeg but appears to be a result of some kind of transformation (there is some photo data in it). There should be no issue with upload under normal circumstances. If you provide specifics on how you uploaded them (code or tool) we can go from there.


Update

Your code to get data for upload is probably not going to work. I don't know specifics about your utilities but when you do

FileUtils.getInputSize(is)

it probably reads the entire stream, so you end up at the end of it and read nothing for upload. I highly recommend that you use Apache Commons IO to read data. And you can get size of it after it's read into memory.


Update 2

You are using byte to string conversion. From String(byte[]) documentation

Constructs a new String by decoding the specified array of bytes using 

the platform's default charset.

Since you use binary data, it's getting transformed. That actually also can be seen from your second file which start with ef bf bd which is Unicode replacement character. So refrain from using string for storing binary data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM