Upload and download from AWS S3

Upload and download from 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.

1. Create an AWS bucket

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).

image

Save the Bucket name and your AWS region as Val Town secrets 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.

2. Create an IAM user

image

Choose a User name.

image

Select Attach policies directly.

image

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.)

image

On the final page, click Create user.

3. Create access keys for the user

Navigate to IAMUsers<the user you created>.

image

In the Security credentials tab, click Create access key.

image

In Step 1, select Application running outside AWS.

image

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

image

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

image

4. Upload text file

Copy and run the following val in your workspace.

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

5. Download the text file you just uploaded

Copy and run the following val in your workspace.

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

6. More resources!

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!