Created
December 19, 2020 04:51
-
-
Save acomagu/0b1ced0add609bf4124ba3a239ea0491 to your computer and use it in GitHub Desktop.
Strapi upload provider for S3, which creates the bucket if it doesn't exist.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const AWS = require('aws-sdk'); | |
| const s3Provider = require('strapi-provider-upload-aws-s3'); | |
| module.exports = { | |
| init(config) { | |
| const s3 = new AWS.S3({ | |
| apiVersion: '2006-03-01', | |
| ...config, | |
| }); | |
| const s3ProviderInstance = s3Provider.init(config); | |
| return { | |
| async upload(file, customParams) { | |
| const bucket = config.params?.Bucket || customParams?.Bucket; | |
| if (bucket) { | |
| try { | |
| await s3.headBucket().promise(); | |
| } catch (e) { | |
| if (e.statusCode < 400 || e.statusCode >= 500) throw e; | |
| await s3.createBucket({ | |
| CreateBucketConfiguration: { | |
| LocationConstraint: config.region, | |
| }, | |
| }).promise(); | |
| } | |
| } | |
| await s3ProviderInstance.upload(file, customParams); | |
| }, | |
| delete(file, customParams) { | |
| return s3ProviderInstance.delete(file, customParams); | |
| }, | |
| }; | |
| }, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment