DEV Community

Richard
Richard

Posted on

Getting data back from Async function in NextJS

I have the following code in /lib/s3libdata.js:

import { Upload } from "@aws-sdk/lib-storage";
import {S3, S3Client} from "@aws-sdk/client-s3";

export const putObject = async (uploadFilePath, fileStream) => {
    const upload = new Upload({
        params: {
            Bucket: process.env.AWS_BUCKET,
            Key: uploadFilePath,
            Body: fileStream,
        },
        client: new S3Client({
            region: process.env.AWS_REGION,
            credentials: {
                accessKeyId: process.env.AWS_ACCESS_KEY,
                secretAccessKey: process.env.AWS_SECRET_KEY,
            },
        }),
        queueSize: 3,
    });

    upload.on("httpUploadProgress", (progress) => {
        console.log(progress);
    });

    await upload.done();
};
Enter fullscreen mode Exit fullscreen mode

Using the @aws-sdk/lib-storage. The upload works, and in my terminal I get the console log output of the progress. My question now is how do I send that progress information back to the page?

The way I call this is from /pages/api/upload.js which is called from /pages/upload.js.

Ideally I want the upload.js page to display the progress data. How can I achieve this?

Top comments (0)