-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploadImageS3.java
More file actions
46 lines (38 loc) · 1.53 KB
/
UploadImageS3.java
File metadata and controls
46 lines (38 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.hebe.imgUpload;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
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.PutObjectRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;
@Component
@RequiredArgsConstructor
public class UploadImageS3 {
private final AmazonS3Client amazonS3;
@Value("${cloud.aws.s3.bucket}")
private String bucket;
public String upload(File uploadFile, String filePath, String saveFileName) {
String fileName = filePath + "/" + saveFileName;
amazonS3.putObject(new PutObjectRequest(bucket, fileName, uploadFile)
.withCannedAcl(CannedAccessControlList.PublicRead));
return fileName;
}
public void delete(String filePath) {
try {
//Delete 객체 생성
DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucket, filePath);
//Delete
amazonS3.deleteObject(deleteObjectRequest);
System.out.println(String.format("[%s] deletion complete", filePath));
} catch (AmazonServiceException e) {
e.printStackTrace();
} catch (SdkClientException e) {
e.printStackTrace();
}
}
}