-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.html
267 lines (227 loc) · 8.58 KB
/
index.html
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Uppy – AWS upload example</title>
<link
href="https://releases.transloadit.com/uppy/v3.25.0/uppy.min.css"
rel="stylesheet"
/>
</head>
<body>
<h1>AWS upload example</h1>
<div id="uppy"></div>
<script type="module">
import {
Uppy,
Dashboard,
AwsS3,
} from 'https://releases.transloadit.com/uppy/v3.25.0/uppy.min.mjs'
/**
* This generator transforms a deep object into URL-encodable pairs
* to work with `URLSearchParams` on the client and `body-parser` on the server.
*/
function* serializeSubPart(key, value) {
if (typeof value !== 'object') {
yield [key, value]
return
}
if (Array.isArray(value)) {
for (const val of value) {
yield* serializeSubPart(`${key}[]`, val)
}
return
}
for (const [subkey, val] of Object.entries(value)) {
yield* serializeSubPart(key ? `${key}[${subkey}]` : subkey, val)
}
}
function serialize(data) {
// If you want to avoid preflight requests, use URL-encoded syntax:
return new URLSearchParams(serializeSubPart(null, data))
// If you don't care about additional preflight requests, you can also use:
// return JSON.stringify(data)
// You'd also have to add `Content-Type` header with value `application/json`.
}
{
const MiB = 0x10_00_00
const uppy = new Uppy()
.use(Dashboard, {
inline: true,
target: '#uppy',
})
.use(AwsS3, {
id: 'myAWSPlugin',
// Files that are more than 100MiB should be uploaded in multiple parts.
shouldUseMultipart: (file) => file.size > 100 * MiB,
/**
* This method tells Uppy how to retrieve a temporary token for signing on the client.
* Signing on the client is optional, you can also do the signing from the server.
*/
async getTemporarySecurityCredentials({ signal }) {
const response = await fetch('/sts', { signal })
if (!response.ok)
throw new Error('Unsuccessful request', { cause: response })
return response.json()
},
// ========== Non-Multipart Uploads ==========
/**
* This method tells Uppy how to handle non-multipart uploads.
* If for some reason you want to only support multipart uploads,
* you don't need to implement it.
*/
async getUploadParameters(file, options) {
if (typeof crypto?.subtle === 'object') {
// If WebCrypto is available, let's do signing from the client.
return uppy
.getPlugin('myAWSPlugin')
.createSignedURL(file, options)
}
// Send a request to our Express.js signing endpoint.
const response = await fetch('/sign-s3', {
method: 'POST',
headers: {
accept: 'application/json',
},
body: serialize({
filename: file.name,
contentType: file.type,
}),
signal: options.signal,
})
if (!response.ok)
throw new Error('Unsuccessful request', { cause: response })
// Parse the JSON response.
const data = await response.json()
// Return an object in the correct shape.
return {
method: data.method,
url: data.url,
fields: {}, // For presigned PUT uploads, this should be left empty.
// Provide content type header required by S3
headers: {
'Content-Type': file.type,
},
}
},
// ========== Multipart Uploads ==========
// The following methods are only useful for multipart uploads:
// If you are not interested in multipart uploads, you don't need to
// implement them (you'd also need to set `shouldUseMultipart: false` though).
async createMultipartUpload(file, signal) {
signal?.throwIfAborted()
const metadata = {}
Object.keys(file.meta || {}).forEach((key) => {
if (file.meta[key] != null) {
metadata[key] = file.meta[key].toString()
}
})
const response = await fetch('/s3/multipart', {
method: 'POST',
// Send and receive JSON.
headers: {
accept: 'application/json',
},
body: serialize({
filename: file.name,
type: file.type,
metadata,
}),
signal,
})
if (!response.ok)
throw new Error('Unsuccessful request', { cause: response })
// Parse the JSON response.
const data = await response.json()
return data
},
async abortMultipartUpload(file, { key, uploadId }, signal) {
const filename = encodeURIComponent(key)
const uploadIdEnc = encodeURIComponent(uploadId)
const response = await fetch(
`/s3/multipart/${uploadIdEnc}?key=${filename}`,
{
method: 'DELETE',
signal,
},
)
if (!response.ok)
throw new Error('Unsuccessful request', { cause: response })
},
async signPart(file, options) {
if (typeof crypto?.subtle === 'object') {
// If WebCrypto, let's do signing from the client.
return uppy
.getPlugin('myAWSPlugin')
.createSignedURL(file, options)
}
const { uploadId, key, partNumber, signal } = options
signal?.throwIfAborted()
if (uploadId == null || key == null || partNumber == null) {
throw new Error(
'Cannot sign without a key, an uploadId, and a partNumber',
)
}
const filename = encodeURIComponent(key)
const response = await fetch(
`/s3/multipart/${uploadId}/${partNumber}?key=${filename}`,
{ signal },
)
if (!response.ok)
throw new Error('Unsuccessful request', { cause: response })
const data = await response.json()
return data
},
async listParts(file, { key, uploadId }, signal) {
signal?.throwIfAborted()
const filename = encodeURIComponent(key)
const response = await fetch(
`/s3/multipart/${uploadId}?key=${filename}`,
{ signal },
)
if (!response.ok)
throw new Error('Unsuccessful request', { cause: response })
const data = await response.json()
return data
},
async completeMultipartUpload(
file,
{ key, uploadId, parts },
signal,
) {
signal?.throwIfAborted()
const filename = encodeURIComponent(key)
const uploadIdEnc = encodeURIComponent(uploadId)
const response = await fetch(
`s3/multipart/${uploadIdEnc}/complete?key=${filename}`,
{
method: 'POST',
headers: {
accept: 'application/json',
},
body: serialize({ parts }),
signal,
},
)
if (!response.ok)
throw new Error('Unsuccessful request', { cause: response })
const data = await response.json()
return data
},
})
uppy.on('complete', (result) => {
console.log(
'Upload complete! We’ve uploaded these files:',
result.successful,
)
})
uppy.on('upload-success', (file, data) => {
console.log(
'Upload success! We’ve uploaded this file:',
file.meta['name'],
)
})
}
</script>
</body>
</html>