Testing AWS Lambda S3 API commands and Notification Events locally.
This is not a full local implementation of AWS S3.
However the local S3 server* supports following requests:
AWS-SDK
- CopyObject
- CreateBucket
- DeleteBucket
- DeleteBucketTagging
- DeleteObject
- DeleteObjects
- DeleteObjectTagging
- GetBucketTagging
- GetObject
- GetObjectTagging
- HeadBucket
- HeadObject
- ListBuckets
- ListObjects
- ListObjectsV2
- PutBucketTagging
- PutObject
- PutObjectTagging
AWS-CLI s3
- cp
- ls
- mb
- mv
- rb
- rm
- sync
AWS-CLI s3api
- copy-object
- create-bucket
- delete-bucket
- delete-bucket-tagging
- delete-object
- delete-objects
- delete-object-tagging
- get-bucket-tagging
- get-object
- get-object-tagging
- head-bucket
- head-object
- list-buckets
- list-objects
- list-objects-v2
- put-bucket-tagging
- put-object
- put-object-tagging
supported options:
- prefix
- delimiter
- marker
- start after
- range
- max keys
- continuation token
- metadata
- metadata directive
- content type
- content disposition
- content encoding
- content language
- cache control
- expires
- website redirect location
- storage class
- if match
- if none match
- if modified since
- if unmodified since
* http://127.0.0.1:PORT/@s3 or http://0.0.0.0:PORT/@s3
Import the plugin inside your defineConfig.
// config.js
import { defineConfig } from "serverless-aws-lambda/defineConfig";
import { s3Plugin } from "serverless-aws-lambda/s3";
export default = defineConfig({
plugins: [s3Plugin()],
});
options:
localStorageDir
: path to store s3 files locally (default: 'localS3/')persist
: indicates if localStorageDir shoud be persistent (default: true)
Subscribe to a S3 event with serverless declaration.
# serverless.yml
functions:
myAwsomeLambda:
handler: src/myAwsomeLambda.default
events:
- s3: myBucket
// src/myAwsomeLambda.js
export default async (s3Event) => {
console.log(s3Event);
};
- s3: myBucket
- s3:
bucket: myBucket
- s3:
bucket: myBucket
event: s3:ObjectCreated:*
- s3:
bucket: myBucket
event: s3:ObjectCreated:Put
rules:
- prefix: "images/"
- suffix: ".jpg"
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
const client = new S3Client({
region: "eu-west-3",
endpoint: `http://0.0.0.0:9999/@s3`,
credentials: {
// not required if you already have aws config on your OS
accessKeyId: "fake id",
secretAccessKey: "fake secret",
},
});
const Bucket = "myLocalBucket";
const Key = "some/file.json";
const putResponse = await client.send(
new PutObjectCommand({
Bucket,
Key,
Body: JSON.stringify({ hello: "world" }),
})
);
console.log(putResponse);
aws --endpoint-url http://0.0.0.0:9999/@s3 s3 ls
aws --endpoint-url http://0.0.0.0:9999/@s3 s3api list-buckets