From 110a0331fa5a2ec226c78253c91c82617521d2b0 Mon Sep 17 00:00:00 2001 From: Shobhit Singhal Date: Mon, 2 Aug 2021 08:42:29 +0530 Subject: [PATCH] initial commit --- .editorconfig | 16 ++++++++ .gitignore | 96 ++++++++++++++++++++++++++++++++++++++++++++ .npmignore | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 43 ++++++++++++++++++++ lib/index.js | 72 +++++++++++++++++++++++++++++++++ package.json | 32 +++++++++++++++ 6 files changed, 368 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 README.md create mode 100644 lib/index.js create mode 100644 package.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..473e451 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[{package.json,*.yml}] +indent_style = space +indent_size = 2 + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..28ca81d --- /dev/null +++ b/.gitignore @@ -0,0 +1,96 @@ +############################ +# OS X +############################ + +.DS_Store +.AppleDouble +.LSOverride +Icon +.Spotlight-V100 +.Trashes +._* + + +############################ +# Linux +############################ + +*~ + + +############################ +# Windows +############################ + +Thumbs.db +ehthumbs.db +Desktop.ini +$RECYCLE.BIN/ +*.cab +*.msi +*.msm +*.msp + + +############################ +# Packages +############################ + +*.7z +*.csv +*.dat +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip +*.com +*.class +*.dll +*.exe +*.o +*.seed +*.so +*.swo +*.swp +*.swn +*.swm +*.out +*.pid + + +############################ +# Logs and databases +############################ + +.tmp +*.log +*.sql +*.sqlite + + +############################ +# Misc. +############################ + +*# +.idea +nbproject + + +############################ +# Node.js +############################ + +lib-cov +lcov.info +pids +logs +results +build +node_modules +.node_history +package-lock.json + diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..eddfe19 --- /dev/null +++ b/.npmignore @@ -0,0 +1,109 @@ +############################ +# OS X +############################ + +.DS_Store +.AppleDouble +.LSOverride +Icon +.Spotlight-V100 +.Trashes +._* + + +############################ +# Linux +############################ + +*~ + + +############################ +# Windows +############################ + +Thumbs.db +ehthumbs.db +Desktop.ini +$RECYCLE.BIN/ +*.cab +*.msi +*.msm +*.msp + + +############################ +# Packages +############################ + +*.7z +*.csv +*.dat +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip +*.com +*.class +*.dll +*.exe +*.o +*.seed +*.so +*.swo +*.swp +*.swn +*.swm +*.out +*.pid + + +############################ +# Logs and databases +############################ + +.tmp +*.log +*.sql +*.sqlite + + +############################ +# Misc. +############################ + +*# +ssl +.editorconfig +.gitattributes +.idea +nbproject + + +############################ +# Node.js +############################ + +lib-cov +lcov.info +pids +logs +results +build +node_modules +.node_history +.snyk + + + +############################ +# Tests +############################ + +test +tests +__tests__ +jest.config.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..2dc2a5c --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +Small modification to [strapi-provider-upload-aws-s3](https://www.npmjs.com/package/strapi-provider-upload-aws-s3) to support an optional baseUrl configuration. +If `baseUrl` is not specified, it works the same as the official aws-s3 plugin. + +Original source is [here](https://github.com/strapi/strapi/tree/master/packages/strapi-provider-upload-aws-s3). + +package.json: + +``` +{ + //... + "dependencies": { + //... + "strapi-provider-upload-aws-s3": "npm:@chakrahq/strapi-provider-upload-aws-s3@3.6.5", + //... + }, + // ... +} +``` + +config/plugins.js: + +``` +module.exports = ({ env }) => { + //... + upload: { + provider: 'aws-s3', + providerOptions: { + accessKeyId: env('AWS_S3_KEY'), + secretAccessKey: env('AWS_S3_SECRET'), + region: env('AWS_S3_REGION'), + params: { + Bucket: env('AWS_S3_BUCKET'), + }, + baseUrl: env('AWS_S3_CDN') + }, + }, + //... +}; +``` + +Note: +`baseUrl` param should be a full URL without a trailing slash. +Example: `https://cdn.example.com` diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..2e9df4b --- /dev/null +++ b/lib/index.js @@ -0,0 +1,72 @@ +'use strict' + +/** + * References: + * - https://strapi.io/documentation/developer-docs/latest/development/plugins/upload.html#create-providers + * - https://github.com/strapi/strapi/tree/master/packages/strapi-provider-upload-aws-s3 + */ + +/** + * Module dependencies + */ + +const AWS = require('aws-sdk'); + +module.exports = { + init(config) { + const S3 = new AWS.S3({ + apiVersion: '2006-03-01', + ...config, + }); + + return { + upload(file, customParams = {}) { + return new Promise((resolve, reject) => { + // upload file on S3 bucket + const path = file.path ? `${file.path}/` : ''; + S3.upload( + { + Key: `${path}${file.hash}${file.ext}`, + Body: Buffer.from(file.buffer, 'binary'), + ACL: 'public-read', + ContentType: file.mime, + ...customParams, + }, + (err, data) => { + if (err) { + return reject(err); + } + + // set the bucket file url + if (config.baseUrl) { + file.url = `${config.baseUrl}/${data.Key}`; + } else { + file.url = data.Location; + } + + resolve(); + } + ); + }); + }, + delete(file, customParams = {}) { + return new Promise((resolve, reject) => { + // delete file on S3 bucket + const path = file.path ? `${file.path}/` : ''; + S3.deleteObject( + { + Key: `${path}${file.hash}${file.ext}`, + ...customParams, + }, + (err) => { + if (err) { + return reject(err); + } + resolve(); + } + ); + }); + }, + }; + }, +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..4adf2f2 --- /dev/null +++ b/package.json @@ -0,0 +1,32 @@ +{ + "name": "@chakrahq/strapi-provider-upload-aws-s3", + "version": "0.1.0", + "description": "AWS S3 provider for strapi upload, with additional optional features over the official AWS S3 provider", + "keywords": [ + "upload", + "aws", + "s3", + "strapi" + ], + "directories": { + "lib": "./lib" + }, + "main": "./lib", + "dependencies": { + "aws-sdk": "^2.892.0" + }, + "strapi": { + "isProvider": true + }, + "engines": { + "node": ">=10.16.0 <=14.x.x", + "npm": ">=6.0.0" + }, + "scripts": { + "test": "echo \"no tests yet\"" + }, + "repository": { + "type": "git", + "url": "git://github.com/chakrahq/strapi-provider-upload-aws-s3.git" + } +}