-
Notifications
You must be signed in to change notification settings - Fork 6
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
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/java/kr/markdown/alreadyme/service/S3Service.java
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,32 @@ | ||
package kr.markdown.alreadyme.service; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.File; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class S3Service { | ||
private final AmazonS3Client amazonS3Client; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
public String upload(File file, String dirName) { | ||
String filePath = dirName + "/" + file.getName(); | ||
return putS3(file, filePath); | ||
} | ||
|
||
private String putS3(File file, String filePath) { | ||
amazonS3Client.putObject( | ||
new PutObjectRequest(bucket, filePath, file) | ||
.withCannedAcl(CannedAccessControlList.PublicRead) | ||
); | ||
return amazonS3Client.getUrl(bucket, filePath).toString(); | ||
} | ||
} |