Commit eedac607 authored by Robert Knight's avatar Robert Knight

Migrate S3 deployment script to AWS SDK for JavaScript v3

This was mostly straightforward except that the `cdn.hypothes.is` bucket resides
in us-east-1, which has a quirk with regards to determining the bucket region.

Fixes https://github.com/hypothesis/client/issues/5544
parent 16be1856
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
"bugs": "https://github.com/hypothesis/client/issues", "bugs": "https://github.com/hypothesis/client/issues",
"repository": "hypothesis/client", "repository": "hypothesis/client",
"devDependencies": { "devDependencies": {
"@aws-sdk/client-s3": "^3.504.0",
"@babel/core": "^7.1.6", "@babel/core": "^7.1.6",
"@babel/preset-env": "^7.1.6", "@babel/preset-env": "^7.1.6",
"@babel/preset-react": "^7.0.0", "@babel/preset-react": "^7.0.0",
...@@ -38,7 +39,6 @@ ...@@ -38,7 +39,6 @@
"@typescript-eslint/parser": "^6.0.0", "@typescript-eslint/parser": "^6.0.0",
"approx-string-match": "^2.0.0", "approx-string-match": "^2.0.0",
"autoprefixer": "^10.0.1", "autoprefixer": "^10.0.1",
"aws-sdk": "^2.345.0",
"axe-core": "^4.0.0", "axe-core": "^4.0.0",
"babel-plugin-inject-args": "^1.0.0", "babel-plugin-inject-args": "^1.0.0",
"babel-plugin-istanbul": "^6.0.0", "babel-plugin-istanbul": "^6.0.0",
......
...@@ -8,7 +8,11 @@ const { extname } = require('path'); ...@@ -8,7 +8,11 @@ const { extname } = require('path');
const { program } = require('commander'); const { program } = require('commander');
const Arborist = require('@npmcli/arborist'); const Arborist = require('@npmcli/arborist');
const packlist = require('npm-packlist'); const packlist = require('npm-packlist');
const AWS = require('aws-sdk'); const {
S3Client,
PutObjectCommand,
GetBucketLocationCommand,
} = require('@aws-sdk/client-s3');
/** /**
* File extension / mime type associations for file types we actually use. * File extension / mime type associations for file types we actually use.
...@@ -52,7 +56,9 @@ function contentTypeFromFilename(path) { ...@@ -52,7 +56,9 @@ function contentTypeFromFilename(path) {
class S3Uploader { class S3Uploader {
constructor(bucket) { constructor(bucket) {
this.s3 = new AWS.S3(); // Set an initial region that we'll use for the call to get the bucket's
// location. We'll create a new client for the bucket's region later.
this.s3 = new S3Client({ region: 'us-west-1' });
this.bucket = bucket; this.bucket = bucket;
this.region = null; this.region = null;
} }
...@@ -60,13 +66,15 @@ class S3Uploader { ...@@ -60,13 +66,15 @@ class S3Uploader {
async upload(destPath, srcFile, { cacheControl }) { async upload(destPath, srcFile, { cacheControl }) {
if (!this.region) { if (!this.region) {
// Find out where the S3 bucket is. // Find out where the S3 bucket is.
const regionResult = await this.s3 const command = new GetBucketLocationCommand({
.getBucketLocation({
Bucket: this.bucket, Bucket: this.bucket,
}) });
.promise(); const regionResult = await this.s3.send(command);
this.region = regionResult.LocationConstraint;
this.s3 = new AWS.S3({ region: this.region }); // LocationConstraint is omitted for us-east-1.
// See https://github.com/aws/aws-sdk-js-v3/pull/844
this.region = regionResult.LocationConstraint ?? 'us-east-1';
this.s3 = new S3Client({ region: this.region });
} }
const fileContent = fs.readFileSync(srcFile); const fileContent = fs.readFileSync(srcFile);
...@@ -78,7 +86,8 @@ class S3Uploader { ...@@ -78,7 +86,8 @@ class S3Uploader {
CacheControl: cacheControl, CacheControl: cacheControl,
ContentType: contentTypeFromFilename(srcFile), ContentType: contentTypeFromFilename(srcFile),
}; };
return this.s3.putObject(params).promise(); const putObjectCommand = new PutObjectCommand(params);
return this.s3.send(putObjectCommand);
} }
} }
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment