From e3d2e7008ef44108a775ca150aab3833f1701bd7 Mon Sep 17 00:00:00 2001 From: Derek Sonnenberg Date: Fri, 7 Jun 2024 13:36:23 -0500 Subject: [PATCH] refactor(s3-data-source): use aws lite client for header check PE-6106 --- src/data/s3-data-source.ts | 18 ++++++++++-------- src/system.ts | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/data/s3-data-source.ts b/src/data/s3-data-source.ts index 1537e7b8..513b33af 100644 --- a/src/data/s3-data-source.ts +++ b/src/data/s3-data-source.ts @@ -25,8 +25,7 @@ import { } from '../types.js'; import { AwsLiteS3 } from '@aws-lite/s3-types'; import { Readable } from 'node:stream'; -import axios from 'axios'; -import { AWS_ENDPOINT } from '../config.js'; +import { awsClient } from '../system.js'; export class S3DataSource implements ContiguousDataSource { private log: winston.Logger; @@ -66,7 +65,7 @@ export class S3DataSource implements ContiguousDataSource { }); try { - // TODO: Use S3 client instead of axios when aws-lite supports Metadata on head-requests + // TODO: Use S3 client instead of accessing aws client directly when aws-lite s3 supports Metadata on head-requests // const head = this.s3Client.HeadObject({ // Bucket: this.s3Bucket, // Key: `${this.s3Prefix}/${id}`, @@ -77,11 +76,14 @@ export class S3DataSource implements ContiguousDataSource { bucket: this.s3Bucket, prefix: this.s3Prefix, }); - const head = await axios.head( - `${AWS_ENDPOINT}/${this.s3Bucket}/${this.s3Prefix}/${id}`, - { validateStatus: () => true }, - ); - if (head.status !== 200) { + + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const head = await awsClient!({ + service: 's3', + path: `${this.s3Bucket}/${this.s3Prefix}/${id}`, + }); + + if (head.statusCode !== 200) { throw new Error('Failed to head data from S3'); } diff --git a/src/system.ts b/src/system.ts index 4cd47a55..6dc64a7b 100644 --- a/src/system.ts +++ b/src/system.ts @@ -80,7 +80,7 @@ process.on('uncaughtException', (error) => { const arweave = Arweave.init({}); const arIO = IO.init({ processId: config.IO_PROCESS_ID }); -const awsClient = +export const awsClient = config.AWS_ACCESS_KEY_ID !== undefined && config.AWS_SECRET_ACCESS_KEY !== undefined && config.AWS_REGION !== undefined