//input : http://@@@@.com:5000/buckets
//output : List of Buckets
router.get('/buckets', (req, res) => {
s3.listBuckets(function(err, data) {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}
else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'buckets'
})
}
});
});
//input: http://@@@@.com:5000/listObjects/?bucketName=<버킷이름>
//output : 오브젝트 리스트
router.get('/listObjects',(req, res) => {
let bucketName = req.query.bucketName;
let Key = req.query.Key;
s3.listObjects(
Bucket: bucketName,
Key : Key
}, (err, data) => {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'getObjects'
})
}
});
});
//input: http://@@@@.com:5000/Objects/?bucketName=<버킷이름>
//output : 오브젝트 리스트
router.get('/Objects', (req, res) => {
let bucketName = req.query.bucketName;
let prefix = req.query.prefix;
s3.listObjectsV2({
Bucket: bucketName,
Prefix: prefix,
Delimiter:'/',
},function(err, data) {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}
else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'objects'
})
}
});
});
//Respoonse
{
"IsTruncated": false,
"Contents": [
{
"Key": "@@@/@@/@/@/@@@@@.png",
"LastModified": "2021-12-28T01:32:00.000Z",
"ETag": "\"f92ecdb7c97c###############\"",
"Size": 1843363,
"StorageClass": "STANDARD"
},
],
"Name": "",
"Prefix": "",
"Delimiter": "/",
"MaxKeys": 1000,
"CommonPrefixes": [
{
"Prefix": "@@@@@@/"
},
{
"Prefix": "@@@@@@@@@/"
}
],
"KeyCount": 200
}
router.get('/allobjects', (req, res) => {
let bucketName = req.query.bucketName;
let prefix = req.query.prefix;
let allObjects = {
Contents: [],
CommonPrefixes: [],
KeyCount :0
};
let opts = {
Bucket: bucketName,
Delimiter:'/',
Prefix:prefix,
}
function listAllObject(token, cb){
if(token) opts.ContinuationToken = token;
s3.listObjectsV2(opts, function(err, data){
console.log('Load process: ',bucketName);
allObjects.Contents = [...allObjects.Contents, ...data.Contents]
// allObjects.Contents.concat(data.Contents);
allObjects.CommonPrefixes = [...allObjects.CommonPrefixes, ...data.CommonPrefixes]
allObjects.KeyCount = allObjects.KeyCount+data.KeyCount;
if(data.IsTruncated)
listAllObject(data.NextContinuationToken, cb);
else
cb();
});
}
listAllObject(undefined,()=>{
res.writeHead(200,{'Content-Type':'application/json'});
console.log('allObjects: '+ allObjects )
res.end(JSON.stringify(allObjects));
console.log({
success: true,
data: 'objects'
})
console.log('end')
})
});
router.get('/allobjects', (req, res) => {
let bucketName = req.query.bucketName;
let prefix = req.query.prefix;
let allObjects = {
Contents: [],
CommonPrefixes: [],
KeyCount :0
};
let opts = {
Bucket: bucketName,
Delimiter:'/',
Prefix:prefix,
}
function listAllObject(token, cb){
if(token) opts.ContinuationToken = token;
s3.listObjectsV2(opts, function(err, data){
console.log('Load process: ',bucketName);
allObjects.Contents = [...allObjects.Contents, ...data.Contents]
// allObjects.Contents.concat(data.Contents);
allObjects.CommonPrefixes = [...allObjects.CommonPrefixes, ...data.CommonPrefixes]
allObjects.KeyCount = allObjects.KeyCount+data.KeyCount;
if(data.IsTruncated)
listAllObject(data.NextContinuationToken, cb);
else
cb();
});
}
listAllObject(undefined,()=>{
res.writeHead(200,{'Content-Type':'application/json'});
console.log('allObjects: '+ allObjects )
res.end(JSON.stringify(allObjects));
console.log({
success: true,
data: 'objects'
})
console.log('end')
})
});
router.get('/getObjects',(req, res) => {
let bucketName = req.query.bucketName;
let Key = req.query.Key;
s3.getObject({
Bucket: bucketName,
Key : Key
}, (err, data) => {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'getObjects'
})
}
});
});
router.get('/getImg', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
let param = {Bucket :bucketName, Key : key}
s3.getObject(param).on('success',function(response){
let result = response.data.Body
console.log("Key was", response.data.Body)
res.writeHead(200, {'Content-Type': ' image/*' });
res.write(result);
res.end('end');
}).send()
})
router.get('/getJson', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
let param = {Bucket :bucketName, Key : key}
s3.getObject(param).on('success',(response)=>{
let result = response.data.Body
console.log(typeof response.data.Body)
res.writeHead(200,{'Content-Type': 'text/json' });
res.write(result);
res.end();
}).send()
})
router.get('/getNpy', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
let param = {Bucket :bucketName, Key : key}
s3.getObject(param).on('success',(response)=>{
let result = response.data.Body
console.log(typeof response.data.Body)
res.writeHead(200,{'Content-Type': 'text/npy' });
res.write(result);
res.end();
}).send()
})
// query : bucketName = 'bucket name'
// query : key = 'object key'
router.get('/downloads', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
//let file = fs.createWriteStream(key);
let param = {Bucket :bucketName, Key : key}
//s3.getObject(param).createReadStream().pipe(file);
s3.getObject(param).on('success',function(response){
let result = response.data.Body
console.log("Key was", response.data.Body)
// res.write(result);
res.send(result);
}).send()
})
const router = express.Router();
const AWS = require("aws-sdk");
AWS.config.setPromisesDependency();
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: "ap-northeast-2",
});
const s3 = new AWS.S3();
//input : http://@@@@.com:5000/buckets
//output : List of Buckets
router.get('/buckets', (req, res) => {
s3.listBuckets(function(err, data) {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}
else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'buckets'
})
}
});
});
//input: http://@@@@.com:5000/listObjects/?bucketName=<버킷이름>
//output : 오브젝트 리스트
router.get('/listObjects',(req, res) => {
let bucketName = req.query.bucketName;
let Key = req.query.Key;
s3.listObjects(
Bucket: bucketName,
Key : Key
}, (err, data) => {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'getObjects'
})
}
});
});
//input: http://@@@@.com:5000/Objects/?bucketName=<버킷이름>
//output : 오브젝트 리스트
router.get('/Objects', (req, res) => {
let bucketName = req.query.bucketName;
let prefix = req.query.prefix;
s3.listObjectsV2({
Bucket: bucketName,
Prefix: prefix,
Delimiter:'/',
},function(err, data) {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}
else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'objects'
})
}
});
});
//Respoonse
{
"IsTruncated": false,
"Contents": [
{
"Key": "@@@/@@/@/@/@@@@@.png",
"LastModified": "2021-12-28T01:32:00.000Z",
"ETag": "\"f92ecdb7c97c###############\"",
"Size": 1843363,
"StorageClass": "STANDARD"
},
],
"Name": "",
"Prefix": "",
"Delimiter": "/",
"MaxKeys": 1000,
"CommonPrefixes": [
{
"Prefix": "@@@@@@/"
},
{
"Prefix": "@@@@@@@@@/"
}
],
"KeyCount": 200
}
router.get('/allobjects', (req, res) => {
let bucketName = req.query.bucketName;
let prefix = req.query.prefix;
let allObjects = {
Contents: [],
CommonPrefixes: [],
KeyCount :0
};
let opts = {
Bucket: bucketName,
Delimiter:'/',
Prefix:prefix,
}
function listAllObject(token, cb){
if(token) opts.ContinuationToken = token;
s3.listObjectsV2(opts, function(err, data){
console.log('Load process: ',bucketName);
allObjects.Contents = [...allObjects.Contents, ...data.Contents]
// allObjects.Contents.concat(data.Contents);
allObjects.CommonPrefixes = [...allObjects.CommonPrefixes, ...data.CommonPrefixes]
allObjects.KeyCount = allObjects.KeyCount+data.KeyCount;
if(data.IsTruncated)
listAllObject(data.NextContinuationToken, cb);
else
cb();
});
}
listAllObject(undefined,()=>{
res.writeHead(200,{'Content-Type':'application/json'});
console.log('allObjects: '+ allObjects )
res.end(JSON.stringify(allObjects));
console.log({
success: true,
data: 'objects'
})
console.log('end')
})
});
router.get('/allobjects', (req, res) => {
let bucketName = req.query.bucketName;
let prefix = req.query.prefix;
let allObjects = {
Contents: [],
CommonPrefixes: [],
KeyCount :0
};
let opts = {
Bucket: bucketName,
Delimiter:'/',
Prefix:prefix,
}
function listAllObject(token, cb){
if(token) opts.ContinuationToken = token;
s3.listObjectsV2(opts, function(err, data){
console.log('Load process: ',bucketName);
allObjects.Contents = [...allObjects.Contents, ...data.Contents]
// allObjects.Contents.concat(data.Contents);
allObjects.CommonPrefixes = [...allObjects.CommonPrefixes, ...data.CommonPrefixes]
allObjects.KeyCount = allObjects.KeyCount+data.KeyCount;
if(data.IsTruncated)
listAllObject(data.NextContinuationToken, cb);
else
cb();
});
}
listAllObject(undefined,()=>{
res.writeHead(200,{'Content-Type':'application/json'});
console.log('allObjects: '+ allObjects )
res.end(JSON.stringify(allObjects));
console.log({
success: true,
data: 'objects'
})
console.log('end')
})
});
router.get('/getObjects',(req, res) => {
let bucketName = req.query.bucketName;
let Key = req.query.Key;
s3.getObject({
Bucket: bucketName,
Key : Key
}, (err, data) => {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'getObjects'
})
}
});
});
router.get('/getImg', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
let param = {Bucket :bucketName, Key : key}
s3.getObject(param).on('success',function(response){
let result = response.data.Body
console.log("Key was", response.data.Body)
res.writeHead(200, {'Content-Type': ' image/*' });
res.write(result);
res.end('end');
}).send()
})
router.get('/getJson', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
let param = {Bucket :bucketName, Key : key}
s3.getObject(param).on('success',(response)=>{
let result = response.data.Body
console.log(typeof response.data.Body)
res.writeHead(200,{'Content-Type': 'text/json' });
res.write(result);
res.end();
}).send()
})
router.get('/getNpy', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
let param = {Bucket :bucketName, Key : key}
s3.getObject(param).on('success',(response)=>{
let result = response.data.Body
console.log(typeof response.data.Body)
res.writeHead(200,{'Content-Type': 'text/npy' });
res.write(result);
res.end();
}).send()
})
// query : bucketName = 'bucket name'
// query : key = 'object key'
router.get('/downloads', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
//let file = fs.createWriteStream(key);
let param = {Bucket :bucketName, Key : key}
//s3.getObject(param).createReadStream().pipe(file);
s3.getObject(param).on('success',function(response){
let result = response.data.Body
console.log("Key was", response.data.Body)
// res.write(result);
res.send(result);
}).send()
})
const router = express.Router();
const AWS = require("aws-sdk");
AWS.config.setPromisesDependency();
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: "ap-northeast-2",
});
const s3 = new AWS.S3();
//input : http://@@@@.com:5000/buckets
//output : List of Buckets
router.get('/buckets', (req, res) => {
s3.listBuckets(function(err, data) {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}
else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'buckets'
})
}
});
});
//input: http://@@@@.com:5000/listObjects/?bucketName=<버킷이름>
//output : 오브젝트 리스트
router.get('/listObjects',(req, res) => {
let bucketName = req.query.bucketName;
let Key = req.query.Key;
s3.listObjects(
Bucket: bucketName,
Key : Key
}, (err, data) => {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'getObjects'
})
}
});
});
//input: http://@@@@.com:5000/Objects/?bucketName=<버킷이름>
//output : 오브젝트 리스트
router.get('/Objects', (req, res) => {
let bucketName = req.query.bucketName;
let prefix = req.query.prefix;
s3.listObjectsV2({
Bucket: bucketName,
Prefix: prefix,
Delimiter:'/',
},function(err, data) {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}
else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'objects'
})
}
});
});
//Respoonse
{
"IsTruncated": false,
"Contents": [
{
"Key": "@@@/@@/@/@/@@@@@.png",
"LastModified": "2021-12-28T01:32:00.000Z",
"ETag": "\"f92ecdb7c97c###############\"",
"Size": 1843363,
"StorageClass": "STANDARD"
},
],
"Name": "",
"Prefix": "",
"Delimiter": "/",
"MaxKeys": 1000,
"CommonPrefixes": [
{
"Prefix": "@@@@@@/"
},
{
"Prefix": "@@@@@@@@@/"
}
],
"KeyCount": 200
}
router.get('/allobjects', (req, res) => {
let bucketName = req.query.bucketName;
let prefix = req.query.prefix;
let allObjects = {
Contents: [],
CommonPrefixes: [],
KeyCount :0
};
let opts = {
Bucket: bucketName,
Delimiter:'/',
Prefix:prefix,
}
function listAllObject(token, cb){
if(token) opts.ContinuationToken = token;
s3.listObjectsV2(opts, function(err, data){
console.log('Load process: ',bucketName);
allObjects.Contents = [...allObjects.Contents, ...data.Contents]
// allObjects.Contents.concat(data.Contents);
allObjects.CommonPrefixes = [...allObjects.CommonPrefixes, ...data.CommonPrefixes]
allObjects.KeyCount = allObjects.KeyCount+data.KeyCount;
if(data.IsTruncated)
listAllObject(data.NextContinuationToken, cb);
else
cb();
});
}
listAllObject(undefined,()=>{
res.writeHead(200,{'Content-Type':'application/json'});
console.log('allObjects: '+ allObjects )
res.end(JSON.stringify(allObjects));
console.log({
success: true,
data: 'objects'
})
console.log('end')
})
});
router.get('/allobjects', (req, res) => {
let bucketName = req.query.bucketName;
let prefix = req.query.prefix;
let allObjects = {
Contents: [],
CommonPrefixes: [],
KeyCount :0
};
let opts = {
Bucket: bucketName,
Delimiter:'/',
Prefix:prefix,
}
function listAllObject(token, cb){
if(token) opts.ContinuationToken = token;
s3.listObjectsV2(opts, function(err, data){
console.log('Load process: ',bucketName);
allObjects.Contents = [...allObjects.Contents, ...data.Contents]
// allObjects.Contents.concat(data.Contents);
allObjects.CommonPrefixes = [...allObjects.CommonPrefixes, ...data.CommonPrefixes]
allObjects.KeyCount = allObjects.KeyCount+data.KeyCount;
if(data.IsTruncated)
listAllObject(data.NextContinuationToken, cb);
else
cb();
});
}
listAllObject(undefined,()=>{
res.writeHead(200,{'Content-Type':'application/json'});
console.log('allObjects: '+ allObjects )
res.end(JSON.stringify(allObjects));
console.log({
success: true,
data: 'objects'
})
console.log('end')
})
});
router.get('/getObjects',(req, res) => {
let bucketName = req.query.bucketName;
let Key = req.query.Key;
s3.getObject({
Bucket: bucketName,
Key : Key
}, (err, data) => {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'getObjects'
})
}
});
});
router.get('/getImg', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
let param = {Bucket :bucketName, Key : key}
s3.getObject(param).on('success',function(response){
let result = response.data.Body
console.log("Key was", response.data.Body)
res.writeHead(200, {'Content-Type': ' image/*' });
res.write(result);
res.end('end');
}).send()
})
router.get('/getJson', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
let param = {Bucket :bucketName, Key : key}
s3.getObject(param).on('success',(response)=>{
let result = response.data.Body
console.log(typeof response.data.Body)
res.writeHead(200,{'Content-Type': 'text/json' });
res.write(result);
res.end();
}).send()
})
router.get('/getNpy', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
let param = {Bucket :bucketName, Key : key}
s3.getObject(param).on('success',(response)=>{
let result = response.data.Body
console.log(typeof response.data.Body)
res.writeHead(200,{'Content-Type': 'text/npy' });
res.write(result);
res.end();
}).send()
})
// query : bucketName = 'bucket name'
// query : key = 'object key'
router.get('/downloads', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
//let file = fs.createWriteStream(key);
let param = {Bucket :bucketName, Key : key}
//s3.getObject(param).createReadStream().pipe(file);
s3.getObject(param).on('success',function(response){
let result = response.data.Body
console.log("Key was", response.data.Body)
// res.write(result);
res.send(result);
}).send()
})
const router = express.Router();
const AWS = require("aws-sdk");
AWS.config.setPromisesDependency();
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: "ap-northeast-2",
});
const s3 = new AWS.S3();
//input : http://@@@@.com:5000/buckets
//output : List of Buckets
router.get('/buckets', (req, res) => {
s3.listBuckets(function(err, data) {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}
else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'buckets'
})
}
});
});
//input: http://@@@@.com:5000/listObjects/?bucketName=<버킷이름>
//output : 오브젝트 리스트
router.get('/listObjects',(req, res) => {
let bucketName = req.query.bucketName;
let Key = req.query.Key;
s3.listObjects(
Bucket: bucketName,
Key : Key
}, (err, data) => {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'getObjects'
})
}
});
});
//input: http://@@@@.com:5000/Objects/?bucketName=<버킷이름>
//output : 오브젝트 리스트
router.get('/Objects', (req, res) => {
let bucketName = req.query.bucketName;
let prefix = req.query.prefix;
s3.listObjectsV2({
Bucket: bucketName,
Prefix: prefix,
Delimiter:'/',
},function(err, data) {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}
else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'objects'
})
}
});
});
//Respoonse
{
"IsTruncated": false,
"Contents": [
{
"Key": "@@@/@@/@/@/@@@@@.png",
"LastModified": "2021-12-28T01:32:00.000Z",
"ETag": "\"f92ecdb7c97c###############\"",
"Size": 1843363,
"StorageClass": "STANDARD"
},
],
"Name": "",
"Prefix": "",
"Delimiter": "/",
"MaxKeys": 1000,
"CommonPrefixes": [
{
"Prefix": "@@@@@@/"
},
{
"Prefix": "@@@@@@@@@/"
}
],
"KeyCount": 200
}
router.get('/allobjects', (req, res) => {
let bucketName = req.query.bucketName;
let prefix = req.query.prefix;
let allObjects = {
Contents: [],
CommonPrefixes: [],
KeyCount :0
};
let opts = {
Bucket: bucketName,
Delimiter:'/',
Prefix:prefix,
}
function listAllObject(token, cb){
if(token) opts.ContinuationToken = token;
s3.listObjectsV2(opts, function(err, data){
console.log('Load process: ',bucketName);
allObjects.Contents = [...allObjects.Contents, ...data.Contents]
// allObjects.Contents.concat(data.Contents);
allObjects.CommonPrefixes = [...allObjects.CommonPrefixes, ...data.CommonPrefixes]
allObjects.KeyCount = allObjects.KeyCount+data.KeyCount;
if(data.IsTruncated)
listAllObject(data.NextContinuationToken, cb);
else
cb();
});
}
listAllObject(undefined,()=>{
res.writeHead(200,{'Content-Type':'application/json'});
console.log('allObjects: '+ allObjects )
res.end(JSON.stringify(allObjects));
console.log({
success: true,
data: 'objects'
})
console.log('end')
})
});
router.get('/allobjects', (req, res) => {
let bucketName = req.query.bucketName;
let prefix = req.query.prefix;
let allObjects = {
Contents: [],
CommonPrefixes: [],
KeyCount :0
};
let opts = {
Bucket: bucketName,
Delimiter:'/',
Prefix:prefix,
}
function listAllObject(token, cb){
if(token) opts.ContinuationToken = token;
s3.listObjectsV2(opts, function(err, data){
console.log('Load process: ',bucketName);
allObjects.Contents = [...allObjects.Contents, ...data.Contents]
// allObjects.Contents.concat(data.Contents);
allObjects.CommonPrefixes = [...allObjects.CommonPrefixes, ...data.CommonPrefixes]
allObjects.KeyCount = allObjects.KeyCount+data.KeyCount;
if(data.IsTruncated)
listAllObject(data.NextContinuationToken, cb);
else
cb();
});
}
listAllObject(undefined,()=>{
res.writeHead(200,{'Content-Type':'application/json'});
console.log('allObjects: '+ allObjects )
res.end(JSON.stringify(allObjects));
console.log({
success: true,
data: 'objects'
})
console.log('end')
})
});
router.get('/getObjects',(req, res) => {
let bucketName = req.query.bucketName;
let Key = req.query.Key;
s3.getObject({
Bucket: bucketName,
Key : Key
}, (err, data) => {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'getObjects'
})
}
});
});
router.get('/getImg', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
let param = {Bucket :bucketName, Key : key}
s3.getObject(param).on('success',function(response){
let result = response.data.Body
console.log("Key was", response.data.Body)
res.writeHead(200, {'Content-Type': ' image/*' });
res.write(result);
res.end('end');
}).send()
})
router.get('/getJson', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
let param = {Bucket :bucketName, Key : key}
s3.getObject(param).on('success',(response)=>{
let result = response.data.Body
console.log(typeof response.data.Body)
res.writeHead(200,{'Content-Type': 'text/json' });
res.write(result);
res.end();
}).send()
})
router.get('/getNpy', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
let param = {Bucket :bucketName, Key : key}
s3.getObject(param).on('success',(response)=>{
let result = response.data.Body
console.log(typeof response.data.Body)
res.writeHead(200,{'Content-Type': 'text/npy' });
res.write(result);
res.end();
}).send()
})
// query : bucketName = 'bucket name'
// query : key = 'object key'
router.get('/downloads', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
//let file = fs.createWriteStream(key);
let param = {Bucket :bucketName, Key : key}
//s3.getObject(param).createReadStream().pipe(file);
s3.getObject(param).on('success',function(response){
let result = response.data.Body
console.log("Key was", response.data.Body)
// res.write(result);
res.send(result);
}).send()
})
const router = express.Router();
const AWS = require("aws-sdk");
AWS.config.setPromisesDependency();
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: "ap-northeast-2",
});
const s3 = new AWS.S3();
//input : http://@@@@.com:5000/buckets
//output : List of Buckets
router.get('/buckets', (req, res) => {
s3.listBuckets(function(err, data) {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}
else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'buckets'
})
}
});
});
//input: http://@@@@.com:5000/listObjects/?bucketName=<버킷이름>
//output : 오브젝트 리스트
router.get('/listObjects',(req, res) => {
let bucketName = req.query.bucketName;
let Key = req.query.Key;
s3.listObjects(
Bucket: bucketName,
Key : Key
}, (err, data) => {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'getObjects'
})
}
});
});
//input: http://@@@@.com:5000/Objects/?bucketName=<버킷이름>
//output : 오브젝트 리스트
router.get('/Objects', (req, res) => {
let bucketName = req.query.bucketName;
let prefix = req.query.prefix;
s3.listObjectsV2({
Bucket: bucketName,
Prefix: prefix,
Delimiter:'/',
},function(err, data) {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}
else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'objects'
})
}
});
});
//Respoonse
{
"IsTruncated": false,
"Contents": [
{
"Key": "@@@/@@/@/@/@@@@@.png",
"LastModified": "2021-12-28T01:32:00.000Z",
"ETag": "\"f92ecdb7c97c###############\"",
"Size": 1843363,
"StorageClass": "STANDARD"
},
],
"Name": "",
"Prefix": "",
"Delimiter": "/",
"MaxKeys": 1000,
"CommonPrefixes": [
{
"Prefix": "@@@@@@/"
},
{
"Prefix": "@@@@@@@@@/"
}
],
"KeyCount": 200
}
router.get('/allobjects', (req, res) => {
let bucketName = req.query.bucketName;
let prefix = req.query.prefix;
let allObjects = {
Contents: [],
CommonPrefixes: [],
KeyCount :0
};
let opts = {
Bucket: bucketName,
Delimiter:'/',
Prefix:prefix,
}
function listAllObject(token, cb){
if(token) opts.ContinuationToken = token;
s3.listObjectsV2(opts, function(err, data){
console.log('Load process: ',bucketName);
allObjects.Contents = [...allObjects.Contents, ...data.Contents]
// allObjects.Contents.concat(data.Contents);
allObjects.CommonPrefixes = [...allObjects.CommonPrefixes, ...data.CommonPrefixes]
allObjects.KeyCount = allObjects.KeyCount+data.KeyCount;
if(data.IsTruncated)
listAllObject(data.NextContinuationToken, cb);
else
cb();
});
}
listAllObject(undefined,()=>{
res.writeHead(200,{'Content-Type':'application/json'});
console.log('allObjects: '+ allObjects )
res.end(JSON.stringify(allObjects));
console.log({
success: true,
data: 'objects'
})
console.log('end')
})
});
router.get('/allobjects', (req, res) => {
let bucketName = req.query.bucketName;
let prefix = req.query.prefix;
let allObjects = {
Contents: [],
CommonPrefixes: [],
KeyCount :0
};
let opts = {
Bucket: bucketName,
Delimiter:'/',
Prefix:prefix,
}
function listAllObject(token, cb){
if(token) opts.ContinuationToken = token;
s3.listObjectsV2(opts, function(err, data){
console.log('Load process: ',bucketName);
allObjects.Contents = [...allObjects.Contents, ...data.Contents]
// allObjects.Contents.concat(data.Contents);
allObjects.CommonPrefixes = [...allObjects.CommonPrefixes, ...data.CommonPrefixes]
allObjects.KeyCount = allObjects.KeyCount+data.KeyCount;
if(data.IsTruncated)
listAllObject(data.NextContinuationToken, cb);
else
cb();
});
}
listAllObject(undefined,()=>{
res.writeHead(200,{'Content-Type':'application/json'});
console.log('allObjects: '+ allObjects )
res.end(JSON.stringify(allObjects));
console.log({
success: true,
data: 'objects'
})
console.log('end')
})
});
router.get('/getObjects',(req, res) => {
let bucketName = req.query.bucketName;
let Key = req.query.Key;
s3.getObject({
Bucket: bucketName,
Key : Key
}, (err, data) => {
if (err) {
console.log(err, err.stack);
res.json({'error': err.stack});
}else {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(data));
console.log({
success: true,
data: 'getObjects'
})
}
});
});
router.get('/getImg', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
let param = {Bucket :bucketName, Key : key}
s3.getObject(param).on('success',function(response){
let result = response.data.Body
console.log("Key was", response.data.Body)
res.writeHead(200, {'Content-Type': ' image/*' });
res.write(result);
res.end('end');
}).send()
})
router.get('/getJson', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
let param = {Bucket :bucketName, Key : key}
s3.getObject(param).on('success',(response)=>{
let result = response.data.Body
console.log(typeof response.data.Body)
res.writeHead(200,{'Content-Type': 'text/json' });
res.write(result);
res.end();
}).send()
})
router.get('/getNpy', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
let param = {Bucket :bucketName, Key : key}
s3.getObject(param).on('success',(response)=>{
let result = response.data.Body
console.log(typeof response.data.Body)
res.writeHead(200,{'Content-Type': 'text/npy' });
res.write(result);
res.end();
}).send()
})
// query : bucketName = 'bucket name'
// query : key = 'object key'
router.get('/downloads', (req,res)=>{
let bucketName = req.query.bucketName;
let key = req.query.key;
//let file = fs.createWriteStream(key);
let param = {Bucket :bucketName, Key : key}
//s3.getObject(param).createReadStream().pipe(file);
s3.getObject(param).on('success',function(response){
let result = response.data.Body
console.log("Key was", response.data.Body)
// res.write(result);
res.send(result);
}).send()
})
const router = express.Router();
const AWS = require("aws-sdk");
AWS.config.setPromisesDependency();
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: "ap-northeast-2",
});
const s3 = new AWS.S3();