Multiple object add in loop is not working

here is my node js code. I want to add multiple objects in loop, but only one object add and other objects are fails.

let me know what issue here?

const uploadMedia = async (req, res) => {
try {
const files = req.files;

    const userId = req.params.userId


    const userMetaData = await UserMeta.findOne({ user_id: userId }, { profileSlug: 1 });

    const cf_account_id = "";
    const cd_r2_access_key_id = "";
    const cd_r2_secret_access_key = "";
    const bucket = '';


    const S3 = new S3Client({
        endpoint: `https://${cf_account_id}.r2.cloudflarestorage.com`,
        credentials: {
          accessKeyId: cd_r2_access_key_id,
          secretAccessKey: cd_r2_secret_access_key,
        },
        region: "auto"
      });

      console.log({endpoint: `https://${cf_account_id}.r2.cloudflarestorage.com`,
      credentials: {
        accessKeyId: cd_r2_access_key_id,
        secretAccessKey: cd_r2_secret_access_key,
      },
      region: "auto"})


    // Create an array to hold promises for saving media documents
    const mediaPromises = files.map(async file => {

        let object_id = file.originalname; // Initialize object_id with the original filename
        let counter = 1;

        // Check if the object_id already exists in the database
        while (await Media.exists({ object_id })) {
            const fileExtensionIndex = object_id.lastIndexOf('.');
            const fileName = object_id.substring(0, fileExtensionIndex); // Extract filename without extension
            const fileExtension = object_id.substring(fileExtensionIndex); // Extract file extension

            // Modify object_id by adding a counter before the file extension
            object_id = `${fileName}-${counter}${fileExtension}`;
            counter++;
        }

        const newMedia = new Media({
            name: file.originalname,
            description: file.originalname,
            alt: file.originalname,
            object_id: object_id,
            type: file.mimetype,
            size: file.size,
            author_id: userId
        });

        // Save the media document
        await newMedia.save();

        S3.middlewareStack.add(
            (next, context) => async (args) => {
              const r = args.request;
              r.headers["cf-create-bucket-if-missing"] = "true";
              return await next(args);
            },
            { step: 'build', name: 'customHeaders' }
        );

        const command = new PutObjectCommand({
            Bucket: bucket,
            Key: object_id,
            Body: file.buffer
        });

        console.log({Bucket: bucket,
            Key: object_id,
            Body: file.buffer})

        S3.send(command)
            .then((response) => {
                console.log({response:response, object_id:object_id});
            })
            .catch((error) => {
                console.log({error:error, message: error.message, object_id:object_id});
                console.error(error);
            });

        return newMedia;
    });

    // Wait for all media documents to be saved
    const savedMedia = await Promise.all(mediaPromises);

    res.status(200).json({ status: true, message: 'get media', result: savedMedia });
}  catch (error) {
    // Handle errors
    res.status(500).json({ status: false, error: error.message });
}

}