Last active
August 2, 2018 17:15
-
-
Save chathurangat/9424697d11614e3092909e64a87436a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.amazonaws.services.s3.model.CannedAccessControlList; | |
import com.amazonaws.services.s3.model.DeleteObjectRequest; | |
import com.amazonaws.services.s3.model.PutObjectRequest; | |
import com.springbootdev.amazon.s3.example.aws.service.AmazonS3ClientService; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.scheduling.annotation.Async; | |
import org.springframework.stereotype.Component; | |
import com.amazonaws.AmazonServiceException; | |
import com.amazonaws.auth.AWSCredentialsProvider; | |
import com.amazonaws.regions.Region; | |
import com.amazonaws.services.s3.AmazonS3; | |
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | |
import org.springframework.web.multipart.MultipartFile; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
@Component | |
public class AmazonS3ClientServiceImpl implements AmazonS3ClientService | |
{ | |
private String awsS3AudioBucket; | |
private AmazonS3 amazonS3; | |
private static final Logger logger = LoggerFactory.getLogger(AmazonS3ClientServiceImpl.class); | |
@Autowired | |
public AmazonS3ClientServiceImpl(Region awsRegion, AWSCredentialsProvider awsCredentialsProvider, String awsS3AudioBucket) | |
{ | |
this.amazonS3 = AmazonS3ClientBuilder.standard() | |
.withCredentials(awsCredentialsProvider) | |
.withRegion(awsRegion.getName()).build(); | |
this.awsS3AudioBucket = awsS3AudioBucket; | |
} | |
@Async | |
public void uploadFileToS3Bucket(MultipartFile multipartFile, boolean enablePublicReadAccess) | |
{ | |
String fileName = multipartFile.getOriginalFilename(); | |
try { | |
//creating the file in the server (temporarily) | |
File file = new File(fileName); | |
FileOutputStream fos = new FileOutputStream(file); | |
fos.write(multipartFile.getBytes()); | |
fos.close(); | |
PutObjectRequest putObjectRequest = new PutObjectRequest(this.awsS3AudioBucket, fileName, file); | |
if (enablePublicReadAccess) { | |
putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); | |
} | |
this.amazonS3.putObject(putObjectRequest); | |
//removing the file created in the server | |
file.delete(); | |
} catch (IOException | AmazonServiceException ex) { | |
logger.error("error [" + ex.getMessage() + "] occurred while uploading [" + fileName + "] "); | |
} | |
} | |
@Async | |
public void deleteFileFromS3Bucket(String fileName) | |
{ | |
try { | |
amazonS3.deleteObject(new DeleteObjectRequest(awsS3AudioBucket, fileName)); | |
} catch (AmazonServiceException ex) { | |
logger.error("error [" + ex.getMessage() + "] occurred while removing [" + fileName + "] "); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment