-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
adf9b1a
commit 5ab045e
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package zerobibim.flory.utils; | ||
|
||
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 lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class S3ImageComponent { | ||
private final AmazonS3Client amazonS3Client; | ||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
public String uploadImage(String category, MultipartFile multipartFile) { | ||
// 파일명 | ||
String fileName = createFileName(category, Objects.requireNonNull(multipartFile.getOriginalFilename())); | ||
|
||
ObjectMetadata objectMetadata = new ObjectMetadata(); | ||
objectMetadata.setContentType(multipartFile.getContentType()); | ||
|
||
// S3에 업로드 | ||
try { | ||
amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, multipartFile.getInputStream(), objectMetadata) | ||
.withCannedAcl(CannedAccessControlList.PublicRead)); | ||
} catch (IOException ignored) { | ||
} | ||
|
||
return amazonS3Client.getUrl(bucket, fileName).toString(); | ||
} | ||
|
||
/** | ||
* 파일명 생성 | ||
* @param category | ||
* @param originalFileName | ||
* @return 작명된 파일 이름 | ||
*/ | ||
public String createFileName(String category, String originalFileName) { | ||
int fileExtensionIndex = originalFileName.lastIndexOf("."); | ||
String fileExtension = originalFileName.substring(fileExtensionIndex); | ||
String fileName = originalFileName.substring(0, fileExtensionIndex); | ||
String random = String.valueOf(UUID.randomUUID()); | ||
|
||
return category + "/" + fileName + "_" + random + fileExtension; | ||
} | ||
|
||
/** | ||
* 이미지 삭제 | ||
* @param fileUrl | ||
*/ | ||
public void deleteImage(String fileUrl) { | ||
String[] deleteUrl = fileUrl.split("/", 4); | ||
amazonS3Client.deleteObject(new DeleteObjectRequest(bucket, deleteUrl[3])); | ||
} | ||
} |