JavaDeveloper
JavaDeveloper

Reputation: 5660

How to stream image to Amazon S3

I am building an image server. There is a servlet which takes in an image and intention is to store image in Amazon S3.

Part filePart = request.getPart("file");
InputStream inputStream = filePart.getInputStream();

How to stream the contents to amazon s3, without downloading file to secondary storage ?

The Amazon S3 api is

   System.out.println("Uploading a new object to S3 from a file\n");
    File file = new File(uploadFileName);
    s3client.putObject(new PutObjectRequest(
                       bucketName, keyName, file));

But this requires some file path to the local storage. How can I transfer the image from the servlet directly to S3 without storing it on disk ?

Upvotes: 0

Views: 1372

Answers (1)

wihoho
wihoho

Reputation: 36

Yes, there is another api is put inputStream into S3 directly.

public PutObjectRequest(java.lang.String bucketName,
            java.lang.String key,
            java.io.InputStream input,
            ObjectMetadata metadata)

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/PutObjectRequest.html#PutObjectRequest(java.lang.String,%20java.lang.String,%20java.io.InputStream,%20com.amazonaws.services.s3.model.ObjectMetadata)

Upvotes: 1

Related Questions