Skip to content

Commit 5113178

Browse files
committed
Added Nodejs sample
1 parent 5aa9b9c commit 5113178

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed

aws.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const AWS = require('aws-sdk')
2+
require('dotenv').config()
3+
4+
const credentials = {
5+
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
6+
secretAccessKey: process.env.AWS_SECRET_KEY,
7+
}
8+
9+
const useLocal = process.env.NODE_ENV !== 'production'
10+
11+
const bucketName = process.env.AWS_BUCKET_NAME
12+
13+
const s3client = new AWS.S3({
14+
s3BucketEndpoint: true,
15+
credentials,
16+
/**
17+
* When working locally, we'll use the Localstack endpoints. This is the one for S3.
18+
* A full list of endpoints for each service can be found in the Localstack docs.
19+
*/
20+
endpoint: useLocal ? 'http://localhost:4572' : undefined,
21+
})
22+
23+
24+
const uploadFile = async (data, name) =>
25+
new Promise((resolve) => {
26+
s3client.upload(
27+
{
28+
Bucket: bucketName,
29+
/*
30+
include the bucket name here. For some reason Localstack needs it.
31+
see: https://github.com/localstack/localstack/issues/1180
32+
*/
33+
Key: `${bucketName}/${name}`,
34+
Body: data,
35+
},
36+
(err, response) => {
37+
if (err) throw err
38+
resolve(response)
39+
},
40+
)
41+
})
42+
43+
module.exports = uploadFile

cute_cat.jpg

135 KB
Loading

package-lock.json

Lines changed: 107 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "sample-terraform-localstack",
3+
"version": "1.0.0",
4+
"description": "Sample project to use Terraform, Localstack (AWS Local) and Docker compose with Nodejs.",
5+
"main": "upload-demo.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/UlisesGascon/sample-terraform-localstack.git"
12+
},
13+
"author": "Ulises Gascón",
14+
"license": "AGPL-3.0",
15+
"bugs": {
16+
"url": "https://github.com/UlisesGascon/sample-terraform-localstack/issues"
17+
},
18+
"homepage": "https://github.com/UlisesGascon/sample-terraform-localstack#readme",
19+
"dependencies": {
20+
"aws-sdk": "^2.493.0",
21+
"dotenv": "^8.0.0"
22+
}
23+
}

upload-demo.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
const fs = require('fs')
3+
const path = require('path')
4+
const uploadFile = require('./aws')
5+
6+
const testUpload = () => {
7+
const filePath = path.resolve(__dirname, 'cute_cat.jpg')
8+
const fileStream = fs.createReadStream(filePath)
9+
const fileName = `test-image-${new Date().toISOString()}.jpg`
10+
uploadFile(fileStream, fileName).then(console.log).catch((err) => {
11+
console.log(`[ERROR] ${err}`)
12+
})
13+
}
14+
15+
testUpload()

0 commit comments

Comments
 (0)