-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (29 loc) · 966 Bytes
/
index.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
const AWS = require("aws-sdk");
const s3Client = new AWS.S3({
region: process.env.REGION,
accessKeyId: process.env.ACCESS_KEY,
secretAccessKey: process.env.SECRET_KEY,
});
exports.handler = async (event, context, callback) => {
const { queryStringParameters } = event;
const ext = queryStringParameters.ext;
const dir = queryStringParameters.dir;
const imageDir = getImageDir(dir);
let today = new Date();
let randomStr = Math.random().toString(36).substring(2, 8);
let imageKey = today.getTime() + randomStr;
const params = {
Bucket: process.env.IMAGE_BUCKET,
Key: `${imageDir}${imageKey}.${ext}`,
Expires: 60 * 60,
};
const presignedUrl = await s3Client.getSignedUrlPromise("putObject", params);
callback(null, {
imageKey: imageKey + "." + ext,
presignedUrl: presignedUrl,
});
};
function getImageDir(imageDir) {
if (imageDir === "user") return "user/";
else if (imageDir === "store") return "store/";
}