UNPKG

@message-queue-toolkit/s3-payload-store

Version:

AWS S3-based message store implementation for message-queue-toolkit

54 lines 1.65 kB
import { randomUUID } from 'node:crypto'; import { NoSuchKey } from '@aws-sdk/client-s3'; export function resolvePayloadStoreConfig(dependencies, config) { if (!config?.s3PayloadOffloadingBucket) return undefined; if (!dependencies.s3) throw new Error('AWS S3 client is required for payload offloading'); return { store: new S3PayloadStore({ s3: dependencies.s3 }, { bucketName: config.s3PayloadOffloadingBucket }), storeName: 's3', messageSizeThreshold: config.messageSizeThreshold, }; } export class S3PayloadStore { s3; config; constructor({ s3 }, config) { this.s3 = s3; this.config = config; } async storePayload(payload) { const id = randomUUID(); const key = this.config?.keyPrefix?.length ? `${this.config.keyPrefix}/${id}` : id; await this.s3.putObject({ Bucket: this.config.bucketName, Key: key, Body: payload.value, ContentLength: payload.size, }); return key; } async retrievePayload(key) { try { const result = await this.s3.getObject({ Bucket: this.config.bucketName, Key: key, }); return result.Body ? result.Body : null; } catch (e) { if (e instanceof NoSuchKey) { return null; } throw e; } } async deletePayload(key) { await this.s3.deleteObject({ Bucket: this.config.bucketName, Key: key, }); } } //# sourceMappingURL=S3PayloadStore.js.map