https://stackoverflow.com/questions/70602566/why-does-array-have-nothing
const AWS = require('aws-sdk');
AWS.config.region = 'ap-northeast-2';
const s3 = new AWS.S3();
const path = require('path');
const multer = require('multer');
const upload = multer({
storage: multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './img/');
},
filename: function (req, file, cb) {
cb(null, new Date().valueOf() + path.extname(file.originalname));
}
}),
});
module.exports = {
spring: (req, res) => {
const arrTemp = new Array();
s3.listObjects({ Bucket: 'ongin' })
.on('success', function handlePage(response) {
const signedUrlExpireSeconds = 60 * 60
for (let key in response.data.Contents) {
const url = s3.getSignedUrl('getObject', {
Bucket: 'ongin',
Key: response.data.Contents[key].Key,
Expires: signedUrlExpireSeconds
})
arrTemp.push(url);
console.log('arrTemp : ', arrTemp);
}
if (response.hasNextPage()) {
response.nextPage().on('success', handlePage).send();
}
}).send();
res.status(201).send(JSON.stringify({ arrTemp: arrTemp }))
}
}
// Create
const AWS = require('aws-sdk');
AWS.config.region = 'ap-northeast-2';
const s3 = new AWS.S3();
const path = require('path');
const multer = require('multer');
const upload = multer({
storage: multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './img/');
},
filename: function (req, file, cb) {
cb(null, new Date().valueOf() + path.extname(file.originalname));
}
}),
});
module.exports = {
spring: (req, res) => {
const arrTemp = new Array();
s3.listObjects({ Bucket: 'ongin' })
.on('success', function handlePage(response) {
const signedUrlExpireSeconds = 60 * 60
for (let key in response.data.Contents) {
const url = s3.getSignedUrl('getObject', {
Bucket: 'ongin',
Key: response.data.Contents[key].Key,
Expires: signedUrlExpireSeconds
})
arrTemp.push(url);
console.log('arrTemp : ', arrTemp);
}
}).send((err, data)=>{
res.status(201).send(JSON.stringify({ arrTemp: arrTemp }));
})
// if(arrTemp.length !== 0){
// console.log('arrTemp.length !== 0 실행됌');
// res.status(201).send(JSON.stringify({ arrTemp: arrTemp }));
// }else{
// console.log('arrTemp.length === 0 실행됌');
// res.status(404).send('not ok')
// }
}
}
/*
s3.upload({
'Bucket': 'ongin',
'Key': 's3에 저장될 파일 이름을 key라고 함. 만약에 이미지 파일이라면 파일이름.png 임 저장할 파일이름',
'ACL': 'public-read', // 모든 사람들이 읽을 수 있기위해서 ACL는 권한이란 뜻임
'Body': fs.createReadStream('파일이름.png'),
'ContentType': 'image/png' // 파일이 어떤 자료형인지 알려주는 것
}, function(err, data){
console.log(err);
console.log(data);
})
*/