Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I am uploading 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 being uploaded but a WARNING is raised when I am not setting 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 a file I am uploading and the stream variable is an InputStream, from which I can get the byte array like this: IOUtils.toByteArray(stream).

So when I try to set the content length and MD5 (taken from here) like this:

// 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);

It causes the following error to come back from S3:

The Content-MD5 you specified was invalid.

What am I doing wrong?

1 Answer

0 votes
by (44.4k points)

We have to do this:

// content is a passed in InputStream

byte[] resultByte = DigestUtils.md5(content);

String streamMD5 = new String(Base64.encodeBase64(resultByte));

metaData.setContentMD5(streamMD5);

You can learn more about AWS S3 on AWS Tutorial.

Related questions

0 votes
1 answer

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...