Minimal library to upload files on Filebase S3 API. Partially based on AWS SDK v3.
pnpm i @stauro/filebase-upload
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.
The default apiUrl
is s3.filebase.com
. To change, pass in the url to the apiUrl
arg.
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,
})
await fetch(decodeURIComponent(url), { method: 'PUT', body: file })
And then run as:
node --env-file=.env main.js
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,
})
await fetch(decodeURIComponent(url), { method: 'PUT', body: file })
And then run as:
deno --allow-read --allow-net mod.ts
Upload a CAR file using createPresignedUrl
. Returns a response object.
import { uploadCar } from './mod.ts'
import { load } from 'https://deno.land/std@0.207.0/dotenv/mod.ts'
import { CAREncoderStream, createFileEncoderStream } from 'https://esm.sh/ipfs-car@1.0.0'
import { CID } from 'https://esm.sh/multiformats@12.1.3/cid'
const env = await load()
const init = {
bucketName: `filebase-upload-tests`,
token: env.FILEBASE_TOKEN,
}
const placeholderCID = CID.parse('bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi')
const stream = createFileEncoderStream(new Blob(['Hello ipfs-car!']))
.pipeThrough(
new TransformStream(),
)
.pipeThrough(new CAREncoderStream([placeholderCID]))
const blob = await new Response(stream).blob()
const file = new File([blob], 'file.car')
const res = await uploadCar({ ...init, file })
console.log(res.headers.get('x-amz-meta-cid'))
Creates a presigned URL for file upload. All options except apiUrl and metadata
are required.
Passing a metadata
object converts to a header object with x-amz-meta-<key>
.
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,
filename: 'hello.txt',
})
console.log(`${res.headers.get('x-amz-meta-cid')}:`, await res.text())
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,
filename: 'hello.txt',
})
console.log(`is uploaded? ${isUploaded ? 'yes' : 'no'}`)