no intentar siempre usar s3

This commit is contained in:
Cat /dev/Nulo 2024-09-12 19:25:45 -03:00
parent 02f63ab709
commit 2c2cc7ab21

View file

@ -15,19 +15,32 @@ function checkEnvVariable(variableName: string) {
process.exit(1); process.exit(1);
} }
} }
export const B2_BUCKET_NAME = checkEnvVariable("B2_BUCKET_NAME"); function getVariables() {
const B2_BUCKET_KEY_ID = checkEnvVariable("B2_BUCKET_KEY_ID"); const B2_BUCKET_NAME = checkEnvVariable("B2_BUCKET_NAME");
const B2_BUCKET_KEY = checkEnvVariable("B2_BUCKET_KEY"); const B2_BUCKET_KEY_ID = checkEnvVariable("B2_BUCKET_KEY_ID");
export const s3 = new S3Client({ const B2_BUCKET_KEY = checkEnvVariable("B2_BUCKET_KEY");
return {
B2_BUCKET_NAME,
B2_BUCKET_KEY_ID,
B2_BUCKET_KEY,
};
}
function getS3Client() {
const { B2_BUCKET_NAME, B2_BUCKET_KEY_ID, B2_BUCKET_KEY } = getVariables();
return new S3Client({
endpoint: "https://s3.us-west-004.backblazeb2.com", endpoint: "https://s3.us-west-004.backblazeb2.com",
region: "us-west-004", region: "us-west-004",
credentials: { credentials: {
accessKeyId: B2_BUCKET_KEY_ID, accessKeyId: B2_BUCKET_KEY_ID,
secretAccessKey: B2_BUCKET_KEY, secretAccessKey: B2_BUCKET_KEY,
}, },
}); });
}
export async function listDirectory(directoryName: string) { export async function listDirectory(directoryName: string) {
const { B2_BUCKET_NAME } = getVariables();
const s3 = getS3Client();
const command = new ListObjectsV2Command({ const command = new ListObjectsV2Command({
Bucket: B2_BUCKET_NAME, Bucket: B2_BUCKET_NAME,
Prefix: directoryName ? `${directoryName}/` : "", Prefix: directoryName ? `${directoryName}/` : "",
@ -39,6 +52,8 @@ export async function listDirectory(directoryName: string) {
} }
export async function getFileContent(fileName: string) { export async function getFileContent(fileName: string) {
const { B2_BUCKET_NAME } = getVariables();
const s3 = getS3Client();
const fileContent = await s3.send( const fileContent = await s3.send(
new GetObjectCommand({ new GetObjectCommand({
Bucket: B2_BUCKET_NAME, Bucket: B2_BUCKET_NAME,
@ -49,6 +64,8 @@ export async function getFileContent(fileName: string) {
} }
export async function checkFileExists(fileName: string): Promise<boolean> { export async function checkFileExists(fileName: string): Promise<boolean> {
const { B2_BUCKET_NAME } = getVariables();
const s3 = getS3Client();
try { try {
await s3.send( await s3.send(
new HeadObjectCommand({ new HeadObjectCommand({