@strapi/provider-upload-aws-s3 0.0.0-next.e3b4cdeebf6e9b0cd5575ff80b8a86715d44ce98 → 0.0.0-next.e6eaa3d0563c85f80fd88b258df70a55c057096e
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/README.md +68 -15
- package/dist/index.d.ts +28 -14
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +156 -107
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +162 -0
- package/dist/index.mjs.map +1 -0
- package/dist/utils.d.ts +5 -15
- package/dist/utils.d.ts.map +1 -0
- package/package.json +16 -12
- package/dist/utils.js +0 -53
- package/dist/utils.js.map +0 -1
package/README.md
CHANGED
|
@@ -49,8 +49,10 @@ module.exports = ({ env }) => ({
|
|
|
49
49
|
baseUrl: env('CDN_URL'),
|
|
50
50
|
rootPath: env('CDN_ROOT_PATH'),
|
|
51
51
|
s3Options: {
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
credentials: {
|
|
53
|
+
accessKeyId: env('AWS_ACCESS_KEY_ID'),
|
|
54
|
+
secretAccessKey: env('AWS_ACCESS_SECRET'),
|
|
55
|
+
},
|
|
54
56
|
region: env('AWS_REGION'),
|
|
55
57
|
params: {
|
|
56
58
|
ACL: env('AWS_ACL', 'public-read'),
|
|
@@ -70,11 +72,13 @@ module.exports = ({ env }) => ({
|
|
|
70
72
|
});
|
|
71
73
|
```
|
|
72
74
|
|
|
73
|
-
### Configuration for a private S3 bucket
|
|
75
|
+
### Configuration for a private S3 bucket and signed URLs
|
|
74
76
|
|
|
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
|
|
77
|
+
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 file URLs are signed.
|
|
76
78
|
|
|
77
|
-
|
|
79
|
+
**Note:** If you are using a CDN, the URLs will not be signed.
|
|
80
|
+
|
|
81
|
+
You can also define the expiration time of the signed URL by setting the `signedUrlExpires` option in the `params` object. The default value is 15 minutes.
|
|
78
82
|
|
|
79
83
|
`./config/plugins.js`
|
|
80
84
|
|
|
@@ -85,12 +89,14 @@ module.exports = ({ env }) => ({
|
|
|
85
89
|
config: {
|
|
86
90
|
provider: 'aws-s3',
|
|
87
91
|
providerOptions: {
|
|
88
|
-
|
|
89
|
-
|
|
92
|
+
credentials: {
|
|
93
|
+
accessKeyId: env('AWS_ACCESS_KEY_ID'),
|
|
94
|
+
secretAccessKey: env('AWS_ACCESS_SECRET'),
|
|
95
|
+
},
|
|
90
96
|
region: env('AWS_REGION'),
|
|
91
97
|
params: {
|
|
92
98
|
ACL: 'private', // <== set ACL to private
|
|
93
|
-
signedUrlExpires: env('AWS_SIGNED_URL_EXPIRES',
|
|
99
|
+
signedUrlExpires: env('AWS_SIGNED_URL_EXPIRES', 15 * 60),
|
|
94
100
|
Bucket: env('AWS_BUCKET'),
|
|
95
101
|
},
|
|
96
102
|
},
|
|
@@ -107,7 +113,7 @@ module.exports = ({ env }) => ({
|
|
|
107
113
|
|
|
108
114
|
#### Configuration for S3 compatible services
|
|
109
115
|
|
|
110
|
-
This plugin may work with S3 compatible services by using the `endpoint
|
|
116
|
+
This plugin may work with S3 compatible services by using the `endpoint`. Scaleway example:
|
|
111
117
|
`./config/plugins.js`
|
|
112
118
|
|
|
113
119
|
```js
|
|
@@ -117,9 +123,12 @@ module.exports = ({ env }) => ({
|
|
|
117
123
|
config: {
|
|
118
124
|
provider: 'aws-s3',
|
|
119
125
|
providerOptions: {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
126
|
+
credentials: {
|
|
127
|
+
accessKeyId: env('SCALEWAY_ACCESS_KEY_ID'),
|
|
128
|
+
secretAccessKey: env('SCALEWAY_ACCESS_SECRET'),
|
|
129
|
+
},
|
|
130
|
+
region: env('SCALEWAY_REGION'), // e.g "fr-par"
|
|
131
|
+
endpoint: env('SCALEWAY_ENDPOINT'), // e.g. "https://s3.fr-par.scw.cloud"
|
|
123
132
|
params: {
|
|
124
133
|
Bucket: env('SCALEWAY_BUCKET'),
|
|
125
134
|
},
|
|
@@ -150,14 +159,14 @@ module.exports = [
|
|
|
150
159
|
"'self'",
|
|
151
160
|
'data:',
|
|
152
161
|
'blob:',
|
|
153
|
-
'
|
|
162
|
+
'market-assets.strapi.io',
|
|
154
163
|
'yourBucketName.s3.yourRegion.amazonaws.com',
|
|
155
164
|
],
|
|
156
165
|
'media-src': [
|
|
157
166
|
"'self'",
|
|
158
167
|
'data:',
|
|
159
168
|
'blob:',
|
|
160
|
-
'
|
|
169
|
+
'market-assets.strapi.io',
|
|
161
170
|
'yourBucketName.s3.yourRegion.amazonaws.com',
|
|
162
171
|
],
|
|
163
172
|
upgradeInsecureRequests: null,
|
|
@@ -169,7 +178,7 @@ module.exports = [
|
|
|
169
178
|
];
|
|
170
179
|
```
|
|
171
180
|
|
|
172
|
-
If you use dots in your bucket name, the url of the
|
|
181
|
+
If you use dots in your bucket name (`forcePathStyle set to false`), the url of the resource is in directory style (`s3.yourRegion.amazonaws.com/your.bucket.name/image.jpg`) instead of `yourBucketName.s3.yourRegion.amazonaws.com/image.jpg` so in that case the img-src and media-src directives to add will be `s3.yourRegion.amazonaws.com` without the bucket name in the url.
|
|
173
182
|
|
|
174
183
|
## Bucket CORS Configuration
|
|
175
184
|
|
|
@@ -200,3 +209,47 @@ These are the minimum amount of permissions needed for this provider to work.
|
|
|
200
209
|
"s3:PutObjectAcl"
|
|
201
210
|
],
|
|
202
211
|
```
|
|
212
|
+
|
|
213
|
+
## Update to AWS SDK V3 and URL Format Change
|
|
214
|
+
|
|
215
|
+
In the recent update of the `@strapi/provider-upload-aws-s3` plugin, we have transitioned from AWS SDK V2 to AWS SDK V3. This significant update brings along a change in the format of the URLs used in Amazon S3 services.
|
|
216
|
+
|
|
217
|
+
### Understanding the New URL Format
|
|
218
|
+
|
|
219
|
+
AWS SDK V3 adopts the virtual-hosted–style URI format for S3 URLs. This format is recommended by AWS and is likely to become required in the near future, as the path-style URI is being deprecated. More details on this format can be found in the [AWS User Guide](https://docs.aws.amazon.com/AmazonS3/latest/userguide/VirtualHosting.html#virtual-hosted-style-access).
|
|
220
|
+
|
|
221
|
+
### Why the Change?
|
|
222
|
+
|
|
223
|
+
The move to virtual-hosted–style URIs aligns with AWS's recommendation and future-proofing strategies. For an in-depth understanding of AWS's decision behind this transition, you can refer to their detailed post [here](https://aws.amazon.com/es/blogs/aws/amazon-s3-path-deprecation-plan-the-rest-of-the-story/).
|
|
224
|
+
|
|
225
|
+
### Configuring Your Strapi Application
|
|
226
|
+
|
|
227
|
+
If you wish to continue using the plugin with Strapi 4.15.x versions or newer without changing your URL format, it's possible to specify your desired URL format directly in the plugin's configuration. Below is an example configuration highlighting the critical `baseUrl` property:
|
|
228
|
+
|
|
229
|
+
```javascript
|
|
230
|
+
upload: {
|
|
231
|
+
config: {
|
|
232
|
+
provider: 'aws-s3',
|
|
233
|
+
providerOptions: {
|
|
234
|
+
credentials: {
|
|
235
|
+
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
236
|
+
secretAccessKey: process.env.AWS_ACCESS_SECRET,
|
|
237
|
+
},
|
|
238
|
+
region: process.env.AWS_REGION,
|
|
239
|
+
baseUrl: `https://s3.${region}.amazonaws.com/${bucket}`, // This line sets the custom url format
|
|
240
|
+
params: {
|
|
241
|
+
ACL: process.env.AWS_ACL || 'public-read',
|
|
242
|
+
signedUrlExpires: process.env.AWS_SIGNED_URL_EXPIRES || 15 * 60,
|
|
243
|
+
Bucket: process.env.AWS_BUCKET,
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
actionOptions: {
|
|
247
|
+
upload: {},
|
|
248
|
+
uploadStream: {},
|
|
249
|
+
delete: {},
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
This configuration ensures compatibility with the updated AWS SDK while providing flexibility in URL format selection, catering to various user needs.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
import type { ReadStream } from 'node:fs';
|
|
4
|
-
import
|
|
5
|
-
|
|
4
|
+
import { DeleteObjectCommandOutput, CompleteMultipartUploadCommandOutput, AbortMultipartUploadCommandOutput, S3ClientConfig, ObjectCannedACL } from '@aws-sdk/client-s3';
|
|
5
|
+
import type { AwsCredentialIdentity } from '@aws-sdk/types';
|
|
6
|
+
export interface File {
|
|
6
7
|
name: string;
|
|
7
8
|
alternativeText?: string;
|
|
8
9
|
caption?: string;
|
|
@@ -13,6 +14,7 @@ interface File {
|
|
|
13
14
|
ext?: string;
|
|
14
15
|
mime: string;
|
|
15
16
|
size: number;
|
|
17
|
+
sizeInBytes: number;
|
|
16
18
|
url: string;
|
|
17
19
|
previewUrl?: string;
|
|
18
20
|
path?: string;
|
|
@@ -21,26 +23,38 @@ interface File {
|
|
|
21
23
|
stream?: ReadStream;
|
|
22
24
|
buffer?: Buffer;
|
|
23
25
|
}
|
|
24
|
-
|
|
26
|
+
export type UploadCommandOutput = (CompleteMultipartUploadCommandOutput | AbortMultipartUploadCommandOutput) & {
|
|
27
|
+
Location: string;
|
|
28
|
+
};
|
|
29
|
+
export interface AWSParams {
|
|
30
|
+
Bucket: string;
|
|
31
|
+
ACL?: ObjectCannedACL;
|
|
32
|
+
signedUrlExpires?: number;
|
|
33
|
+
}
|
|
34
|
+
export interface DefaultOptions extends S3ClientConfig {
|
|
35
|
+
accessKeyId?: AwsCredentialIdentity['accessKeyId'];
|
|
36
|
+
secretAccessKey?: AwsCredentialIdentity['secretAccessKey'];
|
|
37
|
+
credentials?: AwsCredentialIdentity;
|
|
38
|
+
params?: AWSParams;
|
|
39
|
+
[k: string]: any;
|
|
40
|
+
}
|
|
41
|
+
export type InitOptions = (DefaultOptions | {
|
|
42
|
+
s3Options: DefaultOptions;
|
|
43
|
+
}) & {
|
|
25
44
|
baseUrl?: string;
|
|
26
45
|
rootPath?: string;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
Bucket: string;
|
|
30
|
-
ACL?: string;
|
|
31
|
-
signedUrlExpires?: string;
|
|
32
|
-
};
|
|
33
|
-
};
|
|
34
|
-
}
|
|
46
|
+
[k: string]: any;
|
|
47
|
+
};
|
|
35
48
|
declare const _default: {
|
|
36
49
|
init({ baseUrl, rootPath, s3Options, ...legacyS3Options }: InitOptions): {
|
|
37
50
|
isPrivate(): boolean;
|
|
38
|
-
getSignedUrl(file: File): Promise<{
|
|
51
|
+
getSignedUrl(file: File, customParams: any): Promise<{
|
|
39
52
|
url: string;
|
|
40
53
|
}>;
|
|
41
54
|
uploadStream(file: File, customParams?: {}): Promise<void>;
|
|
42
55
|
upload(file: File, customParams?: {}): Promise<void>;
|
|
43
|
-
delete(file: File, customParams?: {}): Promise<
|
|
56
|
+
delete(file: File, customParams?: {}): Promise<DeleteObjectCommandOutput>;
|
|
44
57
|
};
|
|
45
58
|
};
|
|
46
|
-
export
|
|
59
|
+
export default _default;
|
|
60
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C,OAAO,EAIL,yBAAyB,EAEzB,oCAAoC,EACpC,iCAAiC,EACjC,cAAc,EACd,eAAe,EAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAK5D,MAAM,WAAW,IAAI;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CA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C;;+DA0B2D,WAAW;;2BAwCzC,IAAI,gBAAgB,GAAG;iBAAkB,MAAM;;2BAqBrD,IAAI;qBAGV,IAAI;qBAGJ,IAAI,sBAAsB,QAAQ,yBAAyB,CAAC;;;AApE/E,wBA8EE"}
|
package/dist/index.js
CHANGED
|
@@ -1,112 +1,161 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
2
|
+
const fp = require("lodash/fp");
|
|
3
|
+
const clientS3 = require("@aws-sdk/client-s3");
|
|
4
|
+
const s3RequestPresigner = require("@aws-sdk/s3-request-presigner");
|
|
5
|
+
const libStorage = require("@aws-sdk/lib-storage");
|
|
6
|
+
const ENDPOINT_PATTERN = /^(.+\.)?s3[.-]([a-z0-9-]+)\./;
|
|
7
|
+
function isUrlFromBucket(fileUrl, bucketName, baseUrl = "") {
|
|
8
|
+
const url = new URL(fileUrl);
|
|
9
|
+
if (baseUrl) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
const { bucket } = getBucketFromAwsUrl(fileUrl);
|
|
13
|
+
if (bucket) {
|
|
14
|
+
return bucket === bucketName;
|
|
15
|
+
}
|
|
16
|
+
return url.host.startsWith(`${bucketName}.`) || url.pathname.includes(`/${bucketName}/`);
|
|
17
|
+
}
|
|
18
|
+
function getBucketFromAwsUrl(fileUrl) {
|
|
19
|
+
const url = new URL(fileUrl);
|
|
20
|
+
if (url.protocol === "s3:") {
|
|
21
|
+
const bucket = url.host;
|
|
22
|
+
if (!bucket) {
|
|
23
|
+
return { err: `Invalid S3 url: no bucket: ${url}` };
|
|
24
|
+
}
|
|
25
|
+
return { bucket };
|
|
26
|
+
}
|
|
27
|
+
if (!url.host) {
|
|
28
|
+
return { err: `Invalid S3 url: no hostname: ${url}` };
|
|
29
|
+
}
|
|
30
|
+
const matches = url.host.match(ENDPOINT_PATTERN);
|
|
31
|
+
if (!matches) {
|
|
32
|
+
return { err: `Invalid S3 url: hostname does not appear to be a valid S3 endpoint: ${url}` };
|
|
33
|
+
}
|
|
34
|
+
const prefix = matches[1];
|
|
35
|
+
if (!prefix) {
|
|
36
|
+
if (url.pathname === "/") {
|
|
37
|
+
return { bucket: null };
|
|
38
|
+
}
|
|
39
|
+
const index2 = url.pathname.indexOf("/", 1);
|
|
40
|
+
if (index2 === -1) {
|
|
41
|
+
return { bucket: url.pathname.substring(1) };
|
|
42
|
+
}
|
|
43
|
+
if (index2 === url.pathname.length - 1) {
|
|
44
|
+
return { bucket: url.pathname.substring(1, index2) };
|
|
45
|
+
}
|
|
46
|
+
return { bucket: url.pathname.substring(1, index2) };
|
|
47
|
+
}
|
|
48
|
+
return { bucket: prefix.substring(0, prefix.length - 1) };
|
|
14
49
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
50
|
+
const extractCredentials = (options) => {
|
|
51
|
+
if (options.accessKeyId && options.secretAccessKey) {
|
|
52
|
+
return {
|
|
53
|
+
accessKeyId: options.accessKeyId,
|
|
54
|
+
secretAccessKey: options.secretAccessKey
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (options.s3Options?.accessKeyId && options.s3Options.secretAccessKey) {
|
|
58
|
+
process.emitWarning(
|
|
59
|
+
"Credentials passed directly to s3Options is deprecated and will be removed in a future release. Please wrap them inside a credentials object."
|
|
60
|
+
);
|
|
61
|
+
return {
|
|
62
|
+
accessKeyId: options.s3Options.accessKeyId,
|
|
63
|
+
secretAccessKey: options.s3Options.secretAccessKey
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
if (options.s3Options?.credentials) {
|
|
67
|
+
return {
|
|
68
|
+
accessKeyId: options.s3Options.credentials.accessKeyId,
|
|
69
|
+
secretAccessKey: options.s3Options.credentials.secretAccessKey
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
};
|
|
74
|
+
const assertUrlProtocol = (url) => {
|
|
75
|
+
return /^\w*:\/\//.test(url);
|
|
76
|
+
};
|
|
77
|
+
const getConfig = ({ baseUrl, rootPath, s3Options, ...legacyS3Options }) => {
|
|
78
|
+
if (Object.keys(legacyS3Options).length > 0) {
|
|
79
|
+
process.emitWarning(
|
|
80
|
+
"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."
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
const credentials = extractCredentials({ s3Options, ...legacyS3Options });
|
|
84
|
+
const config = {
|
|
85
|
+
...s3Options,
|
|
86
|
+
...legacyS3Options,
|
|
87
|
+
...credentials ? { credentials } : {}
|
|
88
|
+
};
|
|
89
|
+
config.params.ACL = fp.getOr(clientS3.ObjectCannedACL.public_read, ["params", "ACL"], config);
|
|
90
|
+
return config;
|
|
91
|
+
};
|
|
92
|
+
const index = {
|
|
93
|
+
init({ baseUrl, rootPath, s3Options, ...legacyS3Options }) {
|
|
94
|
+
const config = getConfig({ baseUrl, rootPath, s3Options, ...legacyS3Options });
|
|
95
|
+
const s3Client = new clientS3.S3Client(config);
|
|
96
|
+
const filePrefix = rootPath ? `${rootPath.replace(/\/+$/, "")}/` : "";
|
|
97
|
+
const getFileKey = (file) => {
|
|
98
|
+
const path = file.path ? `${file.path}/` : "";
|
|
99
|
+
return `${filePrefix}${path}${file.hash}${file.ext}`;
|
|
100
|
+
};
|
|
101
|
+
const upload = async (file, customParams = {}) => {
|
|
102
|
+
const fileKey = getFileKey(file);
|
|
103
|
+
const uploadObj = new libStorage.Upload({
|
|
104
|
+
client: s3Client,
|
|
105
|
+
params: {
|
|
106
|
+
Bucket: config.params.Bucket,
|
|
107
|
+
Key: fileKey,
|
|
108
|
+
Body: file.stream || Buffer.from(file.buffer, "binary"),
|
|
109
|
+
ACL: config.params.ACL,
|
|
110
|
+
ContentType: file.mime,
|
|
111
|
+
...customParams
|
|
19
112
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
S3.upload(params, onUploaded);
|
|
113
|
+
});
|
|
114
|
+
const upload2 = await uploadObj.done();
|
|
115
|
+
if (assertUrlProtocol(upload2.Location)) {
|
|
116
|
+
file.url = baseUrl ? `${baseUrl}/${fileKey}` : upload2.Location;
|
|
117
|
+
} else {
|
|
118
|
+
file.url = `https://${upload2.Location}`;
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
return {
|
|
122
|
+
isPrivate() {
|
|
123
|
+
return config.params.ACL === "private";
|
|
124
|
+
},
|
|
125
|
+
async getSignedUrl(file, customParams) {
|
|
126
|
+
if (!isUrlFromBucket(file.url, config.params.Bucket, baseUrl)) {
|
|
127
|
+
return { url: file.url };
|
|
128
|
+
}
|
|
129
|
+
const fileKey = getFileKey(file);
|
|
130
|
+
const url = await s3RequestPresigner.getSignedUrl(
|
|
131
|
+
s3Client,
|
|
132
|
+
new clientS3.GetObjectCommand({
|
|
133
|
+
Bucket: config.params.Bucket,
|
|
134
|
+
Key: fileKey,
|
|
135
|
+
...customParams
|
|
136
|
+
}),
|
|
137
|
+
{
|
|
138
|
+
expiresIn: fp.getOr(15 * 60, ["params", "signedUrlExpires"], config)
|
|
139
|
+
}
|
|
140
|
+
);
|
|
141
|
+
return { url };
|
|
142
|
+
},
|
|
143
|
+
uploadStream(file, customParams = {}) {
|
|
144
|
+
return upload(file, customParams);
|
|
145
|
+
},
|
|
146
|
+
upload(file, customParams = {}) {
|
|
147
|
+
return upload(file, customParams);
|
|
148
|
+
},
|
|
149
|
+
delete(file, customParams = {}) {
|
|
150
|
+
const command = new clientS3.DeleteObjectCommand({
|
|
151
|
+
Bucket: config.params.Bucket,
|
|
152
|
+
Key: getFileKey(file),
|
|
153
|
+
...customParams
|
|
62
154
|
});
|
|
63
|
-
return
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
async getSignedUrl(file) {
|
|
68
|
-
// Do not sign the url if it does not come from the same bucket.
|
|
69
|
-
const { bucket } = (0, utils_1.getBucketFromUrl)(file.url);
|
|
70
|
-
if (bucket !== config.params.Bucket) {
|
|
71
|
-
return { url: file.url };
|
|
72
|
-
}
|
|
73
|
-
return new Promise((resolve, reject) => {
|
|
74
|
-
const fileKey = getFileKey(file);
|
|
75
|
-
S3.getSignedUrl('getObject', {
|
|
76
|
-
Bucket: config.params.Bucket,
|
|
77
|
-
Key: fileKey,
|
|
78
|
-
Expires: (0, fp_1.getOr)(15 * 60, ['params', 'signedUrlExpires'], config), // 15 minutes
|
|
79
|
-
}, (err, url) => {
|
|
80
|
-
if (err) {
|
|
81
|
-
return reject(err);
|
|
82
|
-
}
|
|
83
|
-
resolve({ url });
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
},
|
|
87
|
-
uploadStream(file, customParams = {}) {
|
|
88
|
-
return upload(file, customParams);
|
|
89
|
-
},
|
|
90
|
-
upload(file, customParams = {}) {
|
|
91
|
-
return upload(file, customParams);
|
|
92
|
-
},
|
|
93
|
-
delete(file, customParams = {}) {
|
|
94
|
-
return new Promise((resolve, reject) => {
|
|
95
|
-
// delete file on S3 bucket
|
|
96
|
-
const fileKey = getFileKey(file);
|
|
97
|
-
S3.deleteObject({
|
|
98
|
-
Key: fileKey,
|
|
99
|
-
Bucket: config.params.Bucket,
|
|
100
|
-
...customParams,
|
|
101
|
-
}, (err) => {
|
|
102
|
-
if (err) {
|
|
103
|
-
return reject(err);
|
|
104
|
-
}
|
|
105
|
-
resolve();
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
},
|
|
109
|
-
};
|
|
110
|
-
},
|
|
155
|
+
return s3Client.send(command);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
}
|
|
111
159
|
};
|
|
112
|
-
|
|
160
|
+
module.exports = index;
|
|
161
|
+
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AACA,kCAAkC;AAClC,sDAA0B;AAC1B,mCAA2C;AAsB3C,iCAAiC;AACjC,8DAA8D;AAC9D,OAAO,CAAC,sCAAsC,CAAC,CAAC,QAAQ,GAAG,IAAI,CAAC;AAEhE,SAAS,cAAc,CAAC,GAAW;IACjC,oDAAoD;IACpD,OAAO,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC/B,CAAC;AAcD,iBAAS;IACP,IAAI,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,eAAe,EAAe;QACpE,IAAI,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE;YAC3C,OAAO,CAAC,WAAW,CACjB,2LAA2L,CAC5L,CAAC;SACH;QAED,MAAM,MAAM,GAAG,EAAE,GAAG,SAAS,EAAE,GAAG,eAAe,EAAE,CAAC;QAEpD,MAAM,EAAE,GAAG,IAAI,iBAAG,CAAC,EAAE,CAAC;YACpB,UAAU,EAAE,YAAY;YACxB,GAAG,MAAM;SACV,CAAC,CAAC;QAEH,MAAM,UAAU,GAAG,QAAQ,CAAC,CA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ACP,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;yBACpB;wBACD,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;oBACnB,CAAC,CACF,CAAC;gBACJ,CAAC,CAAC,CAAC;YACL,CAAC;YACD,YAAY,CAAC,IAAU,EAAE,YAAY,GAAG,EAAE;gBACxC,OAAO,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;YACpC,CAAC;YACD,MAAM,CAAC,IAAU,EAAE,YAAY,GAAG,EAAE;gBAClC,OAAO,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;YACpC,CAAC;YACD,MAAM,CAAC,IAAU,EAAE,YAAY,GAAG,EAAE;gBAClC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;oBACrC,2BAA2B;oBAC3B,MAAM,OAAO,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;oBACjC,EAAE,CAAC,YAAY,CACb;wBACE,GAAG,EAAE,OAAO;wBACZ,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM;wBAC5B,GAAG,YAAY;qBAChB,EACD,CAAC,GAAG,EAAE,EAAE;wBACN,IAAI,GAAG,EAAE;4BACP,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;yBACpB;wBAED,OAAO,EAAE,CAAC;oBACZ,CAAC,CACF,CAAC;gBACJ,CAAC,CAAC,CAAC;YACL,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":["import type { AwsCredentialIdentity } from '@aws-sdk/types';\nimport type { InitOptions } from '.';\n\nconst ENDPOINT_PATTERN = /^(.+\\.)?s3[.-]([a-z0-9-]+)\\./;\n\ninterface BucketInfo {\n bucket?: string | null;\n err?: string;\n}\n\nexport function isUrlFromBucket(fileUrl: string, bucketName: string, baseUrl = ''): boolean {\n const url = new URL(fileUrl);\n\n // Check if the file URL is using a base URL (e.g. a CDN).\n // In this case do not sign the URL.\n if (baseUrl) {\n return false;\n }\n\n const { bucket } = getBucketFromAwsUrl(fileUrl);\n\n if (bucket) {\n return bucket === bucketName;\n }\n\n // File URL might be of an S3-compatible provider. (or an invalid URL)\n // In this case, check if the bucket name appears in the URL host or path.\n // e.g. https://minio.example.com/bucket-name/object-key\n // e.g. https://bucket.nyc3.digitaloceanspaces.com/folder/img.png\n return url.host.startsWith(`${bucketName}.`) || url.pathname.includes(`/${bucketName}/`);\n}\n\n/**\n * Parse the bucket name from a URL.\n * See all URL formats in https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-bucket-intro.html\n *\n * @param {string} fileUrl - the URL to parse\n * @returns {object} result\n * @returns {string} result.bucket - the bucket name\n * @returns {string} result.err - if any\n */\nfunction getBucketFromAwsUrl(fileUrl: string): BucketInfo {\n const url = new URL(fileUrl);\n\n // S3://<bucket-name>/<key>\n if (url.protocol === 's3:') {\n const bucket = url.host;\n\n if (!bucket) {\n return { err: `Invalid S3 url: no bucket: ${url}` };\n }\n return { bucket };\n }\n\n if (!url.host) {\n return { err: `Invalid S3 url: no hostname: ${url}` };\n }\n\n const matches = url.host.match(ENDPOINT_PATTERN);\n if (!matches) {\n return { err: `Invalid S3 url: hostname does not appear to be a valid S3 endpoint: ${url}` };\n }\n\n const prefix = matches[1];\n // https://s3.amazonaws.com/<bucket-name>\n if (!prefix) {\n if (url.pathname === '/') {\n return { bucket: null };\n }\n\n const index = url.pathname.indexOf('/', 1);\n\n // https://s3.amazonaws.com/<bucket-name>\n if (index === -1) {\n return { bucket: url.pathname.substring(1) };\n }\n\n // https://s3.amazonaws.com/<bucket-name>/\n if (index === url.pathname.length - 1) {\n return { bucket: url.pathname.substring(1, index) };\n }\n\n // https://s3.amazonaws.com/<bucket-name>/key\n return { bucket: url.pathname.substring(1, index) };\n }\n\n // https://<bucket-name>.s3.amazonaws.com/\n return { bucket: prefix.substring(0, prefix.length - 1) };\n}\n\n// TODO Remove this in V5 since we will only support the new config structure\nexport const extractCredentials = (options: InitOptions): AwsCredentialIdentity | null => {\n // legacy\n if (options.accessKeyId && options.secretAccessKey) {\n return {\n accessKeyId: options.accessKeyId,\n secretAccessKey: options.secretAccessKey,\n };\n }\n // Legacy\n if (options.s3Options?.accessKeyId && options.s3Options.secretAccessKey) {\n process.emitWarning(\n 'Credentials passed directly to s3Options is deprecated and will be removed in a future release. Please wrap them inside a credentials object.'\n );\n return {\n accessKeyId: options.s3Options.accessKeyId,\n secretAccessKey: options.s3Options.secretAccessKey,\n };\n }\n // V5\n if (options.s3Options?.credentials) {\n return {\n accessKeyId: options.s3Options.credentials.accessKeyId,\n secretAccessKey: options.s3Options.credentials.secretAccessKey,\n };\n }\n return null;\n};\n","import type { ReadStream } from 'node:fs';\nimport { getOr } from 'lodash/fp';\nimport {\n S3Client,\n GetObjectCommand,\n DeleteObjectCommand,\n DeleteObjectCommandOutput,\n PutObjectCommandInput,\n CompleteMultipartUploadCommandOutput,\n AbortMultipartUploadCommandOutput,\n S3ClientConfig,\n ObjectCannedACL,\n} from '@aws-sdk/client-s3';\nimport type { AwsCredentialIdentity } from '@aws-sdk/types';\nimport { getSignedUrl } from '@aws-sdk/s3-request-presigner';\nimport { Upload } from '@aws-sdk/lib-storage';\nimport { extractCredentials, isUrlFromBucket } from './utils';\n\nexport interface File {\n name: string;\n alternativeText?: string;\n caption?: string;\n width?: number;\n height?: number;\n formats?: Record<string, unknown>;\n hash: string;\n ext?: string;\n mime: string;\n size: number;\n sizeInBytes: number;\n url: string;\n previewUrl?: string;\n path?: string;\n provider?: string;\n provider_metadata?: Record<string, unknown>;\n stream?: ReadStream;\n buffer?: Buffer;\n}\n\nexport type UploadCommandOutput = (\n | CompleteMultipartUploadCommandOutput\n | AbortMultipartUploadCommandOutput\n) & {\n Location: string;\n};\n\nexport interface AWSParams {\n Bucket: string; // making it required\n ACL?: ObjectCannedACL;\n signedUrlExpires?: number;\n}\n\nexport interface DefaultOptions extends S3ClientConfig {\n // TODO Remove this in V5\n accessKeyId?: AwsCredentialIdentity['accessKeyId'];\n secretAccessKey?: AwsCredentialIdentity['secretAccessKey'];\n // Keep this for V5\n credentials?: AwsCredentialIdentity;\n params?: AWSParams;\n [k: string]: any;\n}\n\nexport type InitOptions = (DefaultOptions | { s3Options: DefaultOptions }) & {\n baseUrl?: string;\n rootPath?: string;\n [k: string]: any;\n};\n\nconst assertUrlProtocol = (url: string) => {\n // Regex to test protocol like \"http://\", \"https://\"\n return /^\\w*:\\/\\//.test(url);\n};\n\nconst getConfig = ({ baseUrl, rootPath, s3Options, ...legacyS3Options }: InitOptions) => {\n if (Object.keys(legacyS3Options).length > 0) {\n process.emitWarning(\n \"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.\"\n );\n }\n const credentials = extractCredentials({ s3Options, ...legacyS3Options });\n const config = {\n ...s3Options,\n ...legacyS3Options,\n ...(credentials ? { credentials } : {}),\n };\n\n config.params.ACL = getOr(ObjectCannedACL.public_read, ['params', 'ACL'], config);\n\n return config;\n};\n\nexport default {\n init({ baseUrl, rootPath, s3Options, ...legacyS3Options }: InitOptions) {\n // TODO V5 change config structure to avoid having to do this\n const config = getConfig({ baseUrl, rootPath, s3Options, ...legacyS3Options });\n const s3Client = new S3Client(config);\n const filePrefix = rootPath ? `${rootPath.replace(/\\/+$/, '')}/` : '';\n\n const getFileKey = (file: File) => {\n const path = file.path ? `${file.path}/` : '';\n return `${filePrefix}${path}${file.hash}${file.ext}`;\n };\n\n const upload = async (file: File, customParams: Partial<PutObjectCommandInput> = {}) => {\n const fileKey = getFileKey(file);\n const uploadObj = new Upload({\n client: s3Client,\n params: {\n Bucket: config.params.Bucket,\n Key: fileKey,\n Body: file.stream || Buffer.from(file.buffer as any, 'binary'),\n ACL: config.params.ACL,\n ContentType: file.mime,\n ...customParams,\n },\n });\n\n const upload = (await uploadObj.done()) as UploadCommandOutput;\n\n if (assertUrlProtocol(upload.Location)) {\n file.url = baseUrl ? `${baseUrl}/${fileKey}` : upload.Location;\n } else {\n // Default protocol to https protocol\n file.url = `https://${upload.Location}`;\n }\n };\n\n return {\n isPrivate() {\n return config.params.ACL === 'private';\n },\n\n async getSignedUrl(file: File, customParams: any): Promise<{ url: string }> {\n // Do not sign the url if it does not come from the same bucket.\n if (!isUrlFromBucket(file.url, config.params.Bucket, baseUrl)) {\n return { url: file.url };\n }\n const fileKey = getFileKey(file);\n\n const url = await getSignedUrl(\n s3Client,\n new GetObjectCommand({\n Bucket: config.params.Bucket,\n Key: fileKey,\n ...customParams,\n }),\n {\n expiresIn: getOr(15 * 60, ['params', 'signedUrlExpires'], config),\n }\n );\n\n return { url };\n },\n uploadStream(file: File, customParams = {}) {\n return upload(file, customParams);\n },\n upload(file: File, customParams = {}) {\n return upload(file, customParams);\n },\n delete(file: File, customParams = {}): Promise<DeleteObjectCommandOutput> {\n const command = new DeleteObjectCommand({\n Bucket: config.params.Bucket,\n Key: getFileKey(file),\n ...customParams,\n });\n return s3Client.send(command);\n },\n };\n },\n};\n"],"names":["index","getOr","ObjectCannedACL","S3Client","Upload","upload","getSignedUrl","GetObjectCommand","DeleteObjectCommand"],"mappings":";;;;;AAGA,MAAM,mBAAmB;AAOlB,SAAS,gBAAgB,SAAiB,YAAoB,UAAU,IAAa;AACpF,QAAA,MAAM,IAAI,IAAI,OAAO;AAI3B,MAAI,SAAS;AACJ,WAAA;AAAA,EACT;AAEA,QAAM,EAAE,OAAA,IAAW,oBAAoB,OAAO;AAE9C,MAAI,QAAQ;AACV,WAAO,WAAW;AAAA,EACpB;AAMA,SAAO,IAAI,KAAK,WAAW,GAAG,UAAU,GAAG,KAAK,IAAI,SAAS,SAAS,IAAI,UAAU,GAAG;AACzF;AAWA,SAAS,oBAAoB,SAA6B;AAClD,QAAA,MAAM,IAAI,IAAI,OAAO;AAGvB,MAAA,IAAI,aAAa,OAAO;AAC1B,UAAM,SAAS,IAAI;AAEnB,QAAI,CAAC,QAAQ;AACX,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AAO;AAAA,MAC9B;AAAA,IAAA;AAAA,EAEJ;AACF;;"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { getOr } from "lodash/fp";
|
|
2
|
+
import { S3Client, GetObjectCommand, DeleteObjectCommand, ObjectCannedACL } from "@aws-sdk/client-s3";
|
|
3
|
+
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
4
|
+
import { Upload } from "@aws-sdk/lib-storage";
|
|
5
|
+
const ENDPOINT_PATTERN = /^(.+\.)?s3[.-]([a-z0-9-]+)\./;
|
|
6
|
+
function isUrlFromBucket(fileUrl, bucketName, baseUrl = "") {
|
|
7
|
+
const url = new URL(fileUrl);
|
|
8
|
+
if (baseUrl) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
const { bucket } = getBucketFromAwsUrl(fileUrl);
|
|
12
|
+
if (bucket) {
|
|
13
|
+
return bucket === bucketName;
|
|
14
|
+
}
|
|
15
|
+
return url.host.startsWith(`${bucketName}.`) || url.pathname.includes(`/${bucketName}/`);
|
|
16
|
+
}
|
|
17
|
+
function getBucketFromAwsUrl(fileUrl) {
|
|
18
|
+
const url = new URL(fileUrl);
|
|
19
|
+
if (url.protocol === "s3:") {
|
|
20
|
+
const bucket = url.host;
|
|
21
|
+
if (!bucket) {
|
|
22
|
+
return { err: `Invalid S3 url: no bucket: ${url}` };
|
|
23
|
+
}
|
|
24
|
+
return { bucket };
|
|
25
|
+
}
|
|
26
|
+
if (!url.host) {
|
|
27
|
+
return { err: `Invalid S3 url: no hostname: ${url}` };
|
|
28
|
+
}
|
|
29
|
+
const matches = url.host.match(ENDPOINT_PATTERN);
|
|
30
|
+
if (!matches) {
|
|
31
|
+
return { err: `Invalid S3 url: hostname does not appear to be a valid S3 endpoint: ${url}` };
|
|
32
|
+
}
|
|
33
|
+
const prefix = matches[1];
|
|
34
|
+
if (!prefix) {
|
|
35
|
+
if (url.pathname === "/") {
|
|
36
|
+
return { bucket: null };
|
|
37
|
+
}
|
|
38
|
+
const index2 = url.pathname.indexOf("/", 1);
|
|
39
|
+
if (index2 === -1) {
|
|
40
|
+
return { bucket: url.pathname.substring(1) };
|
|
41
|
+
}
|
|
42
|
+
if (index2 === url.pathname.length - 1) {
|
|
43
|
+
return { bucket: url.pathname.substring(1, index2) };
|
|
44
|
+
}
|
|
45
|
+
return { bucket: url.pathname.substring(1, index2) };
|
|
46
|
+
}
|
|
47
|
+
return { bucket: prefix.substring(0, prefix.length - 1) };
|
|
48
|
+
}
|
|
49
|
+
const extractCredentials = (options) => {
|
|
50
|
+
if (options.accessKeyId && options.secretAccessKey) {
|
|
51
|
+
return {
|
|
52
|
+
accessKeyId: options.accessKeyId,
|
|
53
|
+
secretAccessKey: options.secretAccessKey
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (options.s3Options?.accessKeyId && options.s3Options.secretAccessKey) {
|
|
57
|
+
process.emitWarning(
|
|
58
|
+
"Credentials passed directly to s3Options is deprecated and will be removed in a future release. Please wrap them inside a credentials object."
|
|
59
|
+
);
|
|
60
|
+
return {
|
|
61
|
+
accessKeyId: options.s3Options.accessKeyId,
|
|
62
|
+
secretAccessKey: options.s3Options.secretAccessKey
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
if (options.s3Options?.credentials) {
|
|
66
|
+
return {
|
|
67
|
+
accessKeyId: options.s3Options.credentials.accessKeyId,
|
|
68
|
+
secretAccessKey: options.s3Options.credentials.secretAccessKey
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
};
|
|
73
|
+
const assertUrlProtocol = (url) => {
|
|
74
|
+
return /^\w*:\/\//.test(url);
|
|
75
|
+
};
|
|
76
|
+
const getConfig = ({ baseUrl, rootPath, s3Options, ...legacyS3Options }) => {
|
|
77
|
+
if (Object.keys(legacyS3Options).length > 0) {
|
|
78
|
+
process.emitWarning(
|
|
79
|
+
"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."
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
const credentials = extractCredentials({ s3Options, ...legacyS3Options });
|
|
83
|
+
const config = {
|
|
84
|
+
...s3Options,
|
|
85
|
+
...legacyS3Options,
|
|
86
|
+
...credentials ? { credentials } : {}
|
|
87
|
+
};
|
|
88
|
+
config.params.ACL = getOr(ObjectCannedACL.public_read, ["params", "ACL"], config);
|
|
89
|
+
return config;
|
|
90
|
+
};
|
|
91
|
+
const index = {
|
|
92
|
+
init({ baseUrl, rootPath, s3Options, ...legacyS3Options }) {
|
|
93
|
+
const config = getConfig({ baseUrl, rootPath, s3Options, ...legacyS3Options });
|
|
94
|
+
const s3Client = new S3Client(config);
|
|
95
|
+
const filePrefix = rootPath ? `${rootPath.replace(/\/+$/, "")}/` : "";
|
|
96
|
+
const getFileKey = (file) => {
|
|
97
|
+
const path = file.path ? `${file.path}/` : "";
|
|
98
|
+
return `${filePrefix}${path}${file.hash}${file.ext}`;
|
|
99
|
+
};
|
|
100
|
+
const upload = async (file, customParams = {}) => {
|
|
101
|
+
const fileKey = getFileKey(file);
|
|
102
|
+
const uploadObj = new Upload({
|
|
103
|
+
client: s3Client,
|
|
104
|
+
params: {
|
|
105
|
+
Bucket: config.params.Bucket,
|
|
106
|
+
Key: fileKey,
|
|
107
|
+
Body: file.stream || Buffer.from(file.buffer, "binary"),
|
|
108
|
+
ACL: config.params.ACL,
|
|
109
|
+
ContentType: file.mime,
|
|
110
|
+
...customParams
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
const upload2 = await uploadObj.done();
|
|
114
|
+
if (assertUrlProtocol(upload2.Location)) {
|
|
115
|
+
file.url = baseUrl ? `${baseUrl}/${fileKey}` : upload2.Location;
|
|
116
|
+
} else {
|
|
117
|
+
file.url = `https://${upload2.Location}`;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
return {
|
|
121
|
+
isPrivate() {
|
|
122
|
+
return config.params.ACL === "private";
|
|
123
|
+
},
|
|
124
|
+
async getSignedUrl(file, customParams) {
|
|
125
|
+
if (!isUrlFromBucket(file.url, config.params.Bucket, baseUrl)) {
|
|
126
|
+
return { url: file.url };
|
|
127
|
+
}
|
|
128
|
+
const fileKey = getFileKey(file);
|
|
129
|
+
const url = await getSignedUrl(
|
|
130
|
+
s3Client,
|
|
131
|
+
new GetObjectCommand({
|
|
132
|
+
Bucket: config.params.Bucket,
|
|
133
|
+
Key: fileKey,
|
|
134
|
+
...customParams
|
|
135
|
+
}),
|
|
136
|
+
{
|
|
137
|
+
expiresIn: getOr(15 * 60, ["params", "signedUrlExpires"], config)
|
|
138
|
+
}
|
|
139
|
+
);
|
|
140
|
+
return { url };
|
|
141
|
+
},
|
|
142
|
+
uploadStream(file, customParams = {}) {
|
|
143
|
+
return upload(file, customParams);
|
|
144
|
+
},
|
|
145
|
+
upload(file, customParams = {}) {
|
|
146
|
+
return upload(file, customParams);
|
|
147
|
+
},
|
|
148
|
+
delete(file, customParams = {}) {
|
|
149
|
+
const command = new DeleteObjectCommand({
|
|
150
|
+
Bucket: config.params.Bucket,
|
|
151
|
+
Key: getFileKey(file),
|
|
152
|
+
...customParams
|
|
153
|
+
});
|
|
154
|
+
return s3Client.send(command);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
export {
|
|
160
|
+
index as default
|
|
161
|
+
};
|
|
162
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":["import type { AwsCredentialIdentity } from '@aws-sdk/types';\nimport type { InitOptions } from '.';\n\nconst ENDPOINT_PATTERN = /^(.+\\.)?s3[.-]([a-z0-9-]+)\\./;\n\ninterface BucketInfo {\n bucket?: string | null;\n err?: string;\n}\n\nexport function isUrlFromBucket(fileUrl: string, bucketName: string, baseUrl = ''): boolean {\n const url = new URL(fileUrl);\n\n // Check if the file URL is using a base URL (e.g. a CDN).\n // In this case do not sign the URL.\n if (baseUrl) {\n return false;\n }\n\n const { bucket } = getBucketFromAwsUrl(fileUrl);\n\n if (bucket) {\n return bucket === bucketName;\n }\n\n // File URL might be of an S3-compatible provider. (or an invalid URL)\n // In this case, check if the bucket name appears in the URL host or path.\n // e.g. https://minio.example.com/bucket-name/object-key\n // e.g. https://bucket.nyc3.digitaloceanspaces.com/folder/img.png\n return url.host.startsWith(`${bucketName}.`) || url.pathname.includes(`/${bucketName}/`);\n}\n\n/**\n * Parse the bucket name from a URL.\n * See all URL formats in https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-bucket-intro.html\n *\n * @param {string} fileUrl - the URL to parse\n * @returns {object} result\n * @returns {string} result.bucket - the bucket name\n * @returns {string} result.err - if any\n */\nfunction getBucketFromAwsUrl(fileUrl: string): BucketInfo {\n const url = new URL(fileUrl);\n\n // S3://<bucket-name>/<key>\n if (url.protocol === 's3:') {\n const bucket = url.host;\n\n if (!bucket) {\n return { err: `Invalid S3 url: no bucket: ${url}` };\n }\n return { bucket };\n }\n\n if (!url.host) {\n return { err: `Invalid S3 url: no hostname: ${url}` };\n }\n\n const matches = url.host.match(ENDPOINT_PATTERN);\n if (!matches) {\n return { err: `Invalid S3 url: hostname does not appear to be a valid S3 endpoint: ${url}` };\n }\n\n const prefix = matches[1];\n // https://s3.amazonaws.com/<bucket-name>\n if (!prefix) {\n if (url.pathname === '/') {\n return { bucket: null };\n }\n\n const index = url.pathname.indexOf('/', 1);\n\n // https://s3.amazonaws.com/<bucket-name>\n if (index === -1) {\n return { bucket: url.pathname.substring(1) };\n }\n\n // https://s3.amazonaws.com/<bucket-name>/\n if (index === url.pathname.length - 1) {\n return { bucket: url.pathname.substring(1, index) };\n }\n\n // https://s3.amazonaws.com/<bucket-name>/key\n return { bucket: url.pathname.substring(1, index) };\n }\n\n // https://<bucket-name>.s3.amazonaws.com/\n return { bucket: prefix.substring(0, prefix.length - 1) };\n}\n\n// TODO Remove this in V5 since we will only support the new config structure\nexport const extractCredentials = (options: InitOptions): AwsCredentialIdentity | null => {\n // legacy\n if (options.accessKeyId && options.secretAccessKey) {\n return {\n accessKeyId: options.accessKeyId,\n secretAccessKey: options.secretAccessKey,\n };\n }\n // Legacy\n if (options.s3Options?.accessKeyId && options.s3Options.secretAccessKey) {\n process.emitWarning(\n 'Credentials passed directly to s3Options is deprecated and will be removed in a future release. Please wrap them inside a credentials object.'\n );\n return {\n accessKeyId: options.s3Options.accessKeyId,\n secretAccessKey: options.s3Options.secretAccessKey,\n };\n }\n // V5\n if (options.s3Options?.credentials) {\n return {\n accessKeyId: options.s3Options.credentials.accessKeyId,\n secretAccessKey: options.s3Options.credentials.secretAccessKey,\n };\n }\n return null;\n};\n","import type { ReadStream } from 'node:fs';\nimport { getOr } from 'lodash/fp';\nimport {\n S3Client,\n GetObjectCommand,\n DeleteObjectCommand,\n DeleteObjectCommandOutput,\n PutObjectCommandInput,\n CompleteMultipartUploadCommandOutput,\n AbortMultipartUploadCommandOutput,\n S3ClientConfig,\n ObjectCannedACL,\n} from '@aws-sdk/client-s3';\nimport type { AwsCredentialIdentity } from '@aws-sdk/types';\nimport { getSignedUrl } from '@aws-sdk/s3-request-presigner';\nimport { Upload } from '@aws-sdk/lib-storage';\nimport { extractCredentials, isUrlFromBucket } from './utils';\n\nexport interface File {\n name: string;\n alternativeText?: string;\n caption?: string;\n width?: number;\n height?: number;\n formats?: Record<string, unknown>;\n hash: string;\n ext?: string;\n mime: string;\n size: number;\n sizeInBytes: number;\n url: string;\n previewUrl?: string;\n path?: string;\n provider?: string;\n provider_metadata?: Record<string, unknown>;\n stream?: ReadStream;\n buffer?: Buffer;\n}\n\nexport type UploadCommandOutput = (\n | CompleteMultipartUploadCommandOutput\n | AbortMultipartUploadCommandOutput\n) & {\n Location: string;\n};\n\nexport interface AWSParams {\n Bucket: string; // making it required\n ACL?: ObjectCannedACL;\n signedUrlExpires?: number;\n}\n\nexport interface DefaultOptions extends S3ClientConfig {\n // TODO Remove this in V5\n accessKeyId?: AwsCredentialIdentity['accessKeyId'];\n secretAccessKey?: AwsCredentialIdentity['secretAccessKey'];\n // Keep this for V5\n credentials?: AwsCredentialIdentity;\n params?: AWSParams;\n [k: string]: any;\n}\n\nexport type InitOptions = (DefaultOptions | { s3Options: DefaultOptions }) & {\n baseUrl?: string;\n rootPath?: string;\n [k: string]: any;\n};\n\nconst assertUrlProtocol = (url: string) => {\n // Regex to test protocol like \"http://\", \"https://\"\n return /^\\w*:\\/\\//.test(url);\n};\n\nconst getConfig = ({ baseUrl, rootPath, s3Options, ...legacyS3Options }: InitOptions) => {\n if (Object.keys(legacyS3Options).length > 0) {\n process.emitWarning(\n \"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.\"\n );\n }\n const credentials = extractCredentials({ s3Options, ...legacyS3Options });\n const config = {\n ...s3Options,\n ...legacyS3Options,\n ...(credentials ? { credentials } : {}),\n };\n\n config.params.ACL = getOr(ObjectCannedACL.public_read, ['params', 'ACL'], config);\n\n return config;\n};\n\nexport default {\n init({ baseUrl, rootPath, s3Options, ...legacyS3Options }: InitOptions) {\n // TODO V5 change config structure to avoid having to do this\n const config = getConfig({ baseUrl, rootPath, s3Options, ...legacyS3Options });\n const s3Client = new S3Client(config);\n const filePrefix = rootPath ? `${rootPath.replace(/\\/+$/, '')}/` : '';\n\n const getFileKey = (file: File) => {\n const path = file.path ? `${file.path}/` : '';\n return `${filePrefix}${path}${file.hash}${file.ext}`;\n };\n\n const upload = async (file: File, customParams: Partial<PutObjectCommandInput> = {}) => {\n const fileKey = getFileKey(file);\n const uploadObj = new Upload({\n client: s3Client,\n params: {\n Bucket: config.params.Bucket,\n Key: fileKey,\n Body: file.stream || Buffer.from(file.buffer as any, 'binary'),\n ACL: config.params.ACL,\n ContentType: file.mime,\n ...customParams,\n },\n });\n\n const upload = (await uploadObj.done()) as UploadCommandOutput;\n\n if (assertUrlProtocol(upload.Location)) {\n file.url = baseUrl ? `${baseUrl}/${fileKey}` : upload.Location;\n } else {\n // Default protocol to https protocol\n file.url = `https://${upload.Location}`;\n }\n };\n\n return {\n isPrivate() {\n return config.params.ACL === 'private';\n },\n\n async getSignedUrl(file: File, customParams: any): Promise<{ url: string }> {\n // Do not sign the url if it does not come from the same bucket.\n if (!isUrlFromBucket(file.url, config.params.Bucket, baseUrl)) {\n return { url: file.url };\n }\n const fileKey = getFileKey(file);\n\n const url = await getSignedUrl(\n s3Client,\n new GetObjectCommand({\n Bucket: config.params.Bucket,\n Key: fileKey,\n ...customParams,\n }),\n {\n expiresIn: getOr(15 * 60, ['params', 'signedUrlExpires'], config),\n }\n );\n\n return { url };\n },\n uploadStream(file: File, customParams = {}) {\n return upload(file, customParams);\n },\n upload(file: File, customParams = {}) {\n return upload(file, customParams);\n },\n delete(file: File, customParams = {}): Promise<DeleteObjectCommandOutput> {\n const command = new DeleteObjectCommand({\n Bucket: config.params.Bucket,\n Key: getFileKey(file),\n ...customParams,\n });\n return s3Client.send(command);\n },\n };\n },\n};\n"],"names":["index","upload"],"mappings":";;;;AAGA,MAAM,mBAAmB;AAOlB,SAAS,gBAAgB,SAAiB,YAAoB,UAAU,IAAa;AACpF,QAAA,MAAM,IAAI,IAAI,OAAO;AAI3B,MAAI,SAAS;AACJ,WAAA;AAAA,EACT;AAEA,QAAM,EAAE,OAAA,IAAW,oBAAoB,OAAO;AAE9C,MAAI,QAAQ;AACV,WAAO,WAAW;AAAA,EACpB;AAMA,SAAO,IAAI,KAAK,WAAW,GAAG,UAAU,GAAG,KAAK,IAAI,SAAS,SAAS,IAAI,UAAU,GAAG;AACzF;AAWA,SAAS,oBAAoB,SAA6B;AAClD,QAAA,MAAM,IAAI,IAAI,OAAO;AAGvB,MAAA,IAAI,aAAa,OAAO;AAC1B,UAAM,SAAS,IAAI;AAEnB,QAAI,CAAC,QAAQ;AACX,aAAO,EAAE,KAAK,8BAA8B,GAAG,GAAG;AAAA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
|
@@ -1,15 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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.err - if any
|
|
13
|
-
*/
|
|
14
|
-
export declare function getBucketFromUrl(fileUrl: string): BucketInfo;
|
|
15
|
-
export {};
|
|
1
|
+
import type { AwsCredentialIdentity } from '@aws-sdk/types';
|
|
2
|
+
import type { InitOptions } from '.';
|
|
3
|
+
export declare function isUrlFromBucket(fileUrl: string, bucketName: string, baseUrl?: string): boolean;
|
|
4
|
+
export declare const extractCredentials: (options: InitOptions) => AwsCredentialIdentity | null;
|
|
5
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,GAAG,CAAC;AASrC,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,SAAK,GAAG,OAAO,CAoB1F;AA6DD,eAAO,MAAM,kBAAkB,YAAa,WAAW,KAAG,qBAAqB,GAAG,IA0BjF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/provider-upload-aws-s3",
|
|
3
|
-
"version": "0.0.0-next.
|
|
3
|
+
"version": "0.0.0-next.e6eaa3d0563c85f80fd88b258df70a55c057096e",
|
|
4
4
|
"description": "AWS S3 provider for strapi upload",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"upload",
|
|
@@ -30,32 +30,36 @@
|
|
|
30
30
|
}
|
|
31
31
|
],
|
|
32
32
|
"main": "./dist/index.js",
|
|
33
|
+
"module": "./dist/index.mjs",
|
|
34
|
+
"source": "./src/index.ts",
|
|
33
35
|
"types": "./dist/index.d.ts",
|
|
34
36
|
"files": [
|
|
35
37
|
"./dist"
|
|
36
38
|
],
|
|
37
39
|
"scripts": {
|
|
38
|
-
"build": "
|
|
39
|
-
"build:ts": "run -T tsc",
|
|
40
|
-
"watch": "run -T tsc -w --preserveWatchOutput",
|
|
40
|
+
"build": "pack-up build",
|
|
41
41
|
"clean": "run -T rimraf ./dist",
|
|
42
|
-
"
|
|
42
|
+
"lint": "run -T eslint .",
|
|
43
43
|
"test:unit": "run -T jest",
|
|
44
44
|
"test:unit:watch": "run -T jest --watch",
|
|
45
|
-
"
|
|
45
|
+
"watch": "pack-up watch"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"aws-sdk": "
|
|
48
|
+
"@aws-sdk/client-s3": "3.433.0",
|
|
49
|
+
"@aws-sdk/lib-storage": "3.433.0",
|
|
50
|
+
"@aws-sdk/s3-request-presigner": "3.433.0",
|
|
51
|
+
"@aws-sdk/types": "3.433.0",
|
|
49
52
|
"lodash": "4.17.21"
|
|
50
53
|
},
|
|
51
54
|
"devDependencies": {
|
|
52
|
-
"@
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
+
"@strapi/pack-up": "0.0.0-next.e6eaa3d0563c85f80fd88b258df70a55c057096e",
|
|
56
|
+
"@types/jest": "29.5.2",
|
|
57
|
+
"eslint-config-custom": "0.0.0-next.e6eaa3d0563c85f80fd88b258df70a55c057096e",
|
|
58
|
+
"tsconfig": "0.0.0-next.e6eaa3d0563c85f80fd88b258df70a55c057096e"
|
|
55
59
|
},
|
|
56
60
|
"engines": {
|
|
57
|
-
"node": ">=
|
|
61
|
+
"node": ">=18.0.0 <=20.x.x",
|
|
58
62
|
"npm": ">=6.0.0"
|
|
59
63
|
},
|
|
60
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "e6eaa3d0563c85f80fd88b258df70a55c057096e"
|
|
61
65
|
}
|
package/dist/utils.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getBucketFromUrl = void 0;
|
|
4
|
-
const ENDPOINT_PATTERN = /^(.+\.)?s3[.-]([a-z0-9-]+)\./;
|
|
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.err - if any
|
|
13
|
-
*/
|
|
14
|
-
function getBucketFromUrl(fileUrl) {
|
|
15
|
-
const uri = new URL(fileUrl);
|
|
16
|
-
// S3://<bucket-name>/<key>
|
|
17
|
-
if (uri.protocol === 's3:') {
|
|
18
|
-
const bucket = uri.host;
|
|
19
|
-
if (!bucket) {
|
|
20
|
-
return { err: `Invalid S3 URI: no bucket: ${uri}` };
|
|
21
|
-
}
|
|
22
|
-
return { bucket };
|
|
23
|
-
}
|
|
24
|
-
if (!uri.host) {
|
|
25
|
-
return { err: `Invalid S3 URI: no hostname: ${uri}` };
|
|
26
|
-
}
|
|
27
|
-
const matches = uri.host.match(ENDPOINT_PATTERN);
|
|
28
|
-
if (!matches) {
|
|
29
|
-
return { err: `Invalid S3 URI: hostname does not appear to be a valid S3 endpoint: ${uri}` };
|
|
30
|
-
}
|
|
31
|
-
const prefix = matches[1];
|
|
32
|
-
// https://s3.amazonaws.com/<bucket-name>
|
|
33
|
-
if (!prefix) {
|
|
34
|
-
if (uri.pathname === '/') {
|
|
35
|
-
return { bucket: null };
|
|
36
|
-
}
|
|
37
|
-
const index = uri.pathname.indexOf('/', 1);
|
|
38
|
-
// https://s3.amazonaws.com/<bucket-name>
|
|
39
|
-
if (index === -1) {
|
|
40
|
-
return { bucket: uri.pathname.substring(1) };
|
|
41
|
-
}
|
|
42
|
-
// https://s3.amazonaws.com/<bucket-name>/
|
|
43
|
-
if (index === uri.pathname.length - 1) {
|
|
44
|
-
return { bucket: uri.pathname.substring(1, index) };
|
|
45
|
-
}
|
|
46
|
-
// https://s3.amazonaws.com/<bucket-name>/key
|
|
47
|
-
return { bucket: uri.pathname.substring(1, index) };
|
|
48
|
-
}
|
|
49
|
-
// https://<bucket-name>.s3.amazonaws.com/
|
|
50
|
-
return { bucket: prefix.substring(0, prefix.length - 1) };
|
|
51
|
-
}
|
|
52
|
-
exports.getBucketFromUrl = getBucketFromUrl;
|
|
53
|
-
//# sourceMappingURL=utils.js.map
|
package/dist/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA,MAAM,gBAAgB,GAAG,8BAA8B,CAAC;AAOxD;;;;;;;;GAQG;AACH,SAAgB,gBAAgB,CAAC,OAAe;IAC9C,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC;IAE7B,2BAA2B;IAC3B,IAAI,GAAG,CAAC,QAAQ,KAAK,KAAK,EAAE;QAC1B,MAAM,MAAM,GAAG,GAAG,CAAC,IAAI,CAAC;QAExB,IAAI,CAAC,MAAM,EAAE;YACX,OAAO,EAAE,GAAG,EAAE,8BAA8B,GAAG,EAAE,EAAE,CAAC;SACrD;QACD,OAAO,EAAE,MAAM,EAAE,CAAC;KACnB;IAED,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE;QACb,OAAO,EAAE,GAAG,EAAE,gCAAgC,GAAG,EAAE,EAAE,CAAC;KACvD;IAED,MAAM,OAAO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