Skip to content

Commit d016625

Browse files
authored
Merge pull request #3 from stephent/develop/strapi-4.x
Develop/strapi 4.x
2 parents 1871ad3 + f08c0ff commit d016625

File tree

6 files changed

+1442
-1789
lines changed

6 files changed

+1442
-1789
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"cSpell.words": [
3+
"linebreak"
4+
]
5+
}

README.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ This Strapi upload provider adapts the strapi-provider-upload-aws-s3, bundled wi
77
Inspired by this discussion: https://github.com/strapi/strapi/issues/5868#issuecomment-705200530
88

99
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.
10+
11+
## Compatibility
12+
13+
- Versions 1.x are compatible with Strapi 3.x
14+
- Versions 4.x are compatible with Strapi 4.x (bumped this package version to 4.x to make this more obvious)
15+
1016
## Configuration
1117

1218
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)
1319

14-
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.
20+
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.
1521

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

@@ -23,15 +29,17 @@ If using a CDN to deliver media files to end users, you can include a `cdnUrl` p
2329
module.exports = ({ env }) => ({
2430
// ...
2531
upload: {
26-
provider: 'aws-s3-plus-cdn',
27-
providerOptions: {
28-
accessKeyId: env('AWS_ACCESS_KEY_ID'),
29-
secretAccessKey: env('AWS_ACCESS_SECRET'),
30-
region: env('AWS_REGION'),
31-
params: {
32-
Bucket: env('AWS_BUCKET'),
32+
config: {
33+
provider: 'aws-s3-plus-cdn',
34+
providerOptions: {
35+
accessKeyId: env('AWS_ACCESS_KEY_ID'),
36+
secretAccessKey: env('AWS_ACCESS_SECRET'),
37+
region: env('AWS_REGION'),
38+
params: {
39+
Bucket: env('AWS_BUCKET'),
40+
},
41+
cdnUrl: env("CDN_URL"), // Optional CDN URL - include protocol and trailing forward slash, e.g. 'https://assets.example.com/'
3342
},
34-
cdnUrl: env("CDN_URL"), // Optional CDN URL - include protofol and trailing forward slash, e.g. 'https://assets.example.com/'
3543
},
3644
},
3745
// ...
@@ -42,6 +50,12 @@ module.exports = ({ env }) => ({
4250
Strapi will use the configured S3 bucket for upload and delete operations, but writes the CDN url (if configured) into the database record.
4351

4452
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.
53+
4554
## Resources
4655

4756
- [License](LICENSE)
57+
58+
## Credits
59+
60+
- Thanks to @MattieBelt for the Strapi v4 compatibility work
61+

lib/index.js

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,48 @@ module.exports = {
1515
...config,
1616
});
1717

18-
return {
19-
upload(file, customParams = {}) {
20-
return new Promise((resolve, reject) => {
21-
// upload file on S3 bucket
22-
const path = file.path ? `${file.path}/` : '';
23-
let params = {
24-
Key: `${path}${file.hash}${file.ext}`,
25-
Body: Buffer.from(file.buffer, 'binary'),
26-
ContentType: file.mime,
27-
...customParams,
28-
};
18+
const upload = (file, customParams = {}) => {
19+
return new Promise((resolve, reject) => {
20+
// upload file on S3 bucket
21+
const path = file.path ? `${file.path}/` : '';
22+
let params = {
23+
Key: `${path}${file.hash}${file.ext}`,
24+
Body: file.stream || Buffer.from(file.buffer, 'binary'),
25+
ContentType: file.mime,
26+
...customParams,
27+
};
2928

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

36-
S3.upload(params, (err, data) => {
37-
if (err) {
38-
return reject(err);
39-
}
35+
S3.upload(params, (err, data) => {
36+
if (err) {
37+
return reject(err);
38+
}
4039

41-
// set the bucket file url
42-
if (S3.config.cdnUrl) {
43-
// Write the url using the CDN instead of S3
44-
file.url = `${S3.config.cdnUrl}${data.Key}`;
45-
} else {
46-
// Use the S3 location if no cdn configured
47-
file.url = data.Location;
48-
}
40+
// set the bucket file url
41+
if (S3.config.cdnUrl) {
42+
// Write the url using the CDN instead of S3
43+
file.url = `${S3.config.cdnUrl}${data.Key}`;
44+
} else {
45+
// Use the S3 location if no cdn configured
46+
file.url = data.Location;
47+
}
4948

50-
resolve();
51-
});
49+
resolve();
5250
});
51+
});
52+
};
53+
54+
return {
55+
uploadStream(file, customParams = {}) {
56+
return upload(file, customParams);
57+
},
58+
upload(file, customParams = {}) {
59+
return upload(file, customParams);
5360
},
5461
delete(file, customParams = {}) {
5562
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)