-
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.
Showing
18 changed files
with
316 additions
and
70 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
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
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
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,3 @@ | ||
#!/bin/bash | ||
awslocal s3 mb s3://record-bucket; | ||
awslocal s3 ls; |
33 changes: 0 additions & 33 deletions
33
src/main/java/com/dnd/gooding/common/filestore/api/FileCreate.java
This file was deleted.
Oops, something went wrong.
61 changes: 41 additions & 20 deletions
61
src/main/java/com/dnd/gooding/common/filestore/infra/S3ServiceStore.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 |
---|---|---|
@@ -1,39 +1,60 @@ | ||
package com.dnd.gooding.common.filestore.infra; | ||
|
||
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 java.io.File; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.io.FileUtils; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.stereotype.Service; | ||
import software.amazon.awssdk.core.ResponseInputStream; | ||
import software.amazon.awssdk.core.sync.RequestBody; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest; | ||
import software.amazon.awssdk.services.s3.model.GetObjectResponse; | ||
|
||
@Slf4j | ||
@Component | ||
@Service | ||
public class S3ServiceStore { | ||
|
||
private final AmazonS3Client amazonS3Client; | ||
private final S3Client s3Client; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
public S3ServiceStore(AmazonS3Client amazonS3Client) { | ||
this.amazonS3Client = amazonS3Client; | ||
public S3ServiceStore(S3Client s3Client) { | ||
this.s3Client = s3Client; | ||
} | ||
|
||
@Transactional | ||
public void delete(String key) { | ||
DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucket, key); | ||
amazonS3Client.deleteObject(deleteObjectRequest); | ||
public String putFile(String key, File file) { | ||
s3Client.putObject( | ||
(req) -> { | ||
req.bucket(bucket); | ||
req.key(key); | ||
}, | ||
RequestBody.fromFile(file)); | ||
|
||
return s3Client.utilities().getUrl(request -> request.bucket(bucket).key(key)).toString(); | ||
} | ||
|
||
public File getFile(String key) { | ||
File file = new File(key); | ||
ResponseInputStream<GetObjectResponse> s3Object = | ||
s3Client.getObject( | ||
(req) -> { | ||
req.bucket(bucket); | ||
req.key(key); | ||
}); | ||
|
||
try { | ||
FileUtils.writeByteArrayToFile(file, s3Object.readAllBytes()); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
return file; | ||
} | ||
|
||
@Transactional | ||
public String create(File file) { | ||
amazonS3Client.putObject( | ||
new PutObjectRequest(bucket, file.getName(), file) | ||
.withCannedAcl(CannedAccessControlList.PublicRead)); | ||
return amazonS3Client.getUrl(bucket, file.getName()).toString(); | ||
public void deleteFile(String key) { | ||
DeleteObjectRequest deleteObjectRequest = | ||
DeleteObjectRequest.builder().bucket(bucket).key(key).build(); | ||
s3Client.deleteObject(deleteObjectRequest); | ||
} | ||
} |
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
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
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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/dnd/gooding/record/command/domain/ImageId.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,40 @@ | ||
package com.dnd.gooding.record.command.domain; | ||
|
||
import java.io.Serializable; | ||
import java.util.Objects; | ||
import javax.persistence.Column; | ||
import javax.persistence.Embeddable; | ||
|
||
@Embeddable | ||
public class ImageId implements Serializable { | ||
|
||
@Column(name = "image_id") | ||
private String id; | ||
|
||
protected ImageId() {} | ||
|
||
public ImageId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
ImageId imageId = (ImageId) o; | ||
return Objects.equals(id, imageId.id); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id); | ||
} | ||
|
||
public static ImageId of(String id) { | ||
return new ImageId(id); | ||
} | ||
} |
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
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
53 changes: 53 additions & 0 deletions
53
src/main/java/com/dnd/gooding/springconfig/s3/S3Config.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,53 @@ | ||
package com.dnd.gooding.springconfig.s3; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; | ||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain; | ||
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; | ||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
|
||
@Configuration | ||
public class S3Config { | ||
|
||
@Value("${cloud.aws.endpoint}") | ||
private String awsEndpoint; | ||
|
||
@Value("${cloud.aws.credentials.access-key}") | ||
private String accessKey; | ||
|
||
@Value("${cloud.aws.credentials.secret-key}") | ||
private String secretAccessKey; | ||
|
||
/** | ||
* AWS SDK에서 AWS 서비스를 사용하기 위해 별도의 인증 과정을 거친다. | ||
* | ||
* @return | ||
*/ | ||
@Bean | ||
public AwsCredentialsProvider awsCredentialsProvider() { | ||
return AwsCredentialsProviderChain.builder() | ||
.reuseLastProviderEnabled(true) | ||
.credentialsProviders( | ||
List.of( | ||
DefaultCredentialsProvider.create(), | ||
StaticCredentialsProvider.create( | ||
AwsBasicCredentials.create(accessKey, secretAccessKey)))) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public S3Client s3Client() { | ||
return S3Client.builder() | ||
.credentialsProvider(awsCredentialsProvider()) | ||
.region(Region.AP_NORTHEAST_2) | ||
.endpointOverride(URI.create(awsEndpoint)) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.