s3: create the client instance from a function each time (so missing config doesn't crash it)

This commit is contained in:
Alex Gleason 2023-09-11 14:41:30 -05:00
parent c20e0a0200
commit 94671b6a98
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 7 additions and 4 deletions

View File

@ -3,8 +3,6 @@ import { IpfsHash, S3Client } from '@/deps.ts';
import type { Uploader } from './types.ts';
const s3 = new S3Client({ ...Conf.s3 });
/**
* S3-compatible uploader for AWS, Wasabi, DigitalOcean Spaces, and more.
* Files are named by their IPFS CID and exposed at `/ipfs/<cid>`, letting it
@ -14,7 +12,7 @@ const s3Uploader: Uploader = {
async upload(file) {
const cid = await IpfsHash.of(file.stream()) as string;
await s3.putObject(`ipfs/${cid}`, file.stream(), {
await client().putObject(`ipfs/${cid}`, file.stream(), {
metadata: {
'Content-Type': file.type,
'x-amz-acl': 'public-read',
@ -26,8 +24,13 @@ const s3Uploader: Uploader = {
};
},
async delete(cid) {
await s3.deleteObject(`ipfs/${cid}`);
await client().deleteObject(`ipfs/${cid}`);
},
};
/** Build S3 client from config. */
function client() {
return new S3Client({ ...Conf.s3 });
}
export { s3Uploader };