Reputation: 323
Right now my code takes the MultipartFile and converts into a java.util.File and uploads to S3.
try {
final ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(file.getContentType());
byte[] contentBytes = null;
try {
final InputStream is = file.getInputStream();
contentBytes = IOUtils.toByteArray(is);
} catch (final IOException e) {
log.error("Failed while reading bytes from %s" + e.getMessage());
}
final Long contentLength = Long.valueOf(contentBytes.length);
objectMetadata.setContentLength(contentLength);
objectMetadata.setHeader("filename", fileNameWithExtn);
/*
* Reobtain the tmp uploaded file as input stream
*/
final InputStream inputStream = file.getInputStream();
final File convFile = new File(fileNameWithExtn);
file.transferTo(convFile);
FileUtils.copyInputStreamToFile(inputStream, convFile);
try {
final AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();
versionId = s3Client.putObject(new PutObjectRequest("bucketName", name, convFile))
.getVersionId();
} catch (final AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which"
+ " means your request made it "
+ "to Amazon S3, but was rejected with an error response" + " for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (final AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which means"
+ " the client encountered " + "an internal error while trying to "
+ "communicate with S3, " + "such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
} catch (final Exception e) {
log.error("You failed to upload " + name + " => " + e.getMessage());
}
The reason why I don't want to do this way is because, in my desktop the image I upload in browser stays temporarily in tomcat server and then it get uploaded to S3.
But when my webapp runs in Ec2, the image uploaded from browser doesn't seem to stay in tomcat server because of permission issues(or some other reason). Hence the entire image upload fails.
I tried the solution provided in this article Converting MultipartFile to java.io.File without copying to local machine and missing luck.
Could someone direct me with code to directly stream a multipart file to S3?
Upvotes: 1
Views: 3704
Reputation: 962
In the form tag of jsp make sure you write enctype="multipart/form-data". And in servlet declare the below code just after
@WebServlet(name = "ServletName", urlPatterns = {"/ServletName"})
@MultipartConfig(fileSizeThreshold=1024*1024*10, // 10 MB (You can specify according to your choice)
maxFileSize=1024*1024*50, // 50 MB (You can specify according to your choice)
maxRequestSize=1024*1024*100)
You can write the following code in servlet to get the file in form of inputStream and upload it into S3.
Part filePart= request.getPart("FileName"); // The name of <input type=file name="FileName"> in jsp form. Here, FileName
try {
InputStream inputStream=null;
if(filePart!=null){
inputStream=filePart.getInputStream(); //get file in form of inputstream
}
ObjectMetadata md = new ObjectMetadata();
md.setContentLength(filePart.getSize());
md.setContentType(filePart.getContentType());
try {
final AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();
versionId = s3Client.putObject(new PutObjectRequest("bucketName", name, inputStream,md))
.getVersionId();
} catch (final AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which"
+ " means your request made it "
+ "to Amazon S3, but was rejected with an error response" + " for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (final AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which means"
+ " the client encountered " + "an internal error while trying to "
+ "communicate with S3, " + "such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
} catch (final Exception e) {
log.error("You failed to upload " + name + " => " + e.getMessage());
}
Upvotes: 2