Files uploaded through s3 multipart upload are not working

118 Views Asked by At

I am trying to upload large files to s3 using multipart upload. I am using the aws-sdk v3 for intializing and completing the multipart upload and I am generating presigned urls for every part that can be used on the frontend to upload a specific part. Everything seems to be working fine as I am not getting any errors while uploading or while completing the multipart upload. However, when I am downloading the uploaded file and opening it, it is not opening and says that the file might be corrupted.

error message while opening the file

This is the code that I am using

export const InitializeMultipartUpload = async ({
  client,
  bucketName,
  key,
}) => {
  if (!bucketName || !key || !client) {
    throw new Error("Bucket name / key / client are required.");
  }

  try {
    const data = {
      Bucket: bucketName,
      Key: key,
      ACL: "public-read",
    };

    logger(
      "info",
      `Initializing multipart upload. Config: ${JSON.stringify(data)}`,
    );
    const command = new CreateMultipartUploadCommand(data);
    const res = await client.send(command);

    logger("info", JSON.stringify(res));
    return res.UploadId;
  } catch (error) {
    console.log(error);
    logger("error", `Error initializing multipart upload: ${error.message}`);
    return null;
  }
};


export const GetPresignedUrl = async ({
  client,
  bucketName,
  key,
  partNumber,
  uploadId,
}) => {
  if (!bucketName || !key || !client) {
    throw new Error("Bucket name / key / client are required.");
  }

  try {
    const data = {
      Bucket: bucketName,
      Key: key,
      ...(partNumber ? { PartNumber: partNumber } : {}),
      ...(uploadId ? { UploadId: uploadId } : {}),
    };

    logger("info", `Getting presigned url. Config: ${JSON.stringify(data)}`);
    const command = new UploadPartCommand(data);
    const signedUrl = await getSignedUrl(client, command, { expiresIn: 3600 });

    return signedUrl;
  } catch (error) {
    console.log(error);
    logger("error", `Error getting presigned url: ${error.message}`);
    return null;
  }
};


export const CompleteMultipartUpload = async ({
  client,
  bucketName,
  key,
  uploadId,
  parts,
}) => {
  if (!bucketName || !key || !client) {
    throw new Error("Bucket name / key / client are required.");
  }

  try {
    const data = {
      Bucket: bucketName,
      Key: key,
      UploadId: uploadId,
      MultipartUpload: {
        Parts: orderBy(parts, ["PartNumber"], ["asc"]),
      },
    };

    logger(
      "info",
      `Completing multipart upload. Config: ${JSON.stringify(data)}`,
    );
    const command = new CompleteMultipartUploadCommand(data);
    const res = await client.send(command);

    logger("info", JSON.stringify(res));
    return res;
  } catch (error) {
    console.log(error);
    logger("error", `Error Completing multipart upload: ${error.message}`);
    return null;
  }
};
0

There are 0 best solutions below