Download S3 object in Node with typescript

Asked

Viewed 20 times

0

I am developing an XML signing API with digital certificates in P12/pfx format. These certificates are stored in a S3 Bucket. The API is being built using Node and Typescript. My problem is the certificate download process.

Currently I have this function that is performing the Certificate download and saving in a local file:

async getCertificate() {
const params = {
  Bucket: this.bucket,
  Key: this.certificateKey
}
const s3 = new AWS.S3();
const writeFile = util.promisify(fs.writeFile);

s3.getObject(params)
  .promise()
  .then(data => {
    
    if (data.Body) {
      
      writeFile(this.downloadedCert, data.Body);
    }
  }).catch(err => {
    throw err;
  });

However, the second argument of the writeFile function must be an arraybuffer or a string and the date variable. Body does not meet this criterion and tslint points:

const file: AWS.S3.Body Argument of type 'Body' is not Assignable to Parameter of type 'string | Arraybufferview'. Type 'Readable' is not Assignable to type 'string | Arraybufferview'. Type 'Readable' is Missing the following properties from type 'Dataview': buffer, byteLength, byteOffset, getFloat32, and 20 more.ts(2345)

If I convert data.Body to a string without encoding, the certificate is invalid. If I convert to string with encoding 'Base64', it is valid (I can even install on the machine), but from the error in the function that performs the signature (using lib pem or xm-Crypto).

I found a workaround which is to make typescript ignore the next line, with / @ts-ignore /. But I would like a more elegant and, above all, correct way to resolve this issue. When indicating typescript to ignore this line, I save the data.Body variable directly and everything flows normally.

Obs. A little context: We already have a service running with dotnet (which we acquired from another employee), however, by a technical decision, we decided to bring to the stack that we use in our other projects, to facilitate maintenance and improvement by the current devs(I also don’t know much about c#).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.