Skip to main content

AWS Node.js SDK Example

For AWS SDK v3, make sure you set all checksum calculation and validation options to WHEN_REQUIRED.
The following example shows how the AWS Node.js SDK can be used to interact with Telnyx Cloud Storage.
const { S3Client, CreateBucketCommand, PutObjectCommand, ListObjectsCommand, GetObjectCommand } = require("@aws-sdk/client-s3");
const axios = require("axios");
const { v4: uuidv4 } = require("uuid");

const telnyxApiKey = process.env.TELNYX_API_KEY;

if (!telnyxApiKey) {
  console.error("TELNYX_API_KEY environment variable not set");
  process.exit(1);
}

const endpointUrl = "https://us-central-1.telnyxcloudstorage.com";

// 1. Initialize the AWS S3 client with specific options
const s3Client = new S3Client({
  endpoint: endpointUrl,
  region: "us-central-1",
  credentials: {
    accessKeyId: telnyxApiKey,
    secretAccessKey: telnyxApiKey
  },
  forcePathStyle: true,
  requestChecksumCalculation: 'WHEN_REQUIRED',
  requestChecksumValidation: 'WHEN_REQUIRED',
  responseChecksumCalculation: 'WHEN_REQUIRED',
  responseChecksumValidation: 'WHEN_REQUIRED'
});

(async () => {
  // 2. Create a bucket
  const bucketName = `my-test-bucket-${uuidv4()}`;
  await s3Client.send(new CreateBucketCommand({ Bucket: bucketName }));

  // 3. Upload two objects with random data
  for (let i = 0; i < 2; i++) {
    const name = `my-test-object-${i}`;
    const body = `Telnyx Cloud Storage ${i}`;
    await s3Client.send(new PutObjectCommand({ Bucket: bucketName, Key: name, Body: body }));
  }

  // 4. List objects in the bucket
  const listResult = await s3Client.send(new ListObjectsCommand({ Bucket: bucketName }));
  (listResult.Contents || []).forEach(obj => {
    console.log(obj.Key);
  });

  // 5. Download the first object
  const getResult = await s3Client.send(new GetObjectCommand({ Bucket: bucketName, Key: "my-test-object-0" }));
  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("utf-8")));
  });
  console.log(await streamToString(getResult.Body));

  // 6. Create a presigned URL for the first file
  const presignResponse = await axios.post(
    `https://api.telnyx.com/v2/storage/buckets/${bucketName}/my-test-object-0/presigned_url`,
    { TTL: 30 },
    { headers: { Authorization: `Bearer ${telnyxApiKey}` } }
  );
  console.log(presignResponse.data);

  // 7. Download the file using the presigned URL
  const fileResponse = await axios.get(presignResponse.data.data.presigned_url);
  console.log(fileResponse.data);
})();