Skip to content

Commit

Permalink
improve uploadFile function (#5)
Browse files Browse the repository at this point in the history
* rewrite uploadFile function

* remove temporary logs and comments

* update version to 0.4.1
  • Loading branch information
devfilatov committed Jul 14, 2021
1 parent 3490020 commit 04a4987
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umbrellio/ucdn",
"version": "0.4.0",
"version": "0.4.1",
"bin": "ucdn",
"main": "index.js",
"repository": "git@github.com:umbrellio/ucdn.git",
Expand Down
26 changes: 9 additions & 17 deletions upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,15 @@ const findFiles = directory => {

const getKey = (file, root) => path.relative(root, file)

const uploadFile = (s3, config, file, key) => new Promise((resolve, reject) => {
const { bucket } = config
const stream = fs.createReadStream(file)
const basename = path.basename(file)
const params = {
Bucket: bucket,
Body: stream,
Key: key,
ContentType: mime.contentType(basename),
}

stream.on("error", reject)
s3.send(new PutObjectCommand(params))
.then(data => resolve(data))
.catch(err => reject(err))
.finally(() => stream.close())
})
const uploadFile = (s3, config, file, key) => {
return fs.promises.readFile(file).then(data => {
const { bucket } = config
const basename = path.basename(file)
const contentType = mime.contentType(basename)
const params = { Bucket: bucket, Body: data, Key: key, ContentType: contentType }
return s3.send(new PutObjectCommand(params))
})
}

const upload = argv => {
const config = getConfig(argv)
Expand Down

0 comments on commit 04a4987

Please sign in to comment.