forked from aws-samples/amazon-s3-presigned-urls-aws-sam
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.ts
More file actions
105 lines (89 loc) · 3.35 KB
/
Copy pathapp.ts
File metadata and controls
105 lines (89 loc) · 3.35 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
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
/*
Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
'use strict'
console.log('Loading function')
import AWS from 'aws-sdk'
import { CID } from 'multiformats'
import { base64pad } from 'multiformats/bases/base64'
console.log('config aws')
// @ts-ignore
AWS.config.update({ region: process.env.AWS_REGION })
const s3 = new AWS.S3()
// Change this value to adjust the signed URL's expiration
const URL_EXPIRATION_SECONDS = 300
// Main Lambda entry point
export const handler = async event => {
return await getUploadURL(event)
}
const getUploadURL = async function (event) {
const { searchParams } = new URL(`http://localhost/?${event.rawQueryString}`)
const type = searchParams.get('type')
const name = searchParams.get('name')
if (!type || !name) {
throw new Error('Missing name or type query parameter: ' + event.rawQueryString)
}
let s3Params
if (type === 'data' || type === 'file') {
s3Params = carUploadParams(searchParams, event, type)
} else if (type === 'meta') {
s3Params = metaUploadParams(searchParams, event)
} else {
throw new Error('Unsupported upload type: ' + type)
}
console.log('Params: ', s3Params)
const uploadURL = await s3.getSignedUrlPromise('putObject', s3Params)
return JSON.stringify({
uploadURL: uploadURL,
Key: s3Params.Key
})
}
function metaUploadParams(searchParams, event) {
const name = searchParams.get('name')
const branch = searchParams.get('branch')
if (!name || !branch) {
throw new Error('Missing name or branch query parameter: ' + event.rawQueryString)
}
// this need validation based on user id
const Key = `meta/${name}/${branch}.json`
const s3Params = {
// @ts-ignore
Bucket: process.env.UploadBucket,
Key,
Expires: URL_EXPIRATION_SECONDS,
ContentType: 'application/json',
ACL: 'public-read'
}
return s3Params
}
function carUploadParams(searchParams, event, type: 'data' | 'file') {
const name = searchParams.get('name')
const carCid = searchParams.get('car')
if (!carCid || !name) {
throw new Error('Missing name or car query parameter: ' + event.rawQueryString)
}
const cid = CID.parse(carCid)
const checksum = base64pad.baseEncode(cid.multihash.digest)
const Key = `${type}/${name}/${cid.toString()}.car`
const s3Params = {
// @ts-ignore
Bucket: process.env.UploadBucket,
Key,
Expires: URL_EXPIRATION_SECONDS,
ContentType: 'application/car',
ChecksumSHA256: checksum,
ACL: 'public-read'
}
return s3Params
}