I use react native and backend was built with Prisma and GraphQL (Apollo Server). I don't store image data to Prisma but to aw3.

The problem is I want to upload several images at once to my app. So I make image column of Prisma Array [], not String.

But as using aw3, I can upload only one image at once. So even if I make image column as Array, I can't upload several images at once as Array using aw3.

When I searched people suggest 3 options in order to upload multiple files by aw3.

  1. multi-thread
  2. multi-processing
  3. zip upload (amazon-lambda)

In my case(to upload files as Array), which option is most advisable? And can you teach me the way of doing that?

My backend code:

export const uploadToS3 = async (file, userId, folderName) => {
  const { filename, createReadStream } = await file;
  const readStream = createReadStream();
  const objectName = `${folderName}/${userId}-${Date.now()}-${filename}`;
  const { Location } = await new AWS.S3()
    .upload({
      Bucket: "chungchunonuploads",
      Key: objectName,
      ACL: "public-read",
      Body: readStream,
    })
    .promise();
  return Location;
};

Solution 1:

We need to resolve multiple file upload promises with Promise.all. Let us refactor our code and split it into 2 functions.

// Assume that we have list of all files to upload
const filesToUpload = [file1, file2, file3, fileN];

export const uploadSingleFileToS3 = async (file, userId, folderName) => {
  const { filename, createReadStream } = await file;
  const readStream = createReadStream();
  const objectName = `${folderName}/${userId}-${Date.now()}-${filename}`;
  const response = await new AWS.S3().upload({
    Bucket: "chungchunonuploads",
    Key: objectName,
    ACL: "public-read",
    Body: readStream,
  });

  return response;
};

const uploadMultipleFilesToS3 = async (filesToUpload, userId, folderName) => {
  const uploadPromises = filesToUpload.map((file) => {
    return uploadSingleFileToS3(file, userId, folderName);
  });

  // Array containing all  uploaded files data
  const uploadResult = await Promise.all(uploadPromises);

  // Add logic here to update the database with  Prisma ORM
};

// Call uploadMultipleFilesToS3 with all required parameters