Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change handleDeleteFile in app-web #2822

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
43 changes: 40 additions & 3 deletions services/app-web/src/s3/s3Amplify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,49 @@ function newAmplifyS3Client(bucketConfig: S3BucketConfigType): S3ClientT {
filename: string,
bucket: BucketShortName
): Promise<void | S3Error> => {
// Construct the full key including the bucket prefix
const fullKey = `${bucketConfig[bucket]}/${filename}`
console.info(`Attempting to tag file as deleted: ${fullKey}`)
let metadata

try {
await Storage.remove(filename, {
bucket: bucketConfig[bucket],
})
const result = await Storage.getProperties(fullKey)
metadata = result.metadata
console.info('Successfully got file properties', metadata)
} catch (getPropertiesError) {
console.error('Error in getProperties:', getPropertiesError)
throw getPropertiesError
}

try {
// Add or update the 'deleted' tag
console.info('Preparing updated metadata')
const updatedMetadata = {
...metadata,
deleted: 'true',
deletedAt: new Date().toISOString(),
}

// Update the file's metadata
console.info('Copying file to update metadata')
try {
await Storage.copy(
{ key: fullKey },
{ key: fullKey },
{ metadata: updatedMetadata }
)
console.info('Successfully updated file metadata')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting idea, I like it because we still retain something to show that a field may have been removed in the UI.

} catch (copyError) {
console.error('Error in Storage.copy:', copyError)
throw copyError
}

console.info(
`File ${filename} tagged as deleted in bucket ${bucket}`
)
return
} catch (err) {
console.error('Error in tagFileAsDeleted:', err)
assertIsS3PutError(err)
recordJSException(err)
if (err.name === 'Error' && err.message === 'Network Error') {
Expand Down
9 changes: 9 additions & 0 deletions services/uploads/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ provider:
- Effect: 'Allow'
Action:
- s3:GetObject
- s3:GetObjectAttributes
- s3:GetObjectTagging
- s3:PutObject
- s3:PutObjectAcl
Expand All @@ -39,6 +40,14 @@ provider:
- !Sub arn:aws:s3:::${self:service}-${sls:stage}-qa-${AWS::AccountId}/*
- Effect: 'Allow'
Action:
- s3:GetObject
- s3:GetObjectAttributes
- s3:GetObjectTagging
- s3:PutObject
- s3:PutObjectAcl
- s3:PutObjectTagging
- s3:PutObjectVersionTagging
- s3:DeleteObject
- s3:ListBucket
Resource:
- !Sub arn:aws:s3:::${self:service}-${sls:stage}-uploads-${AWS::AccountId}
Expand Down
Loading