I’m trying to host a static site in S3 using AWS. The problem is that for it to work with a custom domain, the bucket needs to be named like the domain. Domain names contains dots and for some reason they are not supported out of the box in the SDK. I was trying to something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var fs = require('fs');
var aws = require('aws-sdk');
var bucket = 'my.domain.com';

var bucketParams = {
  params: {
    Bucket: bucket
  }
}
var bucket = new aws.S3(bucketParams);

var uploadData = {
  ACL: 'public-read',
  CacheControl: 'max-age=31556926',
  Key: 'somefile.txt',
  ContentType: 'text/plain'
};
uploadData.Body = fs.createReadStream('somefile.txt');
bucket.upload(uploadData).send();

And it was simply not working. Similar code has worked previously with a bucket name that didn’t contain dots, so I did some research and found the solution. For some reason AWS doesn’t allow by default bucket names with dots. To walk around the issue, you have to add s3ForcePathStyle and region when creating the S3 object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var fs = require('fs');
var aws = require('aws-sdk');
var bucket = 'my.domain.com';

var bucketParams = {
  params: {
    Bucket: bucket
  },
  s3ForcePathStyle: true,
  region: 'eu-west-1'
}
var bucket = new aws.S3(bucketParams);

var uploadData = {
  ACL: 'public-read',
  CacheControl: 'max-age=31556926',
  Key: 'somefile.txt',
  ContentType: 'text/plain'
};
uploadData.Body = fs.createReadStream('somefile.txt');
bucket.upload(uploadData).send();

And that fixes the problem.

[ aws  javascript  node  programming  ]
Sorting algorithms javascript programming
The Rabin-Karp algorithm javascript programming
House painting problem javascript programming
Bitwise operations in Javascript javascript programming
Add one to a number without using plus or minus sign javascript programming