Skip to content

Commit fcf54d5

Browse files
committed
Adding block to upload remote url to a public s3 bucket.
1 parent 4ed4a18 commit fcf54d5

File tree

4 files changed

+236
-0
lines changed

4 files changed

+236
-0
lines changed

blocks/upload-url-to-s3/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { Block } = require('node-webpipe')
2+
const { S3RemoteUploader } = require('./s3-uploader')
3+
4+
new Block()
5+
.name('Upload file to S3')
6+
.description('Upload a remote file to a public S3 bucket.')
7+
.input('url', 'string', 'A file url to upload.')
8+
.output('url', 'string', 'Returns the url to the uploaded file.')
9+
.handle((inputs, cb) => {
10+
const fileUpload = new S3RemoteUploader(inputs.url)
11+
12+
fileUpload.dispatch().then(r => {
13+
cb(null, { url: r.Location })
14+
})
15+
})
16+
.listen()

blocks/upload-url-to-s3/package-lock.json

Lines changed: 153 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "upload-url-to-s3",
3+
"dependencies": {
4+
"aws-sdk": "^2.503.0",
5+
"axios": "^0.19.0",
6+
"stream": "0.0.2"
7+
}
8+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const stream = require('stream')
2+
const axios = require('axios')
3+
const AWS = require('aws-sdk')
4+
const url = require('url') // built-in utility
5+
6+
class S3RemoteUploader {
7+
constructor (remoteAddr) {
8+
this.remoteAddr = remoteAddr
9+
this.stream = stream
10+
this.axios = axios
11+
this.AWS = AWS
12+
this.AWS.config.update({
13+
accessKeyId: process.env.AWS_S3_ACCESS_KEY,
14+
secretAccessKey: process.env.AWS_S3_SECRET_KEY
15+
})
16+
// this.spacesEndpoint = new this.AWS.Endpoint(process.env.AWS_S3_ENDPOINT)
17+
this.s3 = new this.AWS.S3()
18+
this.file_name = url.parse(
19+
this.remoteAddr.substring(this.remoteAddr.lastIndexOf('/') + 1)
20+
).pathname
21+
this.obj_key = 'public/' + this.file_name
22+
this.content_type = 'application/octet-stream'
23+
this.uploadStream()
24+
}
25+
26+
uploadStream () {
27+
const pass = new this.stream.PassThrough()
28+
this.promise = this.s3
29+
.upload({
30+
Bucket: process.env.AWS_S3_BUCKET,
31+
Key: this.obj_key,
32+
Body: pass,
33+
ContentType: this.content_type
34+
})
35+
.promise()
36+
return pass
37+
}
38+
39+
initiateAxiosCall () {
40+
return axios({
41+
method: 'get',
42+
url: this.remoteAddr,
43+
responseType: 'stream'
44+
})
45+
}
46+
47+
async dispatch () {
48+
await this.initiateAxiosCall().then(response => {
49+
if (response.status === 200) {
50+
this.content_type = response.headers['content-type']
51+
response.data.pipe(this.uploadStream())
52+
}
53+
})
54+
return this.promise
55+
}
56+
}
57+
module.exports = {
58+
S3RemoteUploader: S3RemoteUploader
59+
}

0 commit comments

Comments
 (0)