Edit on val.town ↗

Store data in AWS S3

You can upload and download from AWS S3 inside val functions.

In this guide, you'll create an AWS bucket, an IAM user, and then test your set up is correct by uploading and downloading a small text file.

Log in to the AWS Console and go to https://s3.console.aws.amazon.com/s3/bucket/create

Create a new bucket by choosing a Bucket name (leave the defaults for everything else).

screenshot_2023-06-28_at_134358.png

Save the Bucket name and your AWS region as Val Town environment variables as awsS3Bucket and awsS3Region respectively. Although these values aren't secrets, doing this means you can copy and paste the val examples in this guide without making any code changes.

Continue to use this AWS region for the rest of this guide.

Go to https://console.aws.amazon.com/iamv2/home#/users and click Add users.

screenshot_2023-06-28_at_134812.png

Choose a User name.

screenshot_2023-06-28_at_135557.png

Select Attach policies directly.

screenshot_2023-06-28_at_135610.png

Give this user AmazonS3FullAccess by searching and selecting the checkbox under Permissions policies.

(This allows this user the highest level of S3 permissions across all of the buckets for the AWS account you're logged into. Consider setting up granular permissions with AWS's Bucket owner granting its users bucket permissions guide.)

screenshot_2023-06-28_at_135625.png

On the final page, click Create user.

Navigate to IAM → Users → <the user you created>.

screenshot_2023-06-28_at_135706.png

In the Security credentials tab, click Create access key.

screenshot_2023-06-28_at_135719.png

In Step 1, select Application running outside AWS.

screenshot_2023-06-28_at_155517.png

In Step 2, give your access keys a helpful Description tag value.

screenshot_2023-06-28_at_155555.png

Finally, copy and save the Access key and Secret access key as Val Town environment variables as awsS3Key and awsS3Secret respectively.

screenshot_2023-06-28_at_135757.png

Copy and run the following val in your workspace.

import { S3Client } from "https://deno.land/x/s3_lite_client@0.6.1/mod.ts";

const s3client = new S3Client({
  endPoint: `s3.${Deno.env.get("awsS3Region")}.amazonaws.com`,
  region: Deno.env.get("awsS3Region"),
  bucket: Deno.env.get("awsS3Bucket"),
  accessKey: Deno.env.get("awsS3Key"),
  secretKey: Deno.env.get("awsS3Secret"),
});
await s3client.putObject("filename.txt", "File contents");

putObject takes any of ReadableStream<Uint8Array> | Uint8Array | string.

Copy and run the following val in your workspace.

import { S3Client } from "https://deno.land/x/s3_lite_client@0.6.1/mod.ts";

const s3client = new S3Client({
  endPoint: `s3.${Deno.env.get("awsS3Region")}.amazonaws.com`,
  region: Deno.env.get("awsS3Region"),
  bucket: Deno.env.get("awsS3Bucket"),
  accessKey: Deno.env.get("awsS3Key"),
  secretKey: Deno.env.get("awsS3Secret"),
});
const res = await s3client.getObject("filename.txt");
console.log(await res.text());

getObject returns a standard HTTP Response object which you consumed with .text(), .json(), .body (ReadableStream), .arrayBuffer(), or .blob().

Documentation for the lite S3 client used in this guide can be found here: https://deno.land/x/s3_lite_client@0.6.1. There's also S3's documentation.

For all other Val Town help, see our Discord!

Last updated