diff --git a/packages/commons/src/file-manager/fileManager.ts b/packages/commons/src/file-manager/fileManager.ts index 58f9a10443..df56e5077c 100644 --- a/packages/commons/src/file-manager/fileManager.ts +++ b/packages/commons/src/file-manager/fileManager.ts @@ -44,9 +44,9 @@ export type FileManager = { }, logger: Logger ) => Promise; - storeBytesByPath: ( + storeBytesByKey: ( bucket: string, - path: string, + key: string, fileContent: Buffer, logger: Logger ) => Promise; @@ -85,6 +85,25 @@ export function initFileManager( ): string => [path, resourceId, fileName].filter((s) => s && s.length > 0).join("/"); + const store = async ( + bucket: string, + key: string, + fileContent: Buffer + ): Promise => { + try { + await client.send( + new PutObjectCommand({ + Bucket: bucket, + Key: key, + Body: fileContent, + }) + ); + return key; + } catch (error) { + throw fileManagerStoreBytesError(key, bucket, error); + } + }; + return { buildS3Key: ( path: string, @@ -172,6 +191,15 @@ export function initFileManager( throw fileManagerListFilesError(bucket, error); } }, + storeBytesByKey: async ( + bucket: string, + key: string, + fileContent: Buffer, + logger: Logger + ): Promise => { + logger.info(`Storing file ${key} in bucket ${bucket}`); + return store(bucket, key, fileContent); + }, storeBytes: async ( s3File: { bucket: string; @@ -184,18 +212,8 @@ export function initFileManager( ): Promise => { const key = buildS3Key(s3File.path, s3File.resourceId, s3File.name); logger.info(`Storing file ${key} in bucket ${s3File.bucket}`); - try { - await client.send( - new PutObjectCommand({ - Bucket: s3File.bucket, - Key: key, - Body: s3File.content, - }) - ); - return key; - } catch (error) { - throw fileManagerStoreBytesError(key, s3File.bucket, error); - } + + return store(s3File.bucket, key, s3File.content); }, generateGetPresignedUrl: async ( bucketName: string, @@ -217,25 +235,5 @@ export function initFileManager( const command = new PutObjectCommand({ Bucket: bucketName, Key: key }); return getSignedUrl(client, command, { expiresIn: durationInMinutes }); }, - storeBytesByPath: async ( - bucket: string, - path: string, - fileContent: Buffer, - logger: Logger - ): Promise => { - logger.info(`Storing file ${path} in bucket ${bucket}`); - try { - await client.send( - new PutObjectCommand({ - Bucket: bucket, - Key: path, - Body: fileContent, - }) - ); - return path; - } catch (error) { - throw fileManagerStoreBytesError(path, bucket, error); - } - }, }; } diff --git a/packages/ivass-certified-attributes-importer/src/service/fileDownloader.ts b/packages/ivass-certified-attributes-importer/src/service/fileDownloader.ts index cfd638c17c..dfad5d10a4 100644 --- a/packages/ivass-certified-attributes-importer/src/service/fileDownloader.ts +++ b/packages/ivass-certified-attributes-importer/src/service/fileDownloader.ts @@ -74,7 +74,7 @@ export const downloadCSV = async ( const { blob, filename } = await downloadFile(sourceUrl); const zipFile = Buffer.from(await blob.arrayBuffer()); - await fileManager.storeBytesByPath( + await fileManager.storeBytesByKey( bucket, `organizations/${filename}`, zipFile,