Skip to content

Commit

Permalink
prepare new S3 files key format
Browse files Browse the repository at this point in the history
  • Loading branch information
SteRiccio committed Nov 6, 2024
1 parent 6cc002e commit 94e9ed1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
9 changes: 4 additions & 5 deletions server/modules/record/manager/fileManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Objects, Promises } from '@openforis/arena-core'
import { Objects } from '@openforis/arena-core'

import { ENV } from '@core/processUtils'
import * as RecordFile from '@core/record/recordFile'
Expand Down Expand Up @@ -119,13 +119,12 @@ export const moveFilesToNewStorageIfNecessary = async ({ surveyId }, client = db
logger.debug(`Survey ${surveyId}: started moving ${fileUuids.length} files from DB to new storage (${storageType})`)

await client.tx(async (tx) => {
await Promises.each(fileUuids, async (fileUuid) => {
for await (const fileUuid of fileUuids) {
const file = await FileRepository.fetchFileAndContentByUuid(surveyId, fileUuid, tx)

const contentStoreFunction = contentStoreFunctionByStorageType[storageType]
const content = RecordFile.getContent(file)
const contentStoreFunction = contentStoreFunctionByStorageType[storageType]
await contentStoreFunction({ surveyId, fileUuid, content })
})
}
logger.debug(`Files moved from DB; clearing 'content' column in DB 'file' table`)
await FileRepository.clearAllSurveyFilesContent({ surveyId }, tx)
})
Expand Down
36 changes: 30 additions & 6 deletions server/modules/record/repository/fileRepositoryS3Bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
DeleteObjectCommand,
GetObjectCommand,
HeadBucketCommand,
HeadObjectCommand,
PutObjectCommand,
S3Client,
} from '@aws-sdk/client-s3'
Expand All @@ -18,16 +19,21 @@ const s3Client = new S3Client({
region: ProcessUtils.ENV.fileStorageAwsS3BucketRegion,
})

const getFileKey = ({ surveyId, fileUuid }) => `${surveyId}_${fileUuid}`
const getFileKey = ({ surveyId, fileUuid }) => `surveys/${surveyId}/files/${fileUuid}`

const createCommandParams = ({ surveyId, fileUuid }) => ({
Bucket,
Key: getFileKey({ surveyId, fileUuid }),
})
const createCommandParams = ({ surveyId, fileUuid }) => {
const Key = getFileKey({ surveyId, fileUuid })
return { Bucket, Key }
}

const fileExists = async (Key) => {
const command = new HeadObjectCommand({ Bucket, Key })
const data = await s3Client.send(command)
return data.$metadata.httpStatusCode === 200
}

export const checkCanAccessS3Bucket = async () => {
const command = new HeadBucketCommand({ Bucket })

try {
await s3Client.send(command)
return true
Expand Down Expand Up @@ -57,3 +63,21 @@ export const deleteFiles = async ({ surveyId, fileUuids }) =>
return s3Client.send(command)
})
)

const getOldFileKey = ({ surveyId, fileUuid }) => `${surveyId}_${fileUuid}`

const createOldKeyCommandParams = ({ surveyId, fileUuid }) => {
const Key = getOldFileKey({ surveyId, fileUuid })
return { Bucket, Key }
}

export const oldKeyFileExists = async ({ surveyId, fileUuid }) => {
const Key = getOldFileKey({ surveyId, fileUuid })
return fileExists(Key)
}

export const getOldKeyFileContentAsStream = async ({ surveyId, fileUuid }) => {
const command = new GetObjectCommand(createOldKeyCommandParams({ surveyId, fileUuid }))
const response = await s3Client.send(command)
return response.Body
}
6 changes: 2 additions & 4 deletions server/modules/record/service/fileService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Promises } from '@openforis/arena-core'

import * as Log from '@server/log/log'
import * as SurveyRepository from '@server/modules/survey/repository/surveyRepository'

Expand All @@ -23,15 +21,15 @@ export const checkFilesStorage = async () => {
const surveyIds = await SurveyRepository.fetchAllSurveyIds()
let allSurveysFilesMoved = false
let errorsFound = false
await Promises.each(surveyIds, async (surveyId) => {
for await (const surveyId of surveyIds) {
try {
const surveyFilesMoved = await FileManager.moveFilesToNewStorageIfNecessary({ surveyId })
allSurveysFilesMoved = allSurveysFilesMoved || surveyFilesMoved
} catch (error) {
errorsFound = true
logger.error(`Error moving files for survey ${surveyId}`, error)
}
})
}
if (errorsFound) {
logger.error(`There were errors moving survey files to the new storage`)
} else if (allSurveysFilesMoved) {
Expand Down

0 comments on commit 94e9ed1

Please sign in to comment.