@strapi/provider-upload-aws-s3 0.0.0-fee711c4e2d9a317dcad4559ef10c87071da6272 → 0.0.0-next.2c917f23cab47f84d4099912c53506cffefef6fe
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 +63 -0
- package/lib/index.js +75 -10
- package/lib/utils.js +63 -0
- package/package.json +6 -4
package/.eslintignore
ADDED
package/.eslintrc.js
ADDED
package/README.md
CHANGED
|
@@ -25,12 +25,57 @@ 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
|
|
|
40
|
+
`./config/plugins.js` or `./config/plugins.ts` for TypeScript projects:
|
|
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
|
+
|
|
34
79
|
`./config/plugins.js`
|
|
35
80
|
|
|
36
81
|
```js
|
|
@@ -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
|
},
|
|
@@ -124,6 +171,22 @@ module.exports = [
|
|
|
124
171
|
|
|
125
172
|
If you use dots in your bucket name, the url of the ressource is in directory style (`s3.yourRegion.amazonaws.com/your.bucket.name/image.jpg`) instead of `yourBucketName.s3.yourRegion.amazonaws.com/image.jpg`. Then only add `s3.yourRegion.amazonaws.com` to img-src and media-src directives.
|
|
126
173
|
|
|
174
|
+
## Bucket CORS Configuration
|
|
175
|
+
|
|
176
|
+
If you are planning on uploading content like GIFs and videos to your S3 bucket, you will want to edit its CORS configuration so that thumbnails are properly shown in Strapi. To do so, open your Bucket on the AWS console and locate the _Cross-origin resource sharing (CORS)_ field under the _Permissions_ tab, then amend the policies by writing your own JSON configuration, or copying and pasting the following one:
|
|
177
|
+
|
|
178
|
+
```json
|
|
179
|
+
[
|
|
180
|
+
{
|
|
181
|
+
"AllowedHeaders": ["*"],
|
|
182
|
+
"AllowedMethods": ["GET"],
|
|
183
|
+
"AllowedOrigins": ["YOUR STRAPI URL"],
|
|
184
|
+
"ExposeHeaders": [],
|
|
185
|
+
"MaxAgeSeconds": 3000
|
|
186
|
+
}
|
|
187
|
+
]
|
|
188
|
+
```
|
|
189
|
+
|
|
127
190
|
## Required AWS Policy Actions
|
|
128
191
|
|
|
129
192
|
These are the minimum amount of permissions needed for this provider to work.
|
package/lib/index.js
CHANGED
|
@@ -6,25 +6,49 @@
|
|
|
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');
|
|
12
|
+
|
|
13
|
+
function assertUrlProtocol(url) {
|
|
14
|
+
// Regex to test protocol like "http://", "https://"
|
|
15
|
+
return /^\w*:\/\//.test(url);
|
|
16
|
+
}
|
|
11
17
|
|
|
12
18
|
module.exports = {
|
|
13
|
-
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
|
+
|
|
14
28
|
const S3 = new AWS.S3({
|
|
15
29
|
apiVersion: '2006-03-01',
|
|
16
30
|
...config,
|
|
17
31
|
});
|
|
18
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
|
+
|
|
19
43
|
const upload = (file, customParams = {}) =>
|
|
20
44
|
new Promise((resolve, reject) => {
|
|
21
45
|
// upload file on S3 bucket
|
|
22
|
-
const
|
|
46
|
+
const fileKey = getFileKey(file);
|
|
23
47
|
S3.upload(
|
|
24
48
|
{
|
|
25
|
-
Key:
|
|
49
|
+
Key: fileKey,
|
|
26
50
|
Body: file.stream || Buffer.from(file.buffer, 'binary'),
|
|
27
|
-
ACL
|
|
51
|
+
ACL,
|
|
28
52
|
ContentType: file.mime,
|
|
29
53
|
...customParams,
|
|
30
54
|
},
|
|
@@ -34,14 +58,55 @@ module.exports = {
|
|
|
34
58
|
}
|
|
35
59
|
|
|
36
60
|
// set the bucket file url
|
|
37
|
-
|
|
38
|
-
|
|
61
|
+
if (assertUrlProtocol(data.Location)) {
|
|
62
|
+
file.url = baseUrl ? `${baseUrl}/${fileKey}` : data.Location;
|
|
63
|
+
} else {
|
|
64
|
+
// Default protocol to https protocol
|
|
65
|
+
file.url = `https://${data.Location}`;
|
|
66
|
+
}
|
|
39
67
|
resolve();
|
|
40
68
|
}
|
|
41
69
|
);
|
|
42
70
|
});
|
|
43
71
|
|
|
44
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
|
+
},
|
|
45
110
|
uploadStream(file, customParams = {}) {
|
|
46
111
|
return upload(file, customParams);
|
|
47
112
|
},
|
|
@@ -51,13 +116,13 @@ module.exports = {
|
|
|
51
116
|
delete(file, customParams = {}) {
|
|
52
117
|
return new Promise((resolve, reject) => {
|
|
53
118
|
// delete file on S3 bucket
|
|
54
|
-
const
|
|
119
|
+
const fileKey = getFileKey(file);
|
|
55
120
|
S3.deleteObject(
|
|
56
121
|
{
|
|
57
|
-
Key:
|
|
122
|
+
Key: fileKey,
|
|
58
123
|
...customParams,
|
|
59
124
|
},
|
|
60
|
-
(err
|
|
125
|
+
(err) => {
|
|
61
126
|
if (err) {
|
|
62
127
|
return reject(err);
|
|
63
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": "0.0.0-
|
|
3
|
+
"version": "0.0.0-next.2c917f23cab47f84d4099912c53506cffefef6fe",
|
|
4
4
|
"description": "AWS S3 provider for strapi upload",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"upload",
|
|
@@ -34,15 +34,17 @@
|
|
|
34
34
|
"lib": "./lib"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
|
-
"test": "
|
|
37
|
+
"test:unit": "jest",
|
|
38
|
+
"test:unit:watch": "jest --watch",
|
|
39
|
+
"lint": "eslint ."
|
|
38
40
|
},
|
|
39
41
|
"dependencies": {
|
|
40
|
-
"aws-sdk": "2.
|
|
42
|
+
"aws-sdk": "2.1287.0",
|
|
41
43
|
"lodash": "4.17.21"
|
|
42
44
|
},
|
|
43
45
|
"engines": {
|
|
44
46
|
"node": ">=14.19.1 <=18.x.x",
|
|
45
47
|
"npm": ">=6.0.0"
|
|
46
48
|
},
|
|
47
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "2c917f23cab47f84d4099912c53506cffefef6fe"
|
|
48
50
|
}
|