简体   繁体   中英

Upload image to S3 from webapp without storing it in server

I have a web application with a Javascript Frontend and a Java Backend. I have a Use Case where the users can upload their profile pictures. The Use Case is simple: send the image from the user browser and store it in S3 with a maximum file size. Currently the Frontend sends the image data stream to the Backend, and this the stores the image in S3 using the AWS Java SDK.

The Backend is obliged to previously store this image in a file system in order to know the image file size (and avoid reading more bytes than a certain maximum), since it is required by S3 to send the PUT Object Request along with the file Content Length.

Is there any other way I can do this with AWS? Using another service? maybe Lambda? I don't like this method of having to previously store the file in a file system and then open again a stream to send it to S3.

Thank you very much in advance.

You might get the file size on the client side as mentioned here but consider browser support. You shouldn't share your keys with the client side code. I believe Query String Authentication should be used in this scenario.

Assuming your maximum file size is less than your available memory, why can't you just read it in a byte[] or something similar where you can send it to S3 without writing it to disk. You also should be able to get the size that way.

Depends on the S3 java client you are using. If it allows you to read from ByteArrayInputStream then you should be able to do whatever.

Looks like you can use InputStream . See javadoc .

new PutObjectRequest(String bucketName,
                String key,
                InputStream input,
                ObjectMetadata metadata)

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