Local file-based mock for AWS S3 for unit and local testing for AWS JavaScript SDK v3+.
Based on mock-aws-s3 and aws-sdk-mock.
import { createS3Client } from 'mock-aws-s3-v3';
import { PutObjectCommand } from '@aws-sdk/client-s3';
const client = createS3Client('./local-folder'); // of type S3Client
await client.send(new PutObjectCommand({
Bucket: 'test-bucket',
Key: 'test-key',
Body: 'hello',
}));
This will create:
- Folder
./local-folder/test-bucket
- File
./local-folder/test-key
with content 'hello'
The following operations are currently supported in this mock:
- CreateBucketCommand
- DeleteBucketCommand
- ListObjectsCommand
- ListObjectsV2Command
- DeleteObjectsCommand
- DeleteObjectCommand
- GetObjectCommand
- HeadObjectCommand
- PutObjectCommand
- CopyObjectCommand
- GetObjectTaggingCommand
- PutObjectTaggingCommand
Same as mock-aws-s3 minus getSignedUrl
. For an example of how getSignedUrl
could be mocked, see templateS3Bucket.ts.
The way aws-sdk-client-mock
is implemented will cause createS3Client
to mock ALL S3 clients globally. Thus the folder specified will be used for all calls happening after createS3Client
.
This will also apply to S3 clients that have not been mocked. To reset the mock and have S3Client
instances behave like real instances, use the method: resetMocks
import { resetMocks } from 'mock-aws-s3-v3';
resetMocks();