简体   繁体   English

如何将图像上传到Amazon S3

[英]how to upload image to Amazon S3

I am developing an Android app that uploads image (jpg) to AWS S3. 我正在开发一个将图像(jpg)上传到AWS S3的Android应用。 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. 在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. 我正在使用Firefox插件S3FOX,然后右键单击,按“编辑ACL”,然后为每个人提供“读取”权限。

You might have to set a custome header. 您可能必须设置一个custome标头。 Content-Type: image/jpeg 内容类型:图片/ jpeg

This depends a bit on how you upload the file, normaly this is handeled by automagic. 这取决于您如何上传文件,通常这是由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. 如果您知道计算机上的图像质量很好,请尝试使用其他工具将图像上传到S3。

http://www.s3fox.net/ http://www.s3fox.net/

First file is just 0 filled. 第一个文件仅填充0。 Second file is not a jpeg but appears to be a result of some kind of transformation (there is some photo data in it). 第二个文件不是jpeg,但似乎是某种转换的结果(其中包含一些照片数据)。 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. 我强烈建议您使用Apache Commons IO读取数据。 And you can get size of it after it's read into memory. 将其读入内存后,您可以得到它的大小。


Update 2 更新2

You are using byte to string conversion. 您正在使用字节到字符串的转换。 From String(byte[]) documentation 来自String(byte[])文档

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. 实际上,从第二个文件ef bf bd开头的第二个文件中也可以看到它,它是Unicode替换字符。 So refrain from using string for storing binary data. 因此,请勿使用字符串存储二进制数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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