S3

The S3 service allows you to read, write, list and delete files from the cloud.

We recommend using our abstracted storage module, rather than integrating directly with S3. This will allow you to easily switch cloud storage provider in the future, without needing to change any code in your applications.You will still need to install the package and add the relevent configuration. Learn more about it here.

Installation

Follow these instructions to use the S3 module in your project.

Package

Install the package into your project:

pnpm install @juicyllama/app-aws

Api Backend

Import the module into your application, this is not needed if you are using the storage wrapper.

app.module.ts
import { AwsS3Module } from '@juicyllama/app-aws'

@Module({
    imports: [
        AwsS3Module,
    ],
})

Environment Variables

Regardless weither you use the S3 integration directly or via the storage module you will need to add the following:

  1. Ensure you have added the AWS config values as per these instructions
  2. Add these optional S3 specific environment variables:
Environment VariablesDescription
AWS_S3_JL_PUBLIC_BUCKETIf you would like to specify a bucket name for public resources
AWS_S3_JL_PRIVATE_BUCKETIf you would like to specify a bucket name for private resources
AWS_S3_JL_REGIONIf you would like to specify a region for your S3 buckets, note it will fallback to your global AWS_JL_REGION value if this is not provided

Services

Use the following operations to read, write, list and delete data to S3.

First setup the service:

app.service.ts
import { AwsS3Service, AwsS3Bucket, AwsS3Format } from '@juicyllama/app-aws'

@Injectable()
export class AppService {
    constructor(@Inject(AwsS3Service) private readonly awsS3Service: AwsS3Service) {}
}

Then you can call the following functions:

Write

Write files to the cloud

app.service.ts
async function writeSomething(file: Blob){
    const result = await this.awsS3Service.create({
        location: '/path/to/save/file/to/file.ext',
        permissions: AwsS3Bucket.PRIVATE, // or you can pass the bucket name as a string
        format: AwsS3Format.BLOB,
        file: file,
    })
}

Read

Read file from the cloud

app.service.ts
async function readSomething(){
    const result = await this.awsS3Service.findOne({
        location: '/path/to/read/file/from/file.ext',
        permissions: AwsS3Bucket.PRIVATE, // or you can pass the bucket name as a string
        format: AwsS3Format.BLOB,
    })
}

List

List files from the cloud

app.service.ts
async function listFiles(){
    const result = await this.awsS3Service.findAll({
        location: '/path/to/read/file/from',
        permissions: AwsS3Bucket.PRIVATE, // or you can pass the bucket name as a string
    })
}

Delete

Delete a file from the cloud

app.service.ts
async function readSomething(){
    const result = await this.awsS3Service.remove({
        location: '/path/to/delete/file/from/file.ext',
        permissions: AwsS3Bucket.PRIVATE, // or you can pass the bucket name as a string
    })
}

Docs v.0.14.0 Copyright © 2024