@strapi/provider-upload-aws-s3 4.9.0-exp.90df253ba90fd6879eb56a720a1f80d04ff745b8 → 4.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +2 -0
- package/.eslintrc.js +4 -0
- package/README.md +47 -0
- package/lib/index.js +65 -10
- package/lib/utils.js +63 -0
- package/package.json +5 -3
package/.eslintignore
ADDED
package/.eslintrc.js
ADDED
package/README.md
CHANGED
|
@@ -25,14 +25,59 @@ npm install @strapi/provider-upload-aws-s3 --save
|
|
|
25
25
|
|
|
26
26
|
- `provider` defines the name of the provider
|
|
27
27
|
- `providerOptions` is passed down during the construction of the provider. (ex: `new AWS.S3(config)`). [Complete list of options](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor-property)
|
|
28
|
+
- `providerOptions.params` is passed directly to the parameters to each method respectively.
|
|
29
|
+
- `ACL` is the access control list for the object. Defaults to `public-read`.
|
|
30
|
+
- `signedUrlExpires` is the number of seconds before a signed URL expires. (See [how signed URLs work](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-signed-urls.html)). Defaults to 15 minutes and URLs are only signed when ACL is set to `private`.
|
|
31
|
+
- `Bucket` is the name of the bucket to upload to.
|
|
28
32
|
- `actionOptions` is passed directly to the parameters to each method respectively. You can find the complete list of [upload/ uploadStream options](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property) and [delete options](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#deleteObject-property)
|
|
29
33
|
|
|
30
34
|
See the [documentation about using a provider](https://docs.strapi.io/developer-docs/latest/plugins/upload.html#using-a-provider) for information on installing and using a provider. To understand how environment variables are used in Strapi, please refer to the [documentation about environment variables](https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/configurations/optional/environment.html#environment-variables).
|
|
31
35
|
|
|
36
|
+
If you're using the bucket as a CDN and deliver the content on a custom domain, you can get use of the `baseUrl` and `rootPath` properties to configure how your assets' urls will be saved inside Strapi.
|
|
37
|
+
|
|
32
38
|
### Provider Configuration
|
|
33
39
|
|
|
34
40
|
`./config/plugins.js` or `./config/plugins.ts` for TypeScript projects:
|
|
35
41
|
|
|
42
|
+
```js
|
|
43
|
+
module.exports = ({ env }) => ({
|
|
44
|
+
// ...
|
|
45
|
+
upload: {
|
|
46
|
+
config: {
|
|
47
|
+
provider: 'aws-s3',
|
|
48
|
+
providerOptions: {
|
|
49
|
+
baseUrl: env('CDN_URL'),
|
|
50
|
+
rootPath: env('CDN_ROOT_PATH'),
|
|
51
|
+
s3Options: {
|
|
52
|
+
accessKeyId: env('AWS_ACCESS_KEY_ID'),
|
|
53
|
+
secretAccessKey: env('AWS_ACCESS_SECRET'),
|
|
54
|
+
region: env('AWS_REGION'),
|
|
55
|
+
params: {
|
|
56
|
+
ACL: env('AWS_ACL', 'public-read'),
|
|
57
|
+
signedUrlExpires: env('AWS_SIGNED_URL_EXPIRES', 15 * 60),
|
|
58
|
+
Bucket: env('AWS_BUCKET'),
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
actionOptions: {
|
|
63
|
+
upload: {},
|
|
64
|
+
uploadStream: {},
|
|
65
|
+
delete: {},
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
// ...
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Configuration for a private S3 bucket
|
|
74
|
+
|
|
75
|
+
If your bucket is configured to be private, you will need to set the `ACL` option to `private` in the `params` object. This will ensure that the signed URL is generated with the correct permissions.
|
|
76
|
+
|
|
77
|
+
You can also define the expiration time of the signed URL by setting the `signedUrlExpires` option in the `params` object. The default value is 7 days.
|
|
78
|
+
|
|
79
|
+
`./config/plugins.js`
|
|
80
|
+
|
|
36
81
|
```js
|
|
37
82
|
module.exports = ({ env }) => ({
|
|
38
83
|
// ...
|
|
@@ -44,6 +89,8 @@ module.exports = ({ env }) => ({
|
|
|
44
89
|
secretAccessKey: env('AWS_ACCESS_SECRET'),
|
|
45
90
|
region: env('AWS_REGION'),
|
|
46
91
|
params: {
|
|
92
|
+
ACL: 'private', // <== set ACL to private
|
|
93
|
+
signedUrlExpires: env('AWS_SIGNED_URL_EXPIRES', 60 * 60 * 24 * 7),
|
|
47
94
|
Bucket: env('AWS_BUCKET'),
|
|
48
95
|
},
|
|
49
96
|
},
|
package/lib/index.js
CHANGED
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
|
|
7
7
|
/* eslint-disable no-unused-vars */
|
|
8
8
|
// Public node modules.
|
|
9
|
-
const
|
|
9
|
+
const { getOr } = require('lodash/fp');
|
|
10
10
|
const AWS = require('aws-sdk');
|
|
11
|
+
const { getBucketFromUrl } = require('./utils');
|
|
11
12
|
|
|
12
13
|
function assertUrlProtocol(url) {
|
|
13
14
|
// Regex to test protocol like "http://", "https://"
|
|
@@ -15,21 +16,39 @@ function assertUrlProtocol(url) {
|
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
module.exports = {
|
|
18
|
-
init(
|
|
19
|
+
init({ baseUrl = null, rootPath = null, s3Options, ...legacyS3Options }) {
|
|
20
|
+
if (legacyS3Options) {
|
|
21
|
+
process.emitWarning(
|
|
22
|
+
"S3 configuration options passed at root level of the plugin's providerOptions is deprecated and will be removed in a future release. Please wrap them inside the 's3Options:{}' property."
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const config = { ...s3Options, ...legacyS3Options };
|
|
27
|
+
|
|
19
28
|
const S3 = new AWS.S3({
|
|
20
29
|
apiVersion: '2006-03-01',
|
|
21
30
|
...config,
|
|
22
31
|
});
|
|
23
32
|
|
|
33
|
+
const filePrefix = rootPath ? `${rootPath.replace(/\/+$/, '')}/` : '';
|
|
34
|
+
|
|
35
|
+
const getFileKey = (file) => {
|
|
36
|
+
const path = file.path ? `${file.path}/` : '';
|
|
37
|
+
|
|
38
|
+
return `${filePrefix}${path}${file.hash}${file.ext}`;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const ACL = getOr('public-read', ['params', 'ACL'], config);
|
|
42
|
+
|
|
24
43
|
const upload = (file, customParams = {}) =>
|
|
25
44
|
new Promise((resolve, reject) => {
|
|
26
45
|
// upload file on S3 bucket
|
|
27
|
-
const
|
|
46
|
+
const fileKey = getFileKey(file);
|
|
28
47
|
S3.upload(
|
|
29
48
|
{
|
|
30
|
-
Key:
|
|
49
|
+
Key: fileKey,
|
|
31
50
|
Body: file.stream || Buffer.from(file.buffer, 'binary'),
|
|
32
|
-
ACL
|
|
51
|
+
ACL,
|
|
33
52
|
ContentType: file.mime,
|
|
34
53
|
...customParams,
|
|
35
54
|
},
|
|
@@ -40,18 +59,54 @@ module.exports = {
|
|
|
40
59
|
|
|
41
60
|
// set the bucket file url
|
|
42
61
|
if (assertUrlProtocol(data.Location)) {
|
|
43
|
-
file.url = data.Location;
|
|
62
|
+
file.url = baseUrl ? `${baseUrl}/${fileKey}` : data.Location;
|
|
44
63
|
} else {
|
|
45
64
|
// Default protocol to https protocol
|
|
46
65
|
file.url = `https://${data.Location}`;
|
|
47
66
|
}
|
|
48
|
-
|
|
49
67
|
resolve();
|
|
50
68
|
}
|
|
51
69
|
);
|
|
52
70
|
});
|
|
53
71
|
|
|
54
72
|
return {
|
|
73
|
+
isPrivate() {
|
|
74
|
+
return ACL === 'private';
|
|
75
|
+
},
|
|
76
|
+
/**
|
|
77
|
+
* @param {Object} file
|
|
78
|
+
* @param {string} file.path
|
|
79
|
+
* @param {string} file.hash
|
|
80
|
+
* @param {string} file.ext
|
|
81
|
+
* @param {Object} customParams
|
|
82
|
+
* @returns {Promise<{url: string}>}
|
|
83
|
+
*/
|
|
84
|
+
getSignedUrl(file, customParams = {}) {
|
|
85
|
+
// Do not sign the url if it does not come from the same bucket.
|
|
86
|
+
const { bucket } = getBucketFromUrl(file.url);
|
|
87
|
+
if (bucket !== config.params.Bucket) {
|
|
88
|
+
return { url: file.url };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
const fileKey = getFileKey(file);
|
|
93
|
+
|
|
94
|
+
S3.getSignedUrl(
|
|
95
|
+
'getObject',
|
|
96
|
+
{
|
|
97
|
+
Bucket: config.params.Bucket,
|
|
98
|
+
Key: fileKey,
|
|
99
|
+
Expires: getOr(15 * 60, ['params', 'signedUrlExpires'], config), // 15 minutes
|
|
100
|
+
},
|
|
101
|
+
(err, url) => {
|
|
102
|
+
if (err) {
|
|
103
|
+
return reject(err);
|
|
104
|
+
}
|
|
105
|
+
resolve({ url });
|
|
106
|
+
}
|
|
107
|
+
);
|
|
108
|
+
});
|
|
109
|
+
},
|
|
55
110
|
uploadStream(file, customParams = {}) {
|
|
56
111
|
return upload(file, customParams);
|
|
57
112
|
},
|
|
@@ -61,13 +116,13 @@ module.exports = {
|
|
|
61
116
|
delete(file, customParams = {}) {
|
|
62
117
|
return new Promise((resolve, reject) => {
|
|
63
118
|
// delete file on S3 bucket
|
|
64
|
-
const
|
|
119
|
+
const fileKey = getFileKey(file);
|
|
65
120
|
S3.deleteObject(
|
|
66
121
|
{
|
|
67
|
-
Key:
|
|
122
|
+
Key: fileKey,
|
|
68
123
|
...customParams,
|
|
69
124
|
},
|
|
70
|
-
(err
|
|
125
|
+
(err) => {
|
|
71
126
|
if (err) {
|
|
72
127
|
return reject(err);
|
|
73
128
|
}
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const ENDPOINT_PATTERN = /^(.+\.)?s3[.-]([a-z0-9-]+)\./;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Parse the bucket name from a URL.
|
|
7
|
+
* See all URL formats in https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-bucket-intro.html
|
|
8
|
+
*
|
|
9
|
+
* @param {string} fileUrl - the URL to parse
|
|
10
|
+
* @returns {object} result
|
|
11
|
+
* @returns {string} result.bucket - the bucket name
|
|
12
|
+
* @returns {string} result.error - if any
|
|
13
|
+
*/
|
|
14
|
+
function getBucketFromUrl(fileUrl) {
|
|
15
|
+
const uri = new URL(fileUrl);
|
|
16
|
+
|
|
17
|
+
// S3://<bucket-name>/<key>
|
|
18
|
+
if (uri.protocol === 's3:') {
|
|
19
|
+
const bucket = uri.host;
|
|
20
|
+
|
|
21
|
+
if (!bucket) {
|
|
22
|
+
return { err: `Invalid S3 URI: no bucket: ${uri}` };
|
|
23
|
+
}
|
|
24
|
+
return { bucket };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!uri.host) {
|
|
28
|
+
return { err: `Invalid S3 URI: no hostname: ${uri}` };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const matches = uri.host.match(ENDPOINT_PATTERN);
|
|
32
|
+
if (!matches) {
|
|
33
|
+
return { err: `Invalid S3 URI: hostname does not appear to be a valid S3 endpoint: ${uri}` };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const prefix = matches[1];
|
|
37
|
+
// https://s3.amazonaws.com/<bucket-name>
|
|
38
|
+
if (!prefix) {
|
|
39
|
+
if (uri.pathname === '/') {
|
|
40
|
+
return { bucket: null };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const index = uri.pathname.indexOf('/', 1);
|
|
44
|
+
|
|
45
|
+
// https://s3.amazonaws.com/<bucket-name>
|
|
46
|
+
if (index === -1) {
|
|
47
|
+
return { bucket: uri.pathname.substring(1) };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// https://s3.amazonaws.com/<bucket-name>/
|
|
51
|
+
if (index === uri.pathname.length - 1) {
|
|
52
|
+
return { bucket: uri.pathname.substring(1, index) };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// https://s3.amazonaws.com/<bucket-name>/key
|
|
56
|
+
return { bucket: uri.pathname.substring(1, index) };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// https://<bucket-name>.s3.amazonaws.com/
|
|
60
|
+
return { bucket: prefix.substring(0, prefix.length - 1) };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
module.exports = { getBucketFromUrl };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/provider-upload-aws-s3",
|
|
3
|
-
"version": "4.9.
|
|
3
|
+
"version": "4.9.1",
|
|
4
4
|
"description": "AWS S3 provider for strapi upload",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"upload",
|
|
@@ -34,7 +34,9 @@
|
|
|
34
34
|
"lib": "./lib"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
|
-
"test": "
|
|
37
|
+
"test:unit": "run -T jest",
|
|
38
|
+
"test:unit:watch": "run -T jest --watch",
|
|
39
|
+
"lint": "run -T eslint ."
|
|
38
40
|
},
|
|
39
41
|
"dependencies": {
|
|
40
42
|
"aws-sdk": "2.1287.0",
|
|
@@ -44,5 +46,5 @@
|
|
|
44
46
|
"node": ">=14.19.1 <=18.x.x",
|
|
45
47
|
"npm": ">=6.0.0"
|
|
46
48
|
},
|
|
47
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "c8f2f0543b45dda8eadde21a0782b64d156fc786"
|
|
48
50
|
}
|