- 0 . . Template
- 1 . Introduction.
- 1 1. . About Spring
- 1 2. . About Spring Boot
- 1 3. . Dev. Environment
- 1 4. . Hello World
- 1 5. . Summary
- 2 . Basic Database.
- 2 1. . MySql & Heidi
- 2 2. . JDBC
- 2 3. . MyBatis
- 2 4. . JPA
- 2 5. . Summary
- 3 . Basic Spring Security.
- 3 1. . About Spring Security
- 3 2. . Signup/Signin/Signout
- 3 3. . Security Information
- 3 4. . Fragment
- 3 5. . Summary
- 4 . Basic Project.
- 4 1. . Question & Answer
- 4 2. . Board & Reply
- 4 3. . Cart & Product
- 4 4. . Buying & Delivery
- 4 5. . Summary
- 5 . Advanced Project.
- 5 1. . Admin
- 5 2. . Chat with Ajax
- 5 3. . Other Login - Korean
- 5 4. . Like & View Count
- 5 5. . Summary
- 6 . Expert Project.
- 6 1. . Map - Korean
- 6 2. . Mail - Korean
- 6 3. . SMS - Korean
- 6 4. . Credit Card - Korean
- 6 5. . Summary
- 7 . Frontend Project.
- 7 1. . HTML/CSS/JS
- 7 2. . Bootstrap
- 7 3. . Theme
- 7 4. . Etc
- 7 5. . Summary
- 8 . Mini Project.
- 8 1. . Storyboard
- 8 2. . Backend - Korean
- 8 3. . Frontend
- 8 4. . Portfolio
- 8 5. . Summary
- 9 . Deploy. Project.
- 9 1. . AWS
- 9 2. . Gabia
- 9 3. . Domain
- 9 4. . Etc
- 9 5. . Summary
- A 1. Bug Repost.
- Appendix 0. .
hw : Notice & Reply, Board & Reply + IMAGE
AWS Image Upload
Basic Configuration
build.gradle
// aws s3
implementation 'com.amazonaws:aws-java-sdk-s3:1.12.716'
application.properties
# aws
cloud.aws.s3.endpoint=https://hisinstitute.s3.ap-northeast-2.amazonaws.com
S3Config.java
package com.example.adminproject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
@Configuration
public class S3Config {
@Value("YOURS")
private String awsAccessKey;
@Value("YOURS")
private String awsSecretKey;
@Value("YOURS")
private String region;
@Bean
public AmazonS3 s3client() {
BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsAccessKey,awsSecretKey);
return AmazonS3ClientBuilder.standard()
.withRegion(Regions.fromName(region))
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.build();
}
}
S3Service
package com.example.adminproject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.PutObjectRequest;
@Service
public class S3Service {
@Autowired
private AmazonS3 amazonS3;
@Value("YOURS")
private String bucketName;
public void uploadFile(MultipartFile multipartFile, String filename) throws IOException {
File file = new File(multipartFile.getOriginalFilename());
try (FileOutputStream fos = new FileOutputStream(file)){
fos.write(multipartFile.getBytes());
}
amazonS3.putObject(new PutObjectRequest(bucketName, filename, file));
file.delete();
}
}
Create.html
<form class="mb-9" action="/product/create" method="post" enctype="multipart/form-data">
<input name="image" type="file" />
Controller.java
@PostMapping("/create")
public String create(Product product,
@RequestParam("image") MultipartFile file
) {
productService.create(product, file);
return "redirect:/product/readlist";
}
Service.java
@Override
public void create(Product product, MultipartFile file) {
UUID uuid = UUID.randomUUID();
String fileName = uuid + "_" + file.getOriginalFilename();
try {
s3Service.uploadFile(file, fileName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
product.setPimg(fileName);
product.setPdate(LocalDateTime.now());
productRepository.save(product);
}
View
@Value("${cloud.aws.s3.endpoint}")
private String path;
@Value("${cloud.aws.s3.endpoint}")
private String path;
@GetMapping("/readdetail/{pid}")
public String readdetail(@PathVariable ("pid") Integer pid, Model model) {
model.addAttribute("product", productService.readdetail(pid));
model.addAttribute("path", path);
return "product/readdetail";
}
readdetail.html
<img th:src="@{|${path}/${product.pimg}|}">
한국정보시스템개발원
|
Hankook Information System Institute
austiny@snu.ac.kr / austiny@gatech.edu