-
Notifications
You must be signed in to change notification settings - Fork 0
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
cc9490f
commit dbabf45
Showing
3 changed files
with
70 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
33 changes: 33 additions & 0 deletions
33
src/main/java/slvtwn/khu/toyouserver/agent/aws/AwsConfiguration.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,33 @@ | ||
package slvtwn.khu.toyouserver.agent.aws; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import lombok.Getter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Getter | ||
@Configuration | ||
public class AwsConfiguration { | ||
|
||
@Value("cloud.aws.credentials.access-key") | ||
private String accessKey; | ||
@Value("cloud.aws.credentials.secret-key") | ||
private String secretKey; | ||
@Value("cloud.aws.region.static") | ||
private String region; | ||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
@Bean | ||
public AmazonS3Client amazonS3Client() { | ||
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); | ||
return (AmazonS3Client) AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) | ||
.build(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/slvtwn/khu/toyouserver/agent/aws/S3Agent.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,35 @@ | ||
package slvtwn.khu.toyouserver.agent.aws; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import java.io.ByteArrayInputStream; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import slvtwn.khu.toyouserver.common.response.ResponseType; | ||
import slvtwn.khu.toyouserver.exception.ToyouException; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class S3Agent { | ||
|
||
private final AmazonS3Client amazonS3Client; | ||
private final AwsConfiguration configuration; | ||
|
||
public String uploadFile(byte[] fileData, String fileName, String contentType) { | ||
try { | ||
String fileUrl = "https://" + configuration.getBucket() + "/to-you/" + fileName; | ||
|
||
ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentType(contentType); | ||
metadata.setContentLength(fileData.length); | ||
|
||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileData); | ||
amazonS3Client.putObject(new PutObjectRequest(configuration.getBucket(), fileName, byteArrayInputStream, metadata)); | ||
|
||
return fileUrl; | ||
} catch (Exception e) { | ||
throw new ToyouException(ResponseType.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
} |