useAccCdn
Authenticated file upload and private-file URL generation through the configured CDN.
Overview
useAccCdn() provides the complete file workflow for the configured CDN. It requests an upload URL through useAccApi(), uploads the file directly to S3, and returns the generated object key and CDN URL.
Use getPrivateFileUrl() when a private file needs a temporary CloudFront signed URL.
Setup
default is the namespace value used by default here and by the backend. A namespace identifies a CDN configuration handled by your backend; it is not a folder name. To use another CDN, add its namespace to levarneAcc.cdn.namespace and pass it to useAccCdn(namespace). The backend must also accept that namespace and route it to the intended CDN.
// nuxt.config.ts
export default defineNuxtConfig({
levarneAcc: {
cdn: {
namespace: ['default', 'secondary-cdn'],
},
},
})
// In a component or composable
const { uploadFile } = useAccCdn('secondary-cdn') Direct browser uploads also require the CDN bucket to allow the application origin, PUT, and Content-Type through S3 CORS.
Usage
const { uploadFile, getPrivateFileUrl } = useAccCdn()
const uploaded = await uploadFile(file, { visibility: 'private' })
const privateUrl = await getPrivateFileUrl(uploaded.key)Try it
This example uses the default CDN namespace and requires an authenticated session.
uploadFile
Requests a presigned S3 PUT URL, uploads the file, and returns its key, CDN URL, and visibility.
const { uploadFile } = useAccCdn()
const uploaded = await uploadFile(file, {
visibility: 'public',
})
// { key, fileUrl, visibility }getPrivateFileUrl
Requests a CloudFront signed URL for a private object key. The URL's Expires query parameter contains its expiration time as a Unix timestamp.
const { getPrivateFileUrl } = useAccCdn()
const fileUrl = await getPrivateFileUrl('default/private/abc.pdf')