I had some issues getting the v3 AWS SDK for JavaScript to communicate with localstack S3, but I found a solution! With the V2 JS SDK, the configuration object for the S3 client looks like:
{
"region": "eu-west-1",
"endpoint": "http://localhost:4566",
"s3ForcePathStyle": true
}
The last field tells the sdk not to use <bucket>.hostname style connections, and instead puts the bucket in the path. For local dev this is important because otherwise the SDK tries to make connections to <bucket>.localhost. This'll work when managing buckets, but not when dealing with their contents (unless you add an entry to your /etc/hosts file, which often isn't practical).
Unfortunately this field doesn't exist for v3, and after searching I didn't find all that much except for a couple of mentions of the v3 JS SDK no longer supporting it.
Fortunately it's not actually the case. It was just renamed to forcePathStyle!
const { S3Client } = require('@aws-sdk/client-s3');
const s3 = new S3Client({
region: 'eu-west-1', // The value here doesn't matter.
endpoint: 'http://localhost:4566', // This is the localstack EDGE_PORT
forcePathStyle: true
});
For reference, I boot localstack with this docker-compose configuration:
version: "3.9"
services:
mock-s3:
image: localstack/localstack:latest
ports:
- "4566-4583:4566-4583"
environment:
- AWS_DEFAULT_REGION=eu-west-1
- EDGE_PORT=4566
- SERVICES=s3
- DEBUG=1
- DATA_DIR=/tmp/localstack/data
volumes:
- "./.localstack:/tmp/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
from https://qubyte.codes/blog/tip-connecting-to-localstack-s3-using-the-javascript-aws-sdk-v3
'AWS > Storage and Databases' 카테고리의 다른 글
[AWS S3] @aws-sdk/s3-request-presigner (0) | 2022.05.07 |
---|---|
[Storage and Databases] AWS Additional database services (0) | 2022.05.04 |
[Storage and Databases] AWS Database Migration Service (0) | 2022.05.04 |
[Storage and Databases] Amazon Redshift (0) | 2022.05.04 |
[Storage and Databases] Amazon DynamoDB (0) | 2022.05.04 |