Last active
August 6, 2020 02:08
-
-
Save deskoh/9cf2aa17137fdf52e4c4a38c1ba4ef7a to your computer and use it in GitHub Desktop.
NodeJS S3 Upload / Download
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require('fs') | |
| const AWS = require('aws-sdk') | |
| const BUCKET_NAME = 'my-bucket' | |
| const ACCESS_KEY = 'AKIAXXXXXXXXXXXXXXXX' | |
| const SECRET_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' | |
| const FILE = 'file-to-download.txt' | |
| AWS.config.update({ | |
| region: 'ap-southeast-1', | |
| accessKeyId: ACCESS_KEY, | |
| secretAccessKey: SECRET_KEY, | |
| }); | |
| const s3 = new AWS.S3() | |
| const options = { | |
| Bucket: BUCKET_NAME, | |
| Key: FILE, | |
| } | |
| s3.getObject(options, function(err, data) { | |
| if (err) { | |
| console.error(err.message) | |
| return | |
| } | |
| fs.writeFileSync(FILE, data.Body); | |
| console.info(`Downloaded ${FILE}`) | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require('fs') | |
| const AWS = require('aws-sdk') | |
| const BUCKET_NAME = 'my-bucket' | |
| const ACCESS_KEY = 'AKIAXXXXXXXXXXXXXXXX' | |
| const SECRET_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' | |
| const FILE = 'T:\\test.txt' | |
| const OBJECT_KEY = 'uploaded.txt' | |
| AWS.config.update({ | |
| region: 'ap-southeast-1', | |
| accessKeyId: ACCESS_KEY, | |
| secretAccessKey: SECRET_KEY, | |
| }); | |
| const s3 = new AWS.S3() | |
| const options = { | |
| Bucket: BUCKET_NAME, | |
| Key: OBJECT_KEY, | |
| Body: fs.readFileSync(FILE), | |
| // ContentType: 'application/json', | |
| } | |
| s3.putObject(options, function(err, data) { | |
| if (err) { | |
| console.error(err.message) | |
| return | |
| } | |
| console.info(`Uploaded ${FILE} to s3://${BUCKET_NAME}/${OBJECT_KEY}`) | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const aws4 = require('aws4') | |
| const bodyParser = require('body-parser') | |
| const cors = require('cors') | |
| const express = require('express') | |
| const http = require('https') | |
| const app = express() | |
| const port = 3030 || process.env.PORT | |
| process.env.AWS_ACCESS_KEY_ID = '' | |
| process.env.AWS_SECRET_ACCESS_KEY = '' | |
| const S3_ENDPOINT = process.env.S3_ENDPOINT || 's3.ap-southeast-1.amazonaws.com' | |
| app.use(cors()) | |
| app.use(bodyParser.raw({ type: '*/*' })) | |
| app.get('*', (req, res) => { | |
| const opts = { | |
| host: S3_ENDPOINT, | |
| path: req.originalUrl | |
| } | |
| aws4.sign(opts) | |
| http.request(opts, proxyRes => { | |
| res.writeHead(proxyRes.statusCode, proxyRes.headers) | |
| proxyRes.pipe(res, { end: true }) | |
| }).end() | |
| }) | |
| app.put('*', (req, res) => { | |
| console.log('body', req.body) | |
| const emptyBody = Object.keys(req.body).length === 0 | |
| const opts = { | |
| method: 'PUT', | |
| host: S3_ENDPOINT, | |
| path: req.originalUrl, | |
| headers: { | |
| 'content-type': req.headers['content-type'], | |
| 'content-length': req.headers['content-length'], | |
| }, | |
| body: emptyBody ? undefined : req.body, | |
| } | |
| aws4.sign(opts) | |
| const proxyClient = http.request(opts, proxyRes => { | |
| res.writeHead(proxyRes.statusCode, proxyRes.headers) | |
| proxyRes.pipe(res, { end: true }) | |
| }) | |
| if (!emptyBody) proxyClient.write(req.body) | |
| proxyClient.end() | |
| }) | |
| app.delete('*', (req, res) => { | |
| const opts = { | |
| method: 'DELETE', | |
| host: S3_ENDPOINT, | |
| path: req.originalUrl | |
| } | |
| aws4.sign(opts) | |
| http.request(opts, proxyRes => { | |
| proxyRes.pipe(res, { end: true }) | |
| }).end() | |
| }) | |
| app.listen(port, () => { | |
| console.log(`S3 proxy listening at http://localhost:${port}`) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment