-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
145 lines (122 loc) · 3.02 KB
/
upload.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const fs = require("fs")
const path = require("path")
const {
S3Client,
CreateMultipartUploadCommand,
UploadPartCommand,
AbortMultipartUploadCommand,
PutObjectCommand
} = require("@aws-sdk/client-s3");
const client = new S3Client({
region: process.env.AWS_REGION,
apiVersion: '2006-03-01'
});
/**
* Chunk Size = 5 MiB
*/
const chunkSize = Math.pow(1024, 2) * 5;
/**function createMultipartUpload(key) {
const params = {
Key: key,
Bucket: process.env.AWS_BUCKET_NAME
};
try {
const command = new CreateMultipartUploadCommand(params);
return new Promise((resolve) => resolve(client.send(command)))
} catch (error) {
console.error(error);
return null;
} finally {
}
}
function uploadPart(buffer, uploadId, partNumber, key) {
const params = {
Key: key,
Bucket: process.env.AWS_BUCKET_NAME,
Body: buffer,
ContentLength: buffer.byteLength,
PartNumber: partNumber,
UploadId: uploadId
};
try {
const command = new UploadPartCommand(params)
return new Promise((resolve) => resolve(client.send(command)))
} catch (error) {
console.error(error)
return null;
} finally {
}
}**/
function upload(buffer, partNumber, key) {
const params = {
Key: `${key}${partNumber}`,
Bucket: process.env.AWS_BUCKET_NAME,
Body: buffer,
ContentLength: buffer.byteLength.toString(),
Metadata: {
IsMultipartUpload: "true",
PartNumber: partNumber.toString()
}
};
try {
const command = new PutObjectCommand(params)
return new Promise((resolve) => resolve(client.send(command)))
} catch (error) {
console.error(error)
return null;
} finally {
}
}
async function uploadHelper(filePath) {
const file = fs.readFileSync(filePath);
const fileSize = file.byteLength;
const iterations = Math.ceil(fileSize / chunkSize);
const key = `upload/pdffile_${Math.random().toString(36).substring(2, 12)}/`
try {
//const res = await createMultipartUpload(key);
//const uploadId = await res.UploadId;
var arr = [];
for (let i = 1; i <= iterations; i++) {
//arr[i - 1] = uploadPart(file.slice((i - 1) * chunkSize, i * chunkSize), uploadId.toString(), i, key)
arr[i - 1] = upload(file.slice((i - 1) * chunkSize, i * chunkSize), i, key)
}
const parts = await Promise.allSettled(arr)
return parts;
} catch (error) {
console.error(error)
return null;
} finally {
}
}
/**async function abortAllUploads() {
// uploads: contains upload info fetched from aws s3api list-multipart-uploads
var x = uploads.Uploads
for (let i = 0; i < x.length; i++) {
var temp = x[i]
var params = {
Key: temp.Key,
Bucket: process.env.AWS_BUCKET_NAME,
UploadId: temp.UploadId
}
var command = new AbortMultipartUploadCommand(params)
var response = await client.send(command)
}
return null
}**/
module.exports.main = async (event, context) => {
let result = null
try {
result = await uploadHelper('./10mbfile.pdf')
console.log(result)
} catch (error) {
const response = {
statusCode: 500,
body: "An Error Occurred"
};
return response;
}
return {
statusCode: 200,
body: "OK"
};
}