Skip to main content
Deno 2 is finally here 🎉️
Learn more

filebase-upload

Version GitHub Workflow Status Codecov

Minimal library to upload files on Filebase S3 API. Partially based on AWS SDK v3.

Install

pnpm i @stauro/filebase-upload

Usage

First, you need to set up a token. A token is a base64 encoded pair of the access key and access secret.

You can generate a token like this:

echo "accessKey:accessSecret" | base64

and save it to a .env file or somewhere else.

Node.js

import { createPresignedUrl } from '@stauro/filebase-upload'

const file = new File(['Hello world'], 'hello.txt')

const url = await createPresignedUrl({
  bucketName: `example-${crypto.randomUUID()}`,
  token: process.env.FILEBASE_TOKEN,
  file,
  apiUrl: 's3.filebase.com',
})

await fetch(decodeURIComponent(url), { method: 'PUT', body: file })

And then run as:

node --env-file=.env main.js

Deno

import { createPresignedUrl } from 'https://deno.land/x/filebase_upload/mod.ts'
import { load } from 'https://deno.land/std@0.207.0/dotenv/mod.ts'

const env = await load()

const file = new File(['Hello world'], 'hello.txt')

const url = await createPresignedUrl({
  bucketName: `example-${crypto.randomUUID()}`,
  token: env.FILEBASE_TOKEN,
  file,
  apiUrl: 's3.filebase.com',
})

await fetch(decodeURIComponent(url), { method: 'PUT', body: file })

And then run as:

deno --allow-read --allow-net mod.ts

API

createPresignedUrl

Creates a presigned URL for file upload. All options are required.

getObject

Retrieves an object from Filebase S3 API and returns a response object.

You can also retrieve the file CID from headers.

import { getObject } from 'https://deno.land/x/filebase_upload/mod.ts'

const res = await getObject({
  bucketName: `example-${crypto.randomUUID()}`,
  token: env.FILEBASE_TOKEN,
  apiUrl: 's3.filebase.com',
  filename: 'hello.txt',
})

console.log(`${res.headers.get('x-amz-meta-cid')}:`, await res.text())

headObject

Checks if the file has been uploaded. Returns a boolean and a CID of the file (if uploaded).

import { headObject } from 'https://deno.land/x/filebase_upload/mod.ts'

const [isUploaded, cid] = await headObject({
  bucketName: `example-${crypto.randomUUID()}`,
  token: env.FILEBASE_TOKEN,
  apiUrl: 's3.filebase.com',
  filename: 'hello.txt',
})

console.log(`is uploaded? ${isUploaded ? 'yes' : 'no'}`)