Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Dec 9, 2024

S3 Upload confirmation

You can use defineStorage and defineFunction to create a function trigger to confirm uploading a file.

To get started, install the @types/aws-lambda package, which contains types for different kinds of Lambda handlers, events, and responses.

Terminal
npm add --save @types/aws-lambda

Update your storage definition to define the onUpload trigger as below:

amplify/storage/resource.ts
import { defineFunction, defineStorage } from "@aws-amplify/backend";
export const storage = defineStorage({
name: 'myProjectFiles',
triggers: {
onUpload: defineFunction({
entry: './on-upload-handler.ts'
resourceGroupName: 'storage',
})
}
});

Next, create a file named amplify/storage/on-upload-handler.ts and use the following code to log the object keys whenever an object is uploaded to the bucket. You can add your custom logic to this function as needed.

amplify/storage/on-upload-handler.ts
import type { S3Handler } from 'aws-lambda';
export const handler: S3Handler = async (event) => {
const objectKeys = event.Records.map((record) => record.s3.object.key);
console.log(`Upload handler invoked for objects [${objectKeys.join(', ')}]`);
};

Now, when you deploy your backend, this function will be invoked whenever an object is uploaded to the bucket.