Skip to content

Instantly share code, notes, and snippets.

@akaz00
Created April 29, 2023 04:14
Show Gist options
  • Select an option

  • Save akaz00/3c2b628d4a44c0479566af867857cf86 to your computer and use it in GitHub Desktop.

Select an option

Save akaz00/3c2b628d4a44c0479566af867857cf86 to your computer and use it in GitHub Desktop.
s3 upload, download with express (using "@aws-sdk/client-s3")
// s3 버킷 생성은 콘솔에서 진행
// (이름이 전역적으로 고유해야 함)
// IAM 사용자 만들고 엑세스 키 생성하여 연결 (AmazonS3FullAccess 권한 줘야 함)
// https://artiiicy.tistory.com/16
const { S3Client, AbortMultipartUploadCommand, PutObjectCommand, GetObjectCommand } = require("@aws-sdk/client-s3");
const fs = require('fs')
const path = require('path');
require('dotenv').config()
const bucketName = "yubs87-testbucket"
const express = require('express')
const app = express()
const multer = require('multer')
const { Stream, Readable, Duplex, PassThrough } = require('stream');
const { Upload } = require('@aws-sdk/lib-storage')
/*
const uploadPath = 'uploads/'
const storage = multer.diskStorage({
destination: uploadPath,
filename: function ( req, file, cb ) {
const arr = file.originalname.split('.')
const ext = arr[arr.length - 1]
const filtnameExceptExtension = arr.slice(0, arr.length - 1).join('.')
cb(null, Date.now() + "_" + filtnameExceptExtension + "." + ext)
}
});
// const upload = multer({ dest: 'uploads/' })
const upload = multer({ storage })
*/
const upload = multer({ storage: multer.memoryStorage() })
// API 키, Secret 보관법?
// https://www.thedonutwhole.com/where-to-store-api-keys-safely/
// https://medium.com/poka-techblog/the-best-way-to-store-secrets-in-your-app-is-not-to-store-secrets-in-your-app-308a6807d3ed
const client = new S3Client({
region: "us-east-1",
credentials: {
accessKeyId: 'access key id here',
secretAccessKey: 'secret access key here'
}
});
// 업로드
// https://stackoverflow.com/questions/69884898/how-to-upload-a-stream-to-s3-with-aws-sdk-v3
// https://docs.aws.amazon.com/ko_kr/AmazonS3/latest/userguide/example_s3_PutObject_section.html
/*
(async () => {
const filename = 'global.css'
const filePath = path.join(__dirname, 'static/' + filename);
const fileStream = fs.createReadStream(filePath);
// console.log(fileStream)
// fileStream.pipe(process.stdout);
const input = {
"Body": fileStream,
"Bucket": bucketName,
"Key": filename
};
const command = new PutObjectCommand(input);
const response = await client.send(command);
console.log(response)
})();
*/
/*
// 다운로드
// https://stackoverflow.com/questions/22143628/nodejs-how-do-i-download-a-file-to-disk-from-an-aws-s3-bucket
(async () => {
const response = await client.send(new GetObjectCommand({
Bucket: bucketName,
Key: "global.css"
}));
const localPath = path.join(__dirname, 'static/download');
const writeStream = fs.createWriteStream(localPath);
response.Body.pipe(writeStream);
// console.log(response);
})();
*/
// https://github.com/aws/aws-sdk-js-v3/issues/1920
app.post("/upload", upload.single('file'), async (req, res) => {
const file = req.file
const upload = new Upload({
client: client,
params: {
Bucket: bucketName,
Key: file.originalname,
Body: file.buffer
},
});
upload.done();
res.status(200).send({ message: "ok" })
})
app.get('/download', async (req, res) => {
const response = await client.send(new GetObjectCommand({
Bucket: bucketName,
Key: req.query.filename
}));
response.Body.pipe(res)
})
const port = 3000
app.listen(port, () => {
console.log(`app listening on port ${port}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment