Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/s3 #9

Merged
merged 3 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/example/fiurinee/S3Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.fiurinee;

import com.amazonaws.services.s3.AmazonS3;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.lang.String;

import java.net.URL;

@RestController
public class S3Controller {

private final AmazonS3 s3Client;
private final String bucketName;

public S3Controller(AmazonS3 s3Client, @Value("${AWS_BUCKET}") String bucketName) {
this.s3Client = s3Client;
this.bucketName = bucketName;
}

@GetMapping("/image")
public ResponseEntity<URL> getImage(){
URL img = s3Client.getUrl(bucketName, "fiurinnn");

return ResponseEntity.ok(img);
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/example/fiurinee/StorageConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.fiurinee;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class StorageConfig {

@Value("${AWS_ACCESSKEY}")
private String accessKey;

@Value("${AWS_SECRETKEY}")
private String accessSecret;
@Value("${cloud.aws.region.static}")
private String region;

@Bean
public AmazonS3 s3Client() {
AWSCredentials credentials = new BasicAWSCredentials(accessKey, accessSecret);
return AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(region).build();
}

}

14 changes: 13 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ spring:

logging.level:
org.hibernate.SQL: debug
org.hibernate.type: trace
org.hibernate.type: trace

cloud:
aws:
credentials:
accessKey: ${AWS_ACCESSKEY}
secretKey: ${AWS_SECRETKEY}
s3:
bucket: ${AWS_BUCKET}
region:
static: ap-northeast-2
stack:
auto: 'false'
Loading