Node.js - bucket.file.getDownloadUrl()
Create a download url for a file within a bucket.
import { bucket } from '@nitric/sdk'const assets = bucket('assets').allow('read', 'write')const logo = assets.file('images/logo.png')// Create a read-only signed url reference for downloadingconst downloadUrl = await logo.getDownloadUrl()
Parameters
- Name
options
- Optional
- Optional
- Type
- SignUrlOptions
- Description
Additional options when creating a signed URL.
- Name
expiry
- Optional
- Optional
- Type
- number
- Description
Seconds until link expiry. Defaults to
600
, Maximum of604800
(7 days).
Examples
Create a readable link that is valid for the next 5 minutes
import { bucket } from '@nitric/sdk'const assets = bucket('assets').allow('read')const logo = assets.file('images/logo.png')const logoUrl = await logo.getDownloadUrl({expiry: 300,})
Redirect response to an image URL
import { api, bucket } from '@nitric/sdk'const mainApi = api('main')const images = bucket('images').allow('read')mainApi.get('/images/:id', async ({ req, res }) => {const { id } = req.paramsconst signedUrl = await images.file(id).getDownloadUrl()res.status = 303res.headers['Location'] = [signedUrl]})
Last updated on Dec 19, 2024