Skip to content

Commit

Permalink
feat: Add S3Service
Browse files Browse the repository at this point in the history
  • Loading branch information
npole0103 committed Sep 6, 2022
1 parent d725acc commit 2156dc7
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/main/java/kr/markdown/alreadyme/service/S3Service.java
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();
}
}

0 comments on commit 2156dc7

Please sign in to comment.