Skip to content

Commit

Permalink
Fixed bucket from environment
Browse files Browse the repository at this point in the history
  • Loading branch information
willemliufdmg committed Feb 9, 2022
1 parent 189b26d commit e02c3c0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,12 @@ The following environment are supported:
- IMGIX_UPLOAD_ACC_S3_SECRET_KEY=<IMGIX S3 secret key> (required)
- IMGIX_UPLOAD_ACC_S3_BUCKET_URL=<IMGIX S3 Bucket URL> (required)
- IMGIX_UPLOAD_ACC_S3_BUCKET=<IMGIX S3 Bucket name> (required)
- IMGIX_UPLOAD_ACC_S3_DISTRIBUTION_ID=<IMGIX CloudFront distribution ID>
- IMGIX_UPLOAD_DEV_S3_API_KEY=<IMGIX S3 API key> (required)
- IMGIX_UPLOAD_DEV_S3_SECRET_KEY=<IMGIX S3 secret key> (required)
- IMGIX_UPLOAD_DEV_S3_BUCKET_URL=<IMGIX S3 Bucket URL> (required)
- IMGIX_UPLOAD_DEV_S3_BUCKET=<IMGIX S3 Bucket name> (required)
- IMGIX_UPLOAD_DEV_S3_DISTRIBUTION_ID=<IMGIX CloudFront distribution ID>
- IMGIX_UPLOAD_PROD_S3_API_KEY=<IMGIX S3 API key> (required)
- IMGIX_UPLOAD_PROD_S3_SECRET_KEY=<IMGIX S3 secret key> (required)
- IMGIX_UPLOAD_PROD_S3_BUCKET_URL=<IMGIX S3 Bucket URL> (required)
- IMGIX_UPLOAD_PROD_S3_BUCKET=<IMGIX S3 Bucket name> (required)
- IMGIX_UPLOAD_PROD_S3_DISTRIBUTION_ID=<IMGIX CloudFront distribution ID>
- IMGIX_UPLOAD_S3_DATA_CACHE_CONTROL=max-age=15768000
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fdmg/imgix-upload",
"version": "0.0.6",
"version": "0.0.7",
"description": "Command-line tool to upload images to the FDMG IMGIX S3 Bucket",
"main": "dist/app.js",
"preferGlobal": true,
Expand All @@ -20,7 +20,7 @@
"dogfood2": "dotenv fiu -- -v -s -i ./public/images",
"help": "node -r dotenv/config dist/app.js --help",
"start": "node -r dotenv/config dist/app.js -o ./imageMap.json -i ./images -s",
"start2": "node -r dotenv/config dist/app.js -o ./imageMap.json -i ./public/images -s",
"start2": "node -r dotenv/config dist/app.js -e development -o ./imageMap.json -i ./public/images -s",
"release:fdmg": "node modifyPackageJson.js -s @fdmg",
"release:fdmediagroep": "node modifyPackageJson.js -s @fdmediagroep",
"test": "echo \"Error: no test specified\" && exit 1",
Expand Down
39 changes: 25 additions & 14 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,27 @@ const argv: any = yargs(process.argv.slice(2)).options({
},
}).argv;

const Bucket = argv.bucket || process.env.IMGIX_UPLOAD_ACC_S3_BUCKET;
console.log(`S3 Bucket: ${Bucket}`);
function getBucket(environment: string) {
let result = "";
switch (environment) {
case "development":
result = process.env.IMGIX_UPLOAD_DEV_S3_BUCKET;
case "acceptance":
result = process.env.IMGIX_UPLOAD_ACC_S3_BUCKET;
case "production":
result = process.env.IMGIX_UPLOAD_DEV_S3_BUCKET;
}
return result;
}

const CacheControl =
argv.cacheControl || process.env.IMGIX_UPLOAD_S3_DATA_CACHE_CONTROL;
console.log(`Cache-control: ${CacheControl}`);
console.log(`Dry-run: ${!!argv.dryRun}`);
const environment = argv.environment || process.env.ENVIRONMENT;
console.log(`Environment: ${environment}`);
const Bucket = argv.bucket || getBucket(environment);
console.log(`S3 Bucket: ${Bucket}`);
let imagesFolder =
argv.imagesFolder || process.env.IMGIX_UPLOAD_RELATIVE_IMAGE_FOLDER_LOCATION;
imagesFolder =
Expand Down Expand Up @@ -114,20 +127,16 @@ const credentials = {
accessKeyId: process.env.IMGIX_UPLOAD_DEV_S3_API_KEY,
secretAccessKey: process.env.IMGIX_UPLOAD_DEV_S3_SECRET_KEY,
};
let distributionId = process.env.IMGIX_UPLOAD_DEV_S3_DISTRIBUTION_ID;
switch (environment) {
case "production":
distributionId = process.env.IMGIX_UPLOAD_PROD_S3_DISTRIBUTION_ID;
credentials.accessKeyId = process.env.IMGIX_UPLOAD_PROD_S3_API_KEY;
credentials.secretAccessKey = process.env.IMGIX_UPLOAD_PROD_S3_SECRET_KEY;
break;
case "acceptance":
distributionId = process.env.IMGIX_UPLOAD_ACC_S3_DISTRIBUTION_ID;
credentials.accessKeyId = process.env.IMGIX_UPLOAD_ACC_S3_API_KEY;
credentials.secretAccessKey = process.env.IMGIX_UPLOAD_ACC_S3_SECRET_KEY;
break;
case "development":
distributionId = process.env.IMGIX_UPLOAD_DEV_S3_DISTRIBUTION_ID;
credentials.accessKeyId = process.env.IMGIX_UPLOAD_DEV_S3_API_KEY;
credentials.secretAccessKey = process.env.IMGIX_UPLOAD_DEV_S3_SECRET_KEY;
break;
Expand All @@ -154,14 +163,16 @@ async function uploadToS3(absolutePath: string, hashedFileName: string) {
mime.contentType(absolutePath.replace(/\//g, "")) || undefined;
if (!argv.dryRun) {
try {
console.log({
Bucket,
CacheControl,
Key: hashedFileName,
ContentType,
ContentEncoding: "gzip",
ACL: "public-read",
});
if (debug) {
console.log({
Bucket,
CacheControl,
Key: hashedFileName,
ContentType,
ContentEncoding: "gzip",
ACL: "public-read",
});
}
// Put object to S3.
await s3
.upload({
Expand Down

0 comments on commit e02c3c0

Please sign in to comment.