- 1. Service Ŭ·¡½º¿¡ readObject ¸Þ¼µå »ý¼º
- 2. Controller Ŭ·¡½º¿¡¼ ÇØ´ç ·ÎÁ÷ ½ÇÇà
- 3. CSVÆÄÀÏ Àб⠼º°ø!
1. Service Ŭ·¡½º¿¡ readObject ¸Þ¼µå »ý¼º
°úÁ¤
- S3ObjectÀÇ getObjectContent()¸¦ È£ÃâÇÏ¿© S3ObjectInputStreamÇüÅ·ΠCSVÆÄÀÏ µ¥ÀÌÅ͸¦ ºÒ·¯¿Â´Ù.
- ±× ÇØ´ç CSVÆÄÀÏÀ» BufferedReader·Î ÇÑ ÁÙ ¾¿ Àоî¿Í¼ µ¥ÀÌÅ͸¦ Àоú´Ù.
* CSVÆÄÀÏÀÌ JSON¿¡ ºñÇØ ¿ë·®ÀÌ ÀÛ¾Æ »ç¿ëÇßÁö¸¸ Àоî¿Ã ¶§ µ¥ÀÌÅÍ ÇüŸ¦ Àß Â¥Áö ¾ÊÀ¸¸é ¹Þ¾Æ¿À±â ±î´Ù·Î¿ï °Í °°´Ù.
@Service
@RequiredArgsConstructor
public class S3Service {
private final AmazonS3 amazonS3;
@Value("${aws.s3.bucket}")
private String bucket;
/**
* S3 bucket ÆÄÀÏ Àбâ
*/
public void readObject(String storedFileName) throws IOException{
S3Object o = amazonS3.getObject(new GetObjectRequest(bucket, storedFileName));
S3ObjectInputStream ois = null;
BufferedReader br = null;
// Read the CSV one line at a time and process it.
try {
ois = o.getObjectContent();
System.out.println ("ois = " + ois);
br = new BufferedReader (new InputStreamReader (ois, "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
// Store 1 record in an array separated by commas
String[] data = line.split(",", 0);
if(!data[0].equals("N")) {
int idx =Integer.parseInt(data[0]);
System.out.println(idx+"-----------------------------------");
for(int i=1; i<data.length; i++){
System.out.print(data[i]+" ");
}
System.out.println();
}else{
for (String s : data) {
System.out.print(s+" ");
}
System.out.println();
}
}
}finally {
if(ois != null){
ois.close();
}
if(br != null){
br.close();
}
}
}
}
Âü°í·Î ³» CSVÆÄÀÏ µ¥ÀÌÅÍ´Â ´ÙÀ½°ú °°´Ù.
2. Controller Ŭ·¡½º¿¡¼ ÇØ´ç ·ÎÁ÷ ½ÇÇà
@Controller
@RequiredArgsConstructor
public class S3Controller {
private final S3Service downloadS3;
@GetMapping("/csv_read")
public String read() throws IOException {
downloadS3.readObject("20210522.csv");
return "csv_read"; // csv_read.html ÀÖ¾î¾ß ¿¡·¯ ¹ß»ý ¾ÈÇÔ
}
}
3. CSVÆÄÀÏ Àб⠼º°ø!
ÇÁ·ÎÁ§Æ®¸¦ ½ÇÇàÇÏ°í localhost:8080/csv_read¸¦ Çϸé csvÆÄÀÏÀÌ ÀоîÁø´Ù.
±× ÀüÀÇ °úÁ¤Àº ¾Æ·¡ ¸µÅ© Âü°í