S3 helper functions in typescript
Tags: typescript, s3, aws, node
Published: 2022-02-10 17:43:18


![S3 + TS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fy4j1dbe9eqrfk31kbv6.png)

Following is a list of some of the most commonly used s3 functions ([aws-sdk](https://github.com/aws/aws-sdk-js)) and how you can use them using typescript.

* getObject
* upload
* deleteObject
* listObjectsV2
* headObject

---


> `getObject`: Retrieves objects from Amazon S3
```javascript
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;
  }
}
```

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 

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

```
---

> `upload`: Uploads objects to Amazon S3
```javascript
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;
  }
}
```

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 

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

```
---

> `deleteObject`: deletes an objects from Amazon S3
```javascript
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;
  }
}
```

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 

```javascript
await deleteFromS3({
  Bucket: "bucketName",
  Key: "objectKey",
});        
```
---

> `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.
```javascript
//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;
  }
}
```

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 

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

> `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.
```javascript
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;
  }
}
```

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 

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

```

You can see an example of how I use these wrapper functions [here](https://github.com/appletreeat56/json-to-xlsx/blob/master/src/utils/awsWrapper.ts).