Upload Video File in Amazon S3 Storage Using Spring Boot

Upload Video File in Amazon S3 Storage Using Spring Boot

package com.aws.momo.project.util;

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Date;

import javax.annotation.PostConstruct;
import javax.imageio.ImageIO;
import javax.security.auth.message.callback.PrivateKeyCallback.Request;

import org.apache.http.HttpRequest;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.util.Base64;
import com.aws.momo.project.controller.MomoUsersController;

@Service
public class AmazonClient {
	private static Logger logger = LoggerFactory.getLogger(AmazonClient.class);

	private AmazonS3 s3client;

	@Value("${amazonProperties.videoUrl}")
	private String videoUrl;
	@Value("${amazonProperties.audioUrl}")
	private String audioUrl;
	@Value("${amazonProperties.thumbUrl}")
	private String thumbUrl;
	@Value("${amazonProperties.localPath}")
	private String localPath;
	@Value("${amazonProperties.serverPath}")
	private String serverPath;
	@Value("${amazonProperties.secretKey}")
	private String secretKey;
	@Value("${amazonProperties.accessKey}")
	private String accessKey;
	@Value("${amazonProperties.bucketNameVideo}")
	private String bucketNameVideo;
	@Value("${amazonProperties.bucketNameSound}")
	private String bucketNameSound;
	@Value("${amazonProperties.bucketNameThumb}")
	private String bucketNameThumb;
	@Value("${amazonProperties.bucketName}")
	private String bucketName;

	@SuppressWarnings("deprecation")
	@PostConstruct
	private void initializeAmazon() {
		AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
		this.s3client = new AmazonS3Client(credentials);
	}

	private File convertMultiPartToFile(MultipartFile file) throws IOException {

		File convFile = new File(file.getOriginalFilename());
		FileOutputStream fos = new FileOutputStream(serverPath + convFile);
		fos.write(file.getBytes());
		fos.close();
		return convFile;
	}

	private String generateFileName(MultipartFile multiPart) throws IOException {
		return new Date().getTime() + "-" + multiPart.getOriginalFilename().replace(" ", "_");
	}

	public String uploadVideoTos3bucket(MultipartFile file) {

		logger.info("uploadVideoTos3bucket");
		String fileUrl = "";
		try {

			logger.info("file.getOriginalFilename() " + file.getOriginalFilename());
			logger.info("file.getContentType()" + file.getContentType());
			logger.info("file.getInputStream() " + file.getInputStream());
			logger.info("file.toString() " + file.toString());
			logger.info("file.getSize() " + file.getSize());
			logger.info("file.getBytes() " + file.getBytes());
			logger.info("file.hashCode() " + file.hashCode());
			logger.info("file.getClass() " + file.getClass());
			logger.info("file.isEmpty() " + file.isEmpty());

			InputStream is = file.getInputStream();
			String keyName = generateFileName(file);

			logger.info("keyName" + keyName);

			logger.info("Uploading a new object to S3 from a uploadVideoTos3bucket\n");
			ObjectMetadata meta = new ObjectMetadata();
			meta.setContentType("video/mp4");
			meta.setContentLength(file.getSize());
			s3client.putObject(new PutObjectRequest(bucketNameVideo, keyName, is, meta)
					.withCannedAcl(CannedAccessControlList.PublicRead));

			logger.info("************************************");

			fileUrl = videoUrl + keyName;

		} catch (AmazonServiceException ase) {
			logger.info("Caught an AmazonServiceException, which " + "means your request made it "
					+ "to Amazon S3, but was rejected with an error response" + " for some reason.");
			logger.info("Error Message:    " + ase.getMessage());
			logger.info("HTTP Status Code: " + ase.getStatusCode());
			logger.info("AWS Error Code:   " + ase.getErrorCode());
			logger.info("Error Type:       " + ase.getErrorType());
			logger.info("Request ID:       " + ase.getRequestId());

		} catch (AmazonClientException ace) {

			logger.info("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.");
			logger.info("Error Message: " + ace.getMessage());
		} catch (IOException e) {
			logger.info("Error Message: " + e.getMessage());
		}

		return fileUrl;
	}

	


}
Application.yml
amazonProperties:
  videoUrl: 
  audioUrl: 
  thumbUrl: 
  localPath: 
  serverPath: 
   accessKey: 
  secretKey: 
  bucketNameVideo: 
  bucketNameSound:  
  bucketNameThumb:
  bucketName: