김현기

사이트에서 파일 받아와 S3에 저장 후 출력하기

1 var createError = require('http-errors'); 1 var createError = require('http-errors');
2 var express = require('express'); 2 var express = require('express');
3 +var formidable = require('formidable');
3 var path = require('path'); 4 var path = require('path');
4 var cookieParser = require('cookie-parser'); 5 var cookieParser = require('cookie-parser');
5 var logger = require('morgan'); 6 var logger = require('morgan');
...@@ -10,17 +11,29 @@ var AWS = require('aws-sdk'); ...@@ -10,17 +11,29 @@ var AWS = require('aws-sdk');
10 // 이미지를 저장할 버킷 이름 11 // 이미지를 저장할 버킷 이름
11 var bucketName = "kindofyourdogimage"; 12 var bucketName = "kindofyourdogimage";
12 // s3 버킷의 엔드 포인트 13 // s3 버킷의 엔드 포인트
13 -var bucketRegion = "ap-northeast-2"; 14 +var bucketRegion = 'ap-northeast-2';
15 +// access key
16 +var accessId= 'AKIAQVXKGU466IQYEGRN';
17 +// access secret key
18 +var secretKey = '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';
14 // AWS Cognito 인증 19 // AWS Cognito 인증
15 -var identityPoolId = "ap-northeast-2:7cba9a17-588b-40d6-8c70-eb8ba4d573be"; 20 +//var identityPoolId = "ap-northeast-2:7cba9a17-588b-40d6-8c70-eb8ba4d573be";
21 +
16 22
17 // AWS의 config 정보를 Update해준다 -> 이용자가 S3 버킷에 접근 가능하다 23 // AWS의 config 정보를 Update해준다 -> 이용자가 S3 버킷에 접근 가능하다
24 +/*
18 AWS.config.update({ 25 AWS.config.update({
19 region:bucketRegion, 26 region:bucketRegion,
20 credentials:new AWS.CognitoIdentityCredentials({ 27 credentials:new AWS.CognitoIdentityCredentials({
21 - IdentityPoolId:identityPoolId 28 + IdentityPoolId:identityPoolId,
22 }) 29 })
23 }) 30 })
31 +*/
32 +AWS.config.update({
33 + region:bucketRegion,
34 + accessKeyId:accessId,
35 + secretAccessKey:secretKey
36 +});
24 37
25 // 내가 사용할 S3 38 // 내가 사용할 S3
26 var s3 = new AWS.S3({ 39 var s3 = new AWS.S3({
...@@ -45,6 +58,7 @@ const uploadFile = (fileName) => { ...@@ -45,6 +58,7 @@ const uploadFile = (fileName) => {
45 }; 58 };
46 uploadFile(fileName); 59 uploadFile(fileName);
47 60
61 +/*
48 // rekognition 객체 62 // rekognition 객체
49 const client = new AWS.Rekognition(); 63 const client = new AWS.Rekognition();
50 64
...@@ -59,6 +73,8 @@ const params = { ...@@ -59,6 +73,8 @@ const params = {
59 "MinConfidence": 75 73 "MinConfidence": 75
60 } 74 }
61 75
76 +
77 +
62 // 이미지 분석하기 78 // 이미지 분석하기
63 client.detectLabels(params, function(err, response) { 79 client.detectLabels(params, function(err, response) {
64 if (err) { 80 if (err) {
...@@ -87,7 +103,7 @@ client.detectLabels(params, function(err, response) { ...@@ -87,7 +103,7 @@ client.detectLabels(params, function(err, response) {
87 }) // for response.labels 103 }) // for response.labels
88 } // if 104 } // if
89 }); 105 });
90 - 106 +*/
91 107
92 var indexRouter = require('./routes/index'); 108 var indexRouter = require('./routes/index');
93 var usersRouter = require('./routes/users'); 109 var usersRouter = require('./routes/users');
...@@ -108,6 +124,44 @@ app.use(express.static(path.join(__dirname, 'public'))); ...@@ -108,6 +124,44 @@ app.use(express.static(path.join(__dirname, 'public')));
108 app.use('/', indexRouter); 124 app.use('/', indexRouter);
109 app.use('/users', usersRouter); 125 app.use('/users', usersRouter);
110 126
127 +// image 올리는 화면
128 +app.get('/form', function(req, res){
129 + var output = `
130 + <html>
131 + <body>
132 + <form enctype="multipart/form-data" method="post" action="upload_receiver">
133 + <input type="file" name="userfile">
134 + <input type="submit">
135 + </form>
136 + </body>
137 + </html>
138 + `;
139 + res.send(output);
140 +});
141 +
142 +// image를 받았을 때
143 +app.post('/upload_receiver', function(req,res){
144 + var form = new formidable.IncomingForm();
145 + form.parse(req, function(err, fields, files){
146 + var s3 = new AWS.S3();
147 + var params = {
148 + Bucket:bucketName,
149 + ACL:'public-read',
150 + Key:files.userfile.namem,
151 + Body: require('fs').createReadStream(files.userfile.path)
152 + }
153 + s3.upload(params, function(err, data){
154 + var result='';
155 + if(err)
156 + result = 'Fail';
157 + else
158 + result = `<img src="${data.Location}>`;
159 + res.send(`<html><body>${result}</body></html`)
160 + });
161 + console.log(err, fields,files);
162 + });
163 +});
164 +
111 // catch 404 and forward to error handler 165 // catch 404 and forward to error handler
112 app.use(function(req, res, next) { 166 app.use(function(req, res, next) {
113 next(createError(404)); 167 next(createError(404));
......

29.7 KB