I upload a file to S3 using Java - this is what I got so far:
AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials("XX","YY")); List<Bucket> buckets = s3.listBuckets(); s3.putObject(new PutObjectRequest(buckets.get(0).getName(), fileName, stream, new ObjectMetadata()));
The file is loading, but a WARNING occurs when I do not set the content length:
com.amazonaws.services.s3.AmazonS3Client putObject: No content length specified for stream > data. Stream contents will be buffered in memory and could result in out of memory errors.
This is the file I'm loading, and the stream variable is an InputStream , from which I can get an array of bytes as follows: IOUtils.toByteArray(stream) .
So when I try to set the length of the content and MD5 (taken from here ), follow these steps:
// get MD5 base64 hash MessageDigest messageDigest = MessageDigest.getInstance("MD5"); messageDigest.reset(); messageDigest.update(IOUtils.toByteArray(stream)); byte[] resultByte = messageDigest.digest(); String hashtext = new String(Hex.encodeHex(resultByte)); ObjectMetadata meta = new ObjectMetadata(); meta.setContentLength(IOUtils.toByteArray(stream).length); meta.setContentMD5(hashtext);
This results in the following error returned from S3:
The content you specified is MD5 invalid.
What am I doing wrong?
Any help appreciated!
PS I am in Google App Engine - I cannot write a file to disk or create a temporary file because AppEngine does not support FileOutputStream.
java google-app-engine inputstream amazon-s3 md5
JohnIdol Dec 02 '11 at 4:45 2011-12-02 04:45
source share