Save text file from S3 using aws-sdk v3

Published on

Photo by Ilya Pavlov

AWS SDK v3 now stable and recommended for general use. AWS SDK for JavaScript(V2) is deprecated. This article is serries of tips and tricks to use aws-sdk v3. The first article is about how to save text file from S3 using aws-sdk v3.

For getting a object from S3, you can simply use the GetObjectCommand method.

import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3'
const client = new S3Client(config)
const command = new GetObjectCommand(input)
const response = await client.send(command)

The response is a GetObjectOutput object.

  Body: ReadableStream, // The body of the object.
  ContentLength: number, // The length of the object in bytes.
  ContentType: string, // The type of object. This is the same as the Content-Type response header.
  ETag: string, // The ETag of the object. This is the same as the ETag response header.
  LastModified: Date, // The date and time the object was last modified. This is the same as the Last-Modified response header.
  Metadata: {
    [key: string]: string // A map of metadata to store with the object.
  RequestCharged: string,
  ServerSideEncryption: string,
  SSECustomerAlgorithm: string,
  SSECustomerKeyMD5: string,
  SSEKMSKeyId: string, // If present, specifies the ID of the AWS Key Management Service (KMS) master encryption key that was used for the object.
  VersionId: string // The version of the object.

You can read the body of the object using the Body property. Because the Body property is a readable stream, you can pipe it to another readable stream. For example, you can pipe the Body to a file.

// Pipe the body to a file
const file = fs.createWriteStream(path)

// or Pipe the body to http response

or you can convert can convert readable stream to string by using my utility function streamToString:

const streamToString = (stream) =>
  new Promise((resolve, reject) => {
    const chunks = []
    stream.on('data', (chunk) => chunks.push(chunk))
    stream.on('error', reject)
    stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))

It is easy, right? You can download full example of this article in this gist.