File Upload App Patterns

💡 Learn patterns for implementing file upload functionality in your app, from file selection to upload, progress display, and previews.

Overview

This guide explains how to implement file upload functionality using the bkend storage API. Since files are uploaded directly to storage via Presigned URLs, even large files can be handled reliably.

spinner

Overall Flow

File upload consists of 3 API calls.

Step
Method
Endpoint
Description

1

POST

/v1/files/presigned-url

Obtain Presigned URL

2

PUT

Issued storage URL

Upload file directly to storage

3

POST

/v1/files

Register metadata with bkend

💡 Presigned URLs are valid for 15 minutes only. Complete the upload before they expire.


1. File Selection UI

Basic File Input

File Selection Event


2. File Validation

Validate file size and format on the client side before uploading.


3. Upload Implementation

3-Step Upload Function

Storage Upload (with Progress Tracking)

Use XMLHttpRequest to track upload progress.

Connect the Upload Button


4. Image Preview

Show a preview when the user selects an image file.

Preview After Upload (Using Download URL)

Display uploaded files by obtaining a download URL.

Optimized URL After Upload (Public Images)

If the file's visibility is public, you can display resized images directly through the img.bkend.ai CDN.

💡 The img.bkend.ai CDN only supports files with public visibility. For private/protected/shared files, use the download URL approach above. See Image Optimization for details.


5. Error Handling

Handle errors from each step separately.

Error Code Summary

Error Code
HTTP
Stage
Description

file/invalid-name

400

Presigned URL

Invalid file name

file/file-too-large

400

Presigned URL

File size exceeded

file/invalid-format

400

Presigned URL

Unsupported format

file/s3-key-already-exists

409

Metadata registration

File already registered

file/access-denied

403

All stages

Access denied

common/authentication-required

401

All stages

Authentication required

file/bucket-not-configured

500

Presigned URL

Storage bucket not configured


6. File Management

Implement file listing and deletion for uploaded files.

File List

File Deletion


API Summary

Feature
Method
Endpoint
Content-Type

Obtain Presigned URL

POST

/v1/files/presigned-url

application/json

Upload to Storage

PUT

Issued URL

File's MIME type

Register metadata

POST

/v1/files

application/json

File list

GET

/v1/files

-

Retrieve file

GET

/v1/files/{fileId}

-

Delete file

DELETE

/v1/files/{fileId}

-


Next Steps

Last updated