@@ -29,7 +29,9 @@ Returns a paginated list of blobs.
29
29
30
30
``` ts [server/api/files.get.ts]
31
31
export default eventHandler (async () => {
32
- return hubBlob ().list ()
32
+ const { blobs } = await hubBlob ().list ({ limit: 10 })
33
+
34
+ return blobs
33
35
})
34
36
```
35
37
@@ -38,17 +40,19 @@ export default eventHandler(async () => {
38
40
:: field-group
39
41
:: field { name =" options " type =" Object " }
40
42
The list options.
41
- ::field{name="limit" type="Number"}
42
- The maximum number of blobs to return per request. Defaults to `1000`.
43
- ::
44
- ::field{name="prefix" type="String"}
45
- Filters the results to only those that begin with the specified prefix.
46
- ::
47
- ::field{name="cursor" type="String"}
48
- The cursor to continue from a previous list operation.
49
- ::
50
- ::field{name="folded" type="Boolean"}
51
- If `true`, the list will be folded using `/` separator and list of folders will be returned.
43
+ ::collapsible
44
+ ::field{name="limit" type="Number"}
45
+ The maximum number of blobs to return per request. Defaults to `1000`.
46
+ ::
47
+ ::field{name="prefix" type="String"}
48
+ Filters the results to only those that begin with the specified prefix.
49
+ ::
50
+ ::field{name="cursor" type="String"}
51
+ The cursor to continue from a previous list operation.
52
+ ::
53
+ ::field{name="folded" type="Boolean"}
54
+ If `true`, the list will be folded using `/` separator and list of folders will be returned.
55
+ ::
52
56
::
53
57
::
54
58
::
@@ -174,17 +178,22 @@ async function uploadImage (e: Event) {
174
178
::
175
179
::field{name="options" type="Object"}
176
180
The put options. Any other provided field will be stored in the blob's metadata.
177
- ::field{name="contentType" type="String"}
178
- The content type of the blob. If not given, it will be inferred from the Blob or the file extension.
179
- ::
180
- ::field{name="contentLength" type="String"}
181
- The content length of the blob.
182
- ::
183
- ::field{name="addRandomSuffix" type="Boolean"}
184
- If ` true ` , a random suffix will be added to the blob's name. Defaults to ` false ` .
185
- ::
186
- ::field{name="prefix" type="string"}
187
- The prefix to use for the blob pathname.
181
+ ::collapsible
182
+ ::field{name="contentType" type="String"}
183
+ The content type of the blob. If not given, it will be inferred from the Blob or the file extension.
184
+ ::
185
+ ::field{name="contentLength" type="String"}
186
+ The content length of the blob.
187
+ ::
188
+ ::field{name="addRandomSuffix" type="Boolean"}
189
+ If ` true ` , a random suffix will be added to the blob's name. Defaults to ` false ` .
190
+ ::
191
+ ::field{name="prefix" type="string"}
192
+ The prefix to use for the blob pathname.
193
+ ::
194
+ ::field{name="customMetadata" type="Record<string, string>"}
195
+ An object with custom metadata to store with the blob.
196
+ ::
188
197
::
189
198
::
190
199
::
@@ -231,16 +240,26 @@ Returns nothing.
231
240
232
241
### ` handleUpload ()`
233
242
234
- ` handleUpload ` is a all in one function to validate a ` Blob ` by checking its size and type and upload it to the storage.
235
- It can used to handle file uploads in API routes.
243
+ This is an "all in one" function to validate a ` Blob ` by checking its size and type and upload it to the storage.
244
+
245
+ ::note
246
+ This server util is made to be used with the [ ` useUpload ()` ](#useupload) Vue composable.
247
+ ::
248
+
249
+ It can be used to handle file uploads in API routes.
236
250
237
251
::code-group
238
252
` ` ` ts [server / api / blob .put .ts ]
239
253
export default eventHandler (async (event ) => {
240
254
return hubBlob ().handleUpload (event , {
241
255
formKey: ' files' , // read file or files form the `formKey` field of request body (body should be a `FormData` object)
242
256
multiple: true , // when `true`, the `formKey` field will be an array of `Blob` objects
243
- contentType: [' image/jpeg' , ' images/png' ], // allowed types of the file
257
+ ensure: {
258
+ contentType: [' image/jpeg' , ' images/png' ], // allowed types of the file
259
+ },
260
+ put: {
261
+ addRandomSuffix: true
262
+ }
244
263
})
245
264
})
246
265
` ` `
@@ -269,25 +288,11 @@ async function onFileSelect(event: Event) {
269
288
::field{name="multiple" type="boolean"}
270
289
When ` true ` , the ` formKey ` field will be an array of ` Blob ` objects.
271
290
::
272
- ::field{name="maxSize" type="BlobSize"}
273
- The maximum size of the file, should be: :br
274
- ( ` 1 ` | ` 2 ` | ` 4 ` | ` 8 ` | ` 16 ` | ` 32 ` | ` 64 ` | ` 128 ` | ` 256 ` | ` 512 ` | ` 1024 ` ) + ( ` B ` | ` KB ` | ` MB ` | ` GB ` ) :br
275
- e.g. ` ' 512KB' ` , ` ' 1MB' ` , ` ' 2GB' ` , etc.
276
- ::
277
- ::field{name="types" type="BlobType[]"}
278
- Allowed types of the file, e.g. ` [' image/jpeg' ]` .
279
- ::
280
- ::field{name="contentType" type="string"}
281
- The content type of the blob.
282
- ::
283
- ::field{name="contentLength" type="string"}
284
- The content length of the blob.
285
- ::
286
- ::field{name="addRandomSuffix" type="boolean"}
287
- If ` true ` , a random suffix will be added to the blob's name. Defaults to ` false ` .
291
+ ::field{name="ensure" type="BlobEnsureOptions"}
292
+ See [ ` ensureBlob ()` ](#ensureblob) options for more details.
288
293
::
289
- ::field{name="prefix " type="string "}
290
- The prefix to use for the blob pathname .
294
+ ::field{name="put " type="BlobPutOptions "}
295
+ See [ ` put () ` ](#put) options for more details .
291
296
::
292
297
::
293
298
@@ -370,14 +375,16 @@ export default eventHandler(async (event) => {
370
375
::
371
376
::field{name="options" type="Object"}
372
377
The put options. Any other provided field will be stored in the blob's metadata.
373
- ::field{name="contentType" type="String"}
374
- The content type of the blob. If not given, it will be inferred from the Blob or the file extension.
375
- ::
376
- ::field{name="contentLength" type="String"}
377
- The content length of the blob.
378
- ::
379
- ::field{name="addRandomSuffix" type="Boolean"}
380
- If ` true ` , a random suffix will be added to the blob's name. Defaults to ` true ` .
378
+ ::collapsible
379
+ ::field{name="contentType" type="String"}
380
+ The content type of the blob. If not given, it will be inferred from the Blob or the file extension.
381
+ ::
382
+ ::field{name="contentLength" type="String"}
383
+ The content length of the blob.
384
+ ::
385
+ ::field{name="addRandomSuffix" type="Boolean"}
386
+ If ` true ` , a random suffix will be added to the blob's name. Defaults to ` true ` .
387
+ ::
381
388
::
382
389
::
383
390
::
@@ -520,13 +527,15 @@ ensureBlob(file, { maxSize: '1MB', types: ['image' ]})
520
527
::
521
528
::field{name="options" type="Object" required}
522
529
Note that at least ` maxSize ` or ` types ` should be provided.
523
- ::field{name="maxSize" type="BlobSize"}
524
- The maximum size of the file, should be: :br
525
- ( ` 1 ` | ` 2 ` | ` 4 ` | ` 8 ` | ` 16 ` | ` 32 ` | ` 64 ` | ` 128 ` | ` 256 ` | ` 512 ` | ` 1024 ` ) + ( ` B ` | ` KB ` | ` MB ` | ` GB ` ) :br
526
- e.g. ` ' 512KB' ` , ` ' 1MB' ` , ` ' 2GB' ` , etc.
527
- ::
528
- ::field{name="types" type="BlobType[]"}
529
- Allowed types of the file, e.g. ` [' image/jpeg' ]` .
530
+ ::collapsible
531
+ ::field{name="maxSize" type="BlobSize"}
532
+ The maximum size of the file, should be: :br
533
+ ( ` 1 ` | ` 2 ` | ` 4 ` | ` 8 ` | ` 16 ` | ` 32 ` | ` 64 ` | ` 128 ` | ` 256 ` | ` 512 ` | ` 1024 ` ) + ( ` B ` | ` KB ` | ` MB ` | ` GB ` ) :br
534
+ e.g. ` ' 512KB' ` , ` ' 1MB' ` , ` ' 2GB' ` , etc.
535
+ ::
536
+ ::field{name="types" type="BlobType[]"}
537
+ Allowed types of the file, e.g. ` [' image/jpeg' ]` .
538
+ ::
530
539
::
531
540
::
532
541
::
@@ -577,14 +586,13 @@ async function onFileSelect({ target }: Event) {
577
586
::
578
587
::field{name="options" type="Object" required}
579
588
Optionally, you can pass Fetch options to the request. Read more about Fetch API [here](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options).
580
- ::field{name="formKey" type="string"}
581
- The key to add the file/files to the request form. Defaults to ` ' files' ` .
582
- ::
583
- ::field{name="multiple" type="boolean"}
584
- Whether to allow multiple files to be uploaded. Defaults to ` true ` .
585
- ::
586
- ::field{name="prefix" type="string"}
587
- The prefix to use for the blob pathname.
589
+ ::collapsible
590
+ ::field{name="formKey" type="string"}
591
+ The key to add the file/files to the request form. Defaults to ` ' files' ` .
592
+ ::
593
+ ::field{name="multiple" type="boolean"}
594
+ Whether to allow multiple files to be uploaded. Defaults to ` true ` .
595
+ ::
588
596
::
589
597
::
590
598
::
@@ -614,21 +622,23 @@ export const mpu = useMultipartUpload('/api/files/multipart')
614
622
::
615
623
::field{name="options"}
616
624
The options for the multipart upload helper.
617
- ::field{name="partSize" type="number"}
618
- The size of each part of the file to be uploaded. Defaults to ` 10MB ` .
619
- ::
620
- ::field{name="concurrent" type="number"}
621
- The maximum number of concurrent uploads. Defaults to ` 1 ` .
622
- ::
623
- ::field{name="maxRetry" type="number"}
624
- The maximum number of retry attempts for the whole upload. Defaults to ` 3 ` .
625
- ::
626
- ::field{name="prefix" type="string"}
627
- The prefix to use for the blob pathname.
628
- ::
629
- ::field{name="fetchOptions" type="Omit<FetchOptions, 'method' | 'baseURL' | 'body' | 'parseResponse' | 'responseType'>"}
630
- Override the ofetch options.
631
- The ` query ` and ` headers ` will be merged with the options provided by the uploader.
625
+ ::collapsible
626
+ ::field{name="partSize" type="number"}
627
+ The size of each part of the file to be uploaded. Defaults to ` 10MB ` .
628
+ ::
629
+ ::field{name="concurrent" type="number"}
630
+ The maximum number of concurrent uploads. Defaults to ` 1 ` .
631
+ ::
632
+ ::field{name="maxRetry" type="number"}
633
+ The maximum number of retry attempts for the whole upload. Defaults to ` 3 ` .
634
+ ::
635
+ ::field{name="prefix" type="string"}
636
+ The prefix to use for the blob pathname.
637
+ ::
638
+ ::field{name="fetchOptions" type="Omit<FetchOptions, 'method' | 'baseURL' | 'body' | 'parseResponse' | 'responseType'>"}
639
+ Override the ofetch options.
640
+ The ` query ` and ` headers ` will be merged with the options provided by the uploader.
641
+ ::
632
642
::
633
643
::
634
644
::
@@ -728,7 +738,7 @@ async function loadMore() {
728
738
return
729
739
}
730
740
const res = await $fetch (' /api/blobs' , {
731
- params : {
741
+ query : {
732
742
limit: 3 ,
733
743
cursor: cursor .value
734
744
}
0 commit comments