forked from alpha2319/cloudjot-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3.js
More file actions
73 lines (53 loc) · 1.57 KB
/
s3.js
File metadata and controls
73 lines (53 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require('dotenv').config();
var AWS = require("aws-sdk");
var fs = require('fs')
// Enter copied or downloaded access ID and secret key here for accessing amazon S3
const ID = process.env.AWS_ACCESS_KEY
const SECRET = process.env.AWS_SECRET_KEY
const region = process.env.AWS_BUCKET_REGION
const bucket = process.env.AWS_BUCKET_NAME
AWS.config.update({
accessKeyId: ID,
secretAccessKey: SECRET,
region: region
})
var s3 = new AWS.S3();
//upload a file to s3
function uploadFile(file){
const uploadParams ={
Bucket:bucket,
Body:file.data,
Key:Date.now() +"-"+ file.name,
ContentType:file.mimetype
}
return s3.upload(uploadParams).promise()
}
exports.uploadFile = uploadFile
//download a file from s3
function getUploadFile(fileKey){
const downloadParams = {
Bucket :bucket,
Key: fileKey
}
return s3.getObject(downloadParams).promise()
}
exports.getUploadFile = getUploadFile
function getDownloadUrl(fileKey){
const downloadParams = {
Bucket :bucket,
Key: fileKey,
Expires:20000,
ResponseContentDisposition : `attachment; filename="${fileKey}"`
}
return s3.getSignedUrl("getObject",downloadParams)
}
exports.getDownloadUrl = getDownloadUrl;
//delete file from s3
function deleteUploadFile(filekey){
const deleteParams = {
Bucket : bucket,
Key: filekey
}
return s3.deleteObject(deleteParams).promise();
}
exports.deleteUploadFile = deleteUploadFile