Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save acomagu/0b1ced0add609bf4124ba3a239ea0491 to your computer and use it in GitHub Desktop.

Select an option

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.
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