Upload files
Files().upload(folder, source) sends a file to your tenant's file storage. You describe the source of the bytes with one of four helpers.
import sdk, { fromDisk } from '@scenid/cloud-sdk'
const files = sdk.asAdmin().Files()
const { status, result } = await files.upload('documents', fromDisk('./contract.pdf'))
console.log(status) // 201
console.log(result.url) // URL of the stored file
Sources
| Helper | Use when |
|---|---|
fromDisk(filePath, filename?) | The file is on the local disk. MIME type from the extension |
fromBase64(filename, data, mimeType?) | You have base64 content, e.g. from a JSON API |
fromBlob(filename, blob) | You already have a Blob |
fromMultiPart(file, filename?) | An uploaded file from multer or express-fileupload |
import { fromBase64 } from '@scenid/cloud-sdk'
await files.upload('avatars', fromBase64('anja.png', base64String, 'image/png'))
Forward a browser upload
import express from 'express'
import multer from 'multer'
import sdk, { fromMultiPart } from '@scenid/cloud-sdk'
const app = express()
const upload = multer()
app.post('/upload', upload.single('file'), async (req, res) => {
const { status, result } = await sdk.asAdmin().Files().upload('uploads', fromMultiPart(req.file))
res.status(status).json(result)
})
Behaviour
folderis one path segment (for exampledocuments). It is not URL-encoded. Do not pass slashes inside it. To choose the area, prefix it withpublic/orprotected/(public/branding), otherwise the file is private. See Private, protected and public files.- The response is
{ status, result }. On success (201)resultdescribes the file, with itsurl. For images, Scenid also creates variants (avatar and image sizes). - Images larger than 2 MB are rejected (
413). Other file types have no size limit in a multipart upload. For very large files use?mode=rawover REST (up to 500 MB). - Uploading the same content twice into the same folder returns the existing file. It is not stored twice.
- The
urlin the result contains the file token needed to read the file. Store it as returned. Uploading identical content again returns a new token and invalidates the previous one.
Limits of the SDK
The SDK can upload only. Downloading and deleting are done through the file URL with a file token (and a Bearer token for private files), see Private, protected and public files and the REST reference.
Non-JSON responses
Unlike other SDK calls, upload parses the response body directly. A gateway error page (HTML) surfaces as a SyntaxError. Wrap the call in try/catch.