Skip to content

Commit

Permalink
Merge pull request #3 from stephent/develop/strapi-4.x
Browse files Browse the repository at this point in the history
Develop/strapi 4.x
  • Loading branch information
stephent authored Jan 10, 2023
2 parents 1871ad3 + f08c0ff commit d016625
Show file tree
Hide file tree
Showing 6 changed files with 1,442 additions and 1,789 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"linebreak"
]
}
32 changes: 23 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ This Strapi upload provider adapts the strapi-provider-upload-aws-s3, bundled wi
Inspired by this discussion: https://github.com/strapi/strapi/issues/5868#issuecomment-705200530

This project is essentially the same as https://www.npmjs.com/package/strapi-provider-upload-aws-s3-cdn, but it includes the required dependencies in package.json.

## Compatibility

- Versions 1.x are compatible with Strapi 3.x
- Versions 4.x are compatible with Strapi 4.x (bumped this package version to 4.x to make this more obvious)

## Configuration

Your configuration is passed down to the provider. (e.g: `new AWS.S3(config)`). You can see the complete list of options [here](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor-property)

See the [using a provider](https://strapi.io/documentation/developer-docs/latest/development/plugins/upload.html#using-a-provider) documentation for information on installing and using a provider. And see the [environment variables](https://strapi.io/documentation/developer-docs/latest/setup-deployment-guides/configurations.html#environment-variables) for setting and using environment variables in your configs.
See the [using a provider](https://docs.strapi.io/developer-docs/latest/development/providers.html) documentation for information on installing and using a provider. And see the [environment variables](https://strapi.io/documentation/developer-docs/latest/setup-deployment-guides/configurations.html#environment-variables) for setting and using environment variables in your configs.

If using a CDN to deliver media files to end users, you can include a `cdnUrl` property, as shown below.

Expand All @@ -23,15 +29,17 @@ If using a CDN to deliver media files to end users, you can include a `cdnUrl` p
module.exports = ({ env }) => ({
// ...
upload: {
provider: 'aws-s3-plus-cdn',
providerOptions: {
accessKeyId: env('AWS_ACCESS_KEY_ID'),
secretAccessKey: env('AWS_ACCESS_SECRET'),
region: env('AWS_REGION'),
params: {
Bucket: env('AWS_BUCKET'),
config: {
provider: 'aws-s3-plus-cdn',
providerOptions: {
accessKeyId: env('AWS_ACCESS_KEY_ID'),
secretAccessKey: env('AWS_ACCESS_SECRET'),
region: env('AWS_REGION'),
params: {
Bucket: env('AWS_BUCKET'),
},
cdnUrl: env("CDN_URL"), // Optional CDN URL - include protocol and trailing forward slash, e.g. 'https://assets.example.com/'
},
cdnUrl: env("CDN_URL"), // Optional CDN URL - include protofol and trailing forward slash, e.g. 'https://assets.example.com/'
},
},
// ...
Expand All @@ -42,6 +50,12 @@ module.exports = ({ env }) => ({
Strapi will use the configured S3 bucket for upload and delete operations, but writes the CDN url (if configured) into the database record.

In the event that you need to change the storage backend in the future, to avoid the need to re-upload assets or to write custom queries to update Strapi database records, it is probably best to configure your CDN to use a URL that you control (e.g. use assets.mydomain.com rather than d12345687abc.cloudfront.net). If you need to change the storage backend later, you can simply update your DNS record.

## Resources

- [License](LICENSE)

## Credits

- Thanks to @MattieBelt for the Strapi v4 compatibility work

67 changes: 37 additions & 30 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,48 @@ module.exports = {
...config,
});

return {
upload(file, customParams = {}) {
return new Promise((resolve, reject) => {
// upload file on S3 bucket
const path = file.path ? `${file.path}/` : '';
let params = {
Key: `${path}${file.hash}${file.ext}`,
Body: Buffer.from(file.buffer, 'binary'),
ContentType: file.mime,
...customParams,
};
const upload = (file, customParams = {}) => {
return new Promise((resolve, reject) => {
// upload file on S3 bucket
const path = file.path ? `${file.path}/` : '';
let params = {
Key: `${path}${file.hash}${file.ext}`,
Body: file.stream || Buffer.from(file.buffer, 'binary'),
ContentType: file.mime,
...customParams,
};

// If CDN url not specified, assume the S3 bucket is configured for public-read
// Essentially, this makes this upload provider behave exactly like strapi-provider-upload-aws-s3 if not using a CDN
if (!S3.config.cdnUrl) {
params.ACL = 'public-read';
}
// If CDN url not specified, assume the S3 bucket is configured for public-read
// Essentially, this makes this upload provider behave exactly like strapi-provider-upload-aws-s3 if not using a CDN
if (!S3.config.cdnUrl) {
params.ACL = 'public-read';
}

S3.upload(params, (err, data) => {
if (err) {
return reject(err);
}
S3.upload(params, (err, data) => {
if (err) {
return reject(err);
}

// set the bucket file url
if (S3.config.cdnUrl) {
// Write the url using the CDN instead of S3
file.url = `${S3.config.cdnUrl}${data.Key}`;
} else {
// Use the S3 location if no cdn configured
file.url = data.Location;
}
// set the bucket file url
if (S3.config.cdnUrl) {
// Write the url using the CDN instead of S3
file.url = `${S3.config.cdnUrl}${data.Key}`;
} else {
// Use the S3 location if no cdn configured
file.url = data.Location;
}

resolve();
});
resolve();
});
});
};

return {
uploadStream(file, customParams = {}) {
return upload(file, customParams);
},
upload(file, customParams = {}) {
return upload(file, customParams);
},
delete(file, customParams = {}) {
return new Promise((resolve, reject) => {
Expand Down
Loading

0 comments on commit d016625

Please sign in to comment.