DEV Community

metacollective
metacollective

Posted on

S3 helper functions in typescript

S3 + TS

Following is a list of some of the most commonly used s3 functions (aws-sdk) and how you can use them using typescript.

  • getObject
  • upload
  • deleteObject
  • listObjectsV2
  • headObject

getObject: Retrieves objects from Amazon S3

export async function downloadFromS3(
  params: S3.GetObjectRequest
): Promise<any> {
  console.info("---- DOWNLOADING FROM S3", JSON.stringify(params, null, 2));
  try {
    return await s3.getObject(params).promise();
  } catch (error) {
    console.log(error);
    throw error;
  }
}
Enter fullscreen mode Exit fullscreen mode

This function expects a parameter of GetObjectRequest type and its definition can be found here - https://github.com/aws/aws-sdk-js/blob/master/clients/s3.d.ts

You can call this function like this

await downloadFromS3({
  Bucket: "bucketName",
  Key: "objectKey",
});

Enter fullscreen mode Exit fullscreen mode

upload: Uploads objects to Amazon S3

export async function uploadtoS3(s3Data: S3.PutObjectRequest) {
  console.info(
    "---- UPLODAING TO S3",
    JSON.stringify(`${s3Data.Bucket} ${s3Data.Key}`, null, 2)
  );

  try {
    return await s3.upload(s3Data).promise();
  } catch (error) {
    console.log(error);
    return error;
  }
}
Enter fullscreen mode Exit fullscreen mode

This function expects a parameter of PutObjectRequest type and its definition can be found here - https://github.com/aws/aws-sdk-js/blob/master/clients/s3.d.ts

You can call this function like this

await uploadtoS3({
  Bucket: "bucketName",
  Key: "objectKey",
  ACL: "public-read",
  CacheControl: "max-age=86400",
  Body: JSON.stringify(dataObject),
  ContentType: "application/json",
});

Enter fullscreen mode Exit fullscreen mode

deleteObject: deletes an objects from Amazon S3

export async function deleteFromS3(
  params: S3.DeleteObjectRequest
): Promise<any> {
  console.info("---- DELETE FROM S3", JSON.stringify(params, null, 2));
  try {
    return await s3.deleteObject(params).promise();
  } catch (error) {
    console.log(error);
    throw error;
  }
}
Enter fullscreen mode Exit fullscreen mode

This function expects a parameter of DeleteObjectRequest type and its definition can be found here - https://github.com/aws/aws-sdk-js/blob/master/clients/s3.d.ts

You can call this function like this

await deleteFromS3({
  Bucket: "bucketName",
  Key: "objectKey",
});        
Enter fullscreen mode Exit fullscreen mode

listObjectsV2: Returns some or all (up to 1,000) of the objects in a bucket with each request. You can use the request parameters as selection criteria to return a subset of the objects in a bucket.

//only returns a max of 1000 keys hence we must check for more. Look for IsTruncated value and then recurse.

export async function listFilesFromS3(s3Request: S3.ListObjectsV2Request, allKeys: Array<string>) {
  console.info("---- LISTING S3 BUCKET", JSON.stringify(s3Request, null, 2));

  try {
    const data:S3.ListObjectsV2Output = await s3.listObjectsV2(s3Request).promise();
    let contents = data.Contents;
    contents.forEach(function (content) {
      allKeys.push(content.Key);
    });
    if (data.IsTruncated) {
      s3Request.ContinuationToken = data.NextContinuationToken;
      console.log("get further list...");
      return listFilesFromS3(s3Request, allKeys);
    } else {
      return allKeys;
    }
  } catch (error) {
    console.log(error);
    return error;
  }
}
Enter fullscreen mode Exit fullscreen mode

This function expects a parameter of ListObjectsV2Request type and its definition can be found here - https://github.com/aws/aws-sdk-js/blob/master/clients/s3.d.ts

You can call this function like this

await listFilesFromS3(
  {
    Bucket: "bucketName",
  },
  []
);

Enter fullscreen mode Exit fullscreen mode

headObject: The HEAD action retrieves metadata from an object without returning the object itself. This action is useful if you're only interested in an object's metadata. To use HEAD, you must have READ access to the object.

export async function getHeadObect(s3Data: S3.HeadObjectRequest) {
  console.info(
    "---- GET HEAD OBJECT",
    JSON.stringify(`${s3Data.Bucket} ${s3Data.Key}`, null, 2)
  );

  try {
    return await s3.headObject(s3Data).promise();
  } catch (error) {
    console.log(error);
    return error;
  }
}
Enter fullscreen mode Exit fullscreen mode

This function expects a parameter of HeadObjectRequest type and its definition can be found here - https://github.com/aws/aws-sdk-js/blob/master/clients/s3.d.ts

An example of how you can call this function can be like this

await listFilesFromS3(
  {
    Bucket: "bucketName",
  },
  []
);

Enter fullscreen mode Exit fullscreen mode

You can see an example of how I use these wrapper functions here.

Discussion (0)