@opengis/fastify-table
Version:
core-plugins
33 lines (26 loc) • 872 B
JavaScript
import {
HeadObjectCommand,
} from '@aws-sdk/client-s3';
import client from '../client.js';
import getPath from '../../../utils/getPath.js';
import getS3FilePath from './utils/getS3FilePath.js';
// if not found on s3 => fs
import isFileExists from '../../../utils/isFileExists.js';
const getFileMetadata = (s3Settings) => async (fp) => {
const filepath = getS3FilePath(fp, s3Settings);
const s3Client = client.getS3Client(s3Settings);
const bucketName = s3Settings.containerName;
const bucketParams = {
Bucket: bucketName,
Key: filepath[0] === '/' ? filepath?.slice(1) : filepath,
};
try {
const data = await s3Client.send(new HeadObjectCommand(bucketParams));
return data;
}
catch (err) {
const filepath1 = getPath(fp);
return isFileExists(filepath1);
}
};
export default getFileMetadata;