aaron
aaron

Reputation: 66

How to check if a pdf can be "loaded" using node js?

I have pdfs in my amazon s3 bucket. while testing my app, i had an issue and some of the pdfs within s3 "fail to load" after downloading.

Instead of deleting and reloading all my pdfs is there a way to check if they can be loaded?

const s3 = new AWS.S3({
    accessKeyId: AWSCredentials.accessKey,
    secretAccessKey: AWSCredentials.secret
});

const downloadFile = (bucketName, key) => {
    const params = {
      Bucket: bucketName,
      Key: key
    };
    s3.getObject(params, (err, data) => {
        console.log(data.Body.toString('utf-8'))

    });
  };


 downloadFile('myBucket', 'mypdf.pdf');

I still "see" data here.

i also tried (data.Body.length); but this file has length, it just wont load as a pdf.

suggestions?

Upvotes: 1

Views: 136

Answers (1)

aaron
aaron

Reputation: 66

I found a way and some of your comments lead me to this solution. @KJ thank you.

const s3 = new AWS.S3({
    accessKeyId: AWSCredentials.accessKey,
    secretAccessKey: AWSCredentials.secret
});

const downloadFile = (bucketName, key) => {
    const params = {
      Bucket: bucketName,
      Key: key
    };
    s3.getObject(params, (err, data) => {
        
 let s3Text = data.Body.toString()

if(s3Text[s3Text.length -2] == "F"){
    console.log("Its a good file")
}else{
    console.log("Its a bad file")
}
    });
  };

downloadFile('someBucket', 'failToLoad.pdf');// Its a bad file
downloadFile('someBucket', 'successfulLoad.pdf');// Its a good file

Upvotes: 1

Related Questions