PDF not uploading to Amazon S3 with Nodejs and Reactjs

Viewed 411

I am working on uploading PDF files to S3 using nodejs and react and I am running into an issue where some PDFs are being uploaded and some are not.

This endpoint gets a signed url from AWS

  '/api/v1/upload/pdf',
  requireAuth,
  roleAuthorization(['admin']),
  (req, res) => {
    const date = new Date();
    const year = date.getFullYear();
    const key = `${date.toLocaleString('default', {
      month: 'long',
    })}-${short.uuid('0123456789').slice(0, 2)}-${year}.pdf`;
    s3.getSignedUrl(
      'putObject',
      {
        Bucket: 'bucket-name',
        ContentType: 'application/pdf',
        Key: key,
      },
      (err, url) => res.send({ key, url })
    );
  }
);

And this endpoint does the upload from react js and save the link to the file in the database

const createIssue = (dispatch) => async (issue, town) => {
  try {
    const token = await localStorage.getItem('token');
    const uploadConfig = await axios.get('/api/v1/upload/pdf', {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    });

    const upload = await axios.put(uploadConfig.data.url, issue, {
      headers: {
        'Content-Type': issue.type,
      },
    });

    const response = await api.post(`/api/v1/town/${town._id}/issue`, {
      issue: uploadConfig.data.key,
    });
    dispatch({ type: 'create_issue', payload: response.data });
  } catch (err) {
    dispatch({
      type: 'add_error',
      payload: err.message,
    });
  }
};

This works but does not work for all PDF files, the file remains pending and does not upload.

Any help welcome. thanks

0 Answers
Related