@strapi/provider-upload-aws-s3 0.0.0-next.f45143c5e2a8a9d85691d0abf79a3f42024a0c71 → 0.0.0-next.f5b09a8e61e059f02784478e27c310c6290be088

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 CHANGED
@@ -70,11 +70,13 @@ module.exports = ({ env }) => ({
70
70
  });
71
71
  ```
72
72
 
73
- ### Configuration for a private S3 bucket
73
+ ### Configuration for a private S3 bucket and signed URLs
74
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.
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 file URLs are signed.
76
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.
77
+ **Note:** If you are using a CDN, the URLs will not be signed.
78
+
79
+ 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
80
 
79
81
  `./config/plugins.js`
80
82
 
@@ -90,7 +92,7 @@ module.exports = ({ env }) => ({
90
92
  region: env('AWS_REGION'),
91
93
  params: {
92
94
  ACL: 'private', // <== set ACL to private
93
- signedUrlExpires: env('AWS_SIGNED_URL_EXPIRES', 60 * 60 * 24 * 7),
95
+ signedUrlExpires: env('AWS_SIGNED_URL_EXPIRES', 15 * 60),
94
96
  Bucket: env('AWS_BUCKET'),
95
97
  },
96
98
  },
@@ -150,14 +152,14 @@ module.exports = [
150
152
  "'self'",
151
153
  'data:',
152
154
  'blob:',
153
- 'dl.airtable.com',
155
+ 'market-assets.strapi.io',
154
156
  'yourBucketName.s3.yourRegion.amazonaws.com',
155
157
  ],
156
158
  'media-src': [
157
159
  "'self'",
158
160
  'data:',
159
161
  'blob:',
160
- 'dl.airtable.com',
162
+ 'market-assets.strapi.io',
161
163
  'yourBucketName.s3.yourRegion.amazonaws.com',
162
164
  ],
163
165
  upgradeInsecureRequests: null,
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import type { ReadStream } from 'node:fs';
3
4
  import AWS from 'aws-sdk';
4
5
  interface File {
@@ -42,4 +43,5 @@ declare const _default: {
42
43
  delete(file: File, customParams?: {}): Promise<void>;
43
44
  };
44
45
  };
45
- export = _default;
46
+ export default _default;
47
+ //# 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,GAAG,MAAM,SAAS,CAAC;AAG1B,UAAU,IAAI;IACZ,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,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC5C,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAWD,UAAU,WAAY,SAAQ,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,mBAAmB,CAAC;IAC/D,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,GAAG,CAAC,EAAE,CAAC,mBAAmB,GAAG;QACtC,MAAM,EAAE;YACN,MAAM,EAAE,MAAM,CAAC;YACf,GAAG,CAAC,EAAE,MAAM,CAAC;YACb,gBAAgB,CAAC,EAAE,MAAM,CAAC;SAC3B,CAAC;KACH,CAAC;CACH;;+DAG4D,WAAW;;2BAkEzC,IAAI,GAAG,QAAQ;YAAE,GAAG,EAAE,MAAM,CAAA;SAAE,CAAC;2BA2BrC,IAAI;qBAGV,IAAI;qBAGJ,IAAI,sBAAsB,QAAQ,IAAI,CAAC;;;AApG1D,wBA0HE"}
package/dist/index.js CHANGED
@@ -1,110 +1,154 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- const fp_1 = require("lodash/fp");
6
- const aws_sdk_1 = __importDefault(require("aws-sdk"));
7
- const utils_1 = require("./utils");
8
- // TODO V5: Migrate to aws-sdk v3
9
- // eslint-disable-next-line @typescript-eslint/no-var-requires
10
- require('aws-sdk/lib/maintenance_mode_message').suppress = true;
11
- function assertUrlProtocol(url) {
12
- // Regex to test protocol like "http://", "https://"
13
- return /^\w*:\/\//.test(url);
2
+ const fp = require("lodash/fp");
3
+ const AWS = require("aws-sdk");
4
+ const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
5
+ const AWS__default = /* @__PURE__ */ _interopDefault(AWS);
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
- module.exports = {
16
- init({ baseUrl, rootPath, s3Options, ...legacyS3Options }) {
17
- if (Object.keys(legacyS3Options).length > 0) {
18
- process.emitWarning("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.");
50
+ require("aws-sdk/lib/maintenance_mode_message").suppress = true;
51
+ function hasUrlProtocol(url) {
52
+ return /^\w*:\/\//.test(url);
53
+ }
54
+ const index = {
55
+ init({ baseUrl, rootPath, s3Options, ...legacyS3Options }) {
56
+ if (Object.keys(legacyS3Options).length > 0) {
57
+ process.emitWarning(
58
+ "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."
59
+ );
60
+ }
61
+ const config = { ...s3Options, ...legacyS3Options };
62
+ const S3 = new AWS__default.default.S3({
63
+ apiVersion: "2006-03-01",
64
+ ...config
65
+ });
66
+ const filePrefix = rootPath ? `${rootPath.replace(/\/+$/, "")}/` : "";
67
+ const getFileKey = (file) => {
68
+ const path = file.path ? `${file.path}/` : "";
69
+ return `${filePrefix}${path}${file.hash}${file.ext}`;
70
+ };
71
+ const ACL = fp.getOr("public-read", ["params", "ACL"], config);
72
+ const upload = (file, customParams = {}) => new Promise((resolve, reject) => {
73
+ const fileKey = getFileKey(file);
74
+ if (!file.stream && !file.buffer) {
75
+ reject(new Error("Missing file stream or buffer"));
76
+ return;
77
+ }
78
+ const params = {
79
+ Key: fileKey,
80
+ Bucket: config.params.Bucket,
81
+ Body: file.stream || file.buffer,
82
+ ACL,
83
+ ContentType: file.mime,
84
+ ...customParams
85
+ };
86
+ const onUploaded = (err, data) => {
87
+ if (err) {
88
+ return reject(err);
19
89
  }
20
- const config = { ...s3Options, ...legacyS3Options };
21
- const S3 = new aws_sdk_1.default.S3({
22
- apiVersion: '2006-03-01',
23
- ...config,
24
- });
25
- const filePrefix = rootPath ? `${rootPath.replace(/\/+$/, '')}/` : '';
26
- const getFileKey = (file) => {
27
- const path = file.path ? `${file.path}/` : '';
28
- return `${filePrefix}${path}${file.hash}${file.ext}`;
29
- };
30
- const ACL = (0, fp_1.getOr)('public-read', ['params', 'ACL'], config);
31
- const upload = (file, customParams = {}) => new Promise((resolve, reject) => {
32
- const fileKey = getFileKey(file);
33
- if (!file.stream && !file.buffer) {
34
- reject(new Error('Missing file stream or buffer'));
35
- return;
90
+ if (baseUrl) {
91
+ file.url = `${baseUrl}/${fileKey}`;
92
+ } else {
93
+ file.url = hasUrlProtocol(data.Location) ? data.Location : `https://${data.Location}`;
94
+ }
95
+ resolve();
96
+ };
97
+ S3.upload(params, onUploaded);
98
+ });
99
+ return {
100
+ isPrivate() {
101
+ return ACL === "private";
102
+ },
103
+ async getSignedUrl(file) {
104
+ if (!isUrlFromBucket(file.url, config.params.Bucket, baseUrl)) {
105
+ return { url: file.url };
106
+ }
107
+ const signedUrlExpires = fp.getOr(15 * 60, ["params", "signedUrlExpires"], config);
108
+ return new Promise((resolve, reject) => {
109
+ const fileKey = getFileKey(file);
110
+ S3.getSignedUrl(
111
+ "getObject",
112
+ {
113
+ Bucket: config.params.Bucket,
114
+ Key: fileKey,
115
+ Expires: parseInt(signedUrlExpires, 10)
116
+ },
117
+ (err, url) => {
118
+ if (err) {
119
+ return reject(err);
120
+ }
121
+ resolve({ url });
36
122
  }
37
- const params = {
38
- Key: fileKey,
39
- Bucket: config.params.Bucket,
40
- Body: file.stream || file.buffer,
41
- ACL,
42
- ContentType: file.mime,
43
- ...customParams,
44
- };
45
- const onUploaded = (err, data) => {
46
- if (err) {
47
- return reject(err);
48
- }
49
- // set the bucket file url
50
- if (assertUrlProtocol(data.Location)) {
51
- file.url = baseUrl ? `${baseUrl}/${fileKey}` : data.Location;
52
- }
53
- else {
54
- // Default protocol to https protocol
55
- file.url = `https://${data.Location}`;
56
- }
57
- resolve();
58
- };
59
- S3.upload(params, onUploaded);
123
+ );
60
124
  });
61
- return {
62
- isPrivate() {
63
- return ACL === 'private';
125
+ },
126
+ uploadStream(file, customParams = {}) {
127
+ return upload(file, customParams);
128
+ },
129
+ upload(file, customParams = {}) {
130
+ return upload(file, customParams);
131
+ },
132
+ delete(file, customParams = {}) {
133
+ return new Promise((resolve, reject) => {
134
+ const fileKey = getFileKey(file);
135
+ S3.deleteObject(
136
+ {
137
+ Key: fileKey,
138
+ Bucket: config.params.Bucket,
139
+ ...customParams
64
140
  },
65
- async getSignedUrl(file) {
66
- // Do not sign the url if it does not come from the same bucket.
67
- const { bucket } = (0, utils_1.getBucketFromUrl)(file.url);
68
- if (bucket !== config.params.Bucket) {
69
- return { url: file.url };
70
- }
71
- return new Promise((resolve, reject) => {
72
- const fileKey = getFileKey(file);
73
- S3.getSignedUrl('getObject', {
74
- Bucket: config.params.Bucket,
75
- Key: fileKey,
76
- Expires: (0, fp_1.getOr)(15 * 60, ['params', 'signedUrlExpires'], config), // 15 minutes
77
- }, (err, url) => {
78
- if (err) {
79
- return reject(err);
80
- }
81
- resolve({ url });
82
- });
83
- });
84
- },
85
- uploadStream(file, customParams = {}) {
86
- return upload(file, customParams);
87
- },
88
- upload(file, customParams = {}) {
89
- return upload(file, customParams);
90
- },
91
- delete(file, customParams = {}) {
92
- return new Promise((resolve, reject) => {
93
- // delete file on S3 bucket
94
- const fileKey = getFileKey(file);
95
- S3.deleteObject({
96
- Key: fileKey,
97
- Bucket: config.params.Bucket,
98
- ...customParams,
99
- }, (err) => {
100
- if (err) {
101
- return reject(err);
102
- }
103
- resolve();
104
- });
105
- });
106
- },
107
- };
108
- },
141
+ (err) => {
142
+ if (err) {
143
+ return reject(err);
144
+ }
145
+ resolve();
146
+ }
147
+ );
148
+ });
149
+ }
150
+ };
151
+ }
109
152
  };
110
- //# sourceMappingURL=index.js.map
153
+ module.exports = index;
154
+ //# 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,iBAAiB,CAAC,GAAW;IACpC,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,UA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CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;wBACX,IAAI,GAAG,EAAE;4BACP,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":["const 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","import type { ReadStream } from 'node:fs';\nimport { getOr } from 'lodash/fp';\nimport AWS from 'aws-sdk';\nimport { isUrlFromBucket } from './utils';\n\ninterface 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 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\n// TODO V5: Migrate to aws-sdk v3\n// eslint-disable-next-line @typescript-eslint/no-var-requires\nrequire('aws-sdk/lib/maintenance_mode_message').suppress = true;\n\nfunction hasUrlProtocol(url: string) {\n // Regex to test protocol like \"http://\", \"https://\"\n return /^\\w*:\\/\\//.test(url);\n}\n\ninterface InitOptions extends Partial<AWS.S3.ClientConfiguration> {\n baseUrl?: string;\n rootPath?: string;\n s3Options: AWS.S3.ClientConfiguration & {\n params: {\n Bucket: string; // making it required\n ACL?: string;\n signedUrlExpires?: string;\n };\n };\n}\n\nexport default {\n init({ 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\n const config = { ...s3Options, ...legacyS3Options };\n\n const S3 = new AWS.S3({\n apiVersion: '2006-03-01',\n ...config,\n });\n\n const filePrefix = rootPath ? `${rootPath.replace(/\\/+$/, '')}/` : '';\n\n const getFileKey = (file: File) => {\n const path = file.path ? `${file.path}/` : '';\n\n return `${filePrefix}${path}${file.hash}${file.ext}`;\n };\n\n const ACL = getOr('public-read', ['params', 'ACL'], config);\n\n const upload = (file: File, customParams = {}): Promise<void> =>\n new Promise((resolve, reject) => {\n const fileKey = getFileKey(file);\n\n if (!file.stream && !file.buffer) {\n reject(new Error('Missing file stream or buffer'));\n return;\n }\n\n const params = {\n Key: fileKey,\n Bucket: config.params.Bucket,\n Body: file.stream || file.buffer,\n ACL,\n ContentType: file.mime,\n ...customParams,\n };\n\n const onUploaded = (err: Error, data: AWS.S3.ManagedUpload.SendData) => {\n if (err) {\n return reject(err);\n }\n\n // set the bucket file url\n if (baseUrl) {\n // Construct the url with the baseUrl\n file.url = `${baseUrl}/${fileKey}`;\n } else {\n // Add the protocol if it is missing\n // Some providers like DigitalOcean Spaces return the url without the protocol\n file.url = hasUrlProtocol(data.Location) ? data.Location : `https://${data.Location}`;\n }\n resolve();\n };\n\n S3.upload(params, onUploaded);\n });\n\n return {\n isPrivate() {\n return ACL === 'private';\n },\n async getSignedUrl(file: File): 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\n const signedUrlExpires: string = getOr(15 * 60, ['params', 'signedUrlExpires'], config); // 15 minutes\n\n return new Promise((resolve, reject) => {\n const fileKey = getFileKey(file);\n\n S3.getSignedUrl(\n 'getObject',\n {\n Bucket: config.params.Bucket,\n Key: fileKey,\n Expires: parseInt(signedUrlExpires, 10),\n },\n (err, url) => {\n if (err) {\n return reject(err);\n }\n resolve({ url });\n }\n );\n });\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<void> {\n return new Promise((resolve, reject) => {\n // delete file on S3 bucket\n const fileKey = getFileKey(file);\n S3.deleteObject(\n {\n Key: fileKey,\n Bucket: config.params.Bucket,\n ...customParams,\n },\n (err) => {\n if (err) {\n return reject(err);\n }\n\n resolve();\n }\n );\n });\n },\n };\n },\n};\n"],"names":["index","AWS","getOr"],"mappings":";;;;;AAAA,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,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F;;"}
package/dist/index.mjs ADDED
@@ -0,0 +1,153 @@
1
+ import { getOr } from "lodash/fp";
2
+ import AWS from "aws-sdk";
3
+ const ENDPOINT_PATTERN = /^(.+\.)?s3[.-]([a-z0-9-]+)\./;
4
+ function isUrlFromBucket(fileUrl, bucketName, baseUrl = "") {
5
+ const url = new URL(fileUrl);
6
+ if (baseUrl) {
7
+ return false;
8
+ }
9
+ const { bucket } = getBucketFromAwsUrl(fileUrl);
10
+ if (bucket) {
11
+ return bucket === bucketName;
12
+ }
13
+ return url.host.startsWith(`${bucketName}.`) || url.pathname.includes(`/${bucketName}/`);
14
+ }
15
+ function getBucketFromAwsUrl(fileUrl) {
16
+ const url = new URL(fileUrl);
17
+ if (url.protocol === "s3:") {
18
+ const bucket = url.host;
19
+ if (!bucket) {
20
+ return { err: `Invalid S3 url: no bucket: ${url}` };
21
+ }
22
+ return { bucket };
23
+ }
24
+ if (!url.host) {
25
+ return { err: `Invalid S3 url: no hostname: ${url}` };
26
+ }
27
+ const matches = url.host.match(ENDPOINT_PATTERN);
28
+ if (!matches) {
29
+ return { err: `Invalid S3 url: hostname does not appear to be a valid S3 endpoint: ${url}` };
30
+ }
31
+ const prefix = matches[1];
32
+ if (!prefix) {
33
+ if (url.pathname === "/") {
34
+ return { bucket: null };
35
+ }
36
+ const index2 = url.pathname.indexOf("/", 1);
37
+ if (index2 === -1) {
38
+ return { bucket: url.pathname.substring(1) };
39
+ }
40
+ if (index2 === url.pathname.length - 1) {
41
+ return { bucket: url.pathname.substring(1, index2) };
42
+ }
43
+ return { bucket: url.pathname.substring(1, index2) };
44
+ }
45
+ return { bucket: prefix.substring(0, prefix.length - 1) };
46
+ }
47
+ require("aws-sdk/lib/maintenance_mode_message").suppress = true;
48
+ function hasUrlProtocol(url) {
49
+ return /^\w*:\/\//.test(url);
50
+ }
51
+ const index = {
52
+ init({ baseUrl, rootPath, s3Options, ...legacyS3Options }) {
53
+ if (Object.keys(legacyS3Options).length > 0) {
54
+ process.emitWarning(
55
+ "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."
56
+ );
57
+ }
58
+ const config = { ...s3Options, ...legacyS3Options };
59
+ const S3 = new AWS.S3({
60
+ apiVersion: "2006-03-01",
61
+ ...config
62
+ });
63
+ const filePrefix = rootPath ? `${rootPath.replace(/\/+$/, "")}/` : "";
64
+ const getFileKey = (file) => {
65
+ const path = file.path ? `${file.path}/` : "";
66
+ return `${filePrefix}${path}${file.hash}${file.ext}`;
67
+ };
68
+ const ACL = getOr("public-read", ["params", "ACL"], config);
69
+ const upload = (file, customParams = {}) => new Promise((resolve, reject) => {
70
+ const fileKey = getFileKey(file);
71
+ if (!file.stream && !file.buffer) {
72
+ reject(new Error("Missing file stream or buffer"));
73
+ return;
74
+ }
75
+ const params = {
76
+ Key: fileKey,
77
+ Bucket: config.params.Bucket,
78
+ Body: file.stream || file.buffer,
79
+ ACL,
80
+ ContentType: file.mime,
81
+ ...customParams
82
+ };
83
+ const onUploaded = (err, data) => {
84
+ if (err) {
85
+ return reject(err);
86
+ }
87
+ if (baseUrl) {
88
+ file.url = `${baseUrl}/${fileKey}`;
89
+ } else {
90
+ file.url = hasUrlProtocol(data.Location) ? data.Location : `https://${data.Location}`;
91
+ }
92
+ resolve();
93
+ };
94
+ S3.upload(params, onUploaded);
95
+ });
96
+ return {
97
+ isPrivate() {
98
+ return ACL === "private";
99
+ },
100
+ async getSignedUrl(file) {
101
+ if (!isUrlFromBucket(file.url, config.params.Bucket, baseUrl)) {
102
+ return { url: file.url };
103
+ }
104
+ const signedUrlExpires = getOr(15 * 60, ["params", "signedUrlExpires"], config);
105
+ return new Promise((resolve, reject) => {
106
+ const fileKey = getFileKey(file);
107
+ S3.getSignedUrl(
108
+ "getObject",
109
+ {
110
+ Bucket: config.params.Bucket,
111
+ Key: fileKey,
112
+ Expires: parseInt(signedUrlExpires, 10)
113
+ },
114
+ (err, url) => {
115
+ if (err) {
116
+ return reject(err);
117
+ }
118
+ resolve({ url });
119
+ }
120
+ );
121
+ });
122
+ },
123
+ uploadStream(file, customParams = {}) {
124
+ return upload(file, customParams);
125
+ },
126
+ upload(file, customParams = {}) {
127
+ return upload(file, customParams);
128
+ },
129
+ delete(file, customParams = {}) {
130
+ return new Promise((resolve, reject) => {
131
+ const fileKey = getFileKey(file);
132
+ S3.deleteObject(
133
+ {
134
+ Key: fileKey,
135
+ Bucket: config.params.Bucket,
136
+ ...customParams
137
+ },
138
+ (err) => {
139
+ if (err) {
140
+ return reject(err);
141
+ }
142
+ resolve();
143
+ }
144
+ );
145
+ });
146
+ }
147
+ };
148
+ }
149
+ };
150
+ export {
151
+ index as default
152
+ };
153
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":["const 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","import type { ReadStream } from 'node:fs';\nimport { getOr } from 'lodash/fp';\nimport AWS from 'aws-sdk';\nimport { isUrlFromBucket } from './utils';\n\ninterface 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 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\n// TODO V5: Migrate to aws-sdk v3\n// eslint-disable-next-line @typescript-eslint/no-var-requires\nrequire('aws-sdk/lib/maintenance_mode_message').suppress = true;\n\nfunction hasUrlProtocol(url: string) {\n // Regex to test protocol like \"http://\", \"https://\"\n return /^\\w*:\\/\\//.test(url);\n}\n\ninterface InitOptions extends Partial<AWS.S3.ClientConfiguration> {\n baseUrl?: string;\n rootPath?: string;\n s3Options: AWS.S3.ClientConfiguration & {\n params: {\n Bucket: string; // making it required\n ACL?: string;\n signedUrlExpires?: string;\n };\n };\n}\n\nexport default {\n init({ 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\n const config = { ...s3Options, ...legacyS3Options };\n\n const S3 = new AWS.S3({\n apiVersion: '2006-03-01',\n ...config,\n });\n\n const filePrefix = rootPath ? `${rootPath.replace(/\\/+$/, '')}/` : '';\n\n const getFileKey = (file: File) => {\n const path = file.path ? `${file.path}/` : '';\n\n return `${filePrefix}${path}${file.hash}${file.ext}`;\n };\n\n const ACL = getOr('public-read', ['params', 'ACL'], config);\n\n const upload = (file: File, customParams = {}): Promise<void> =>\n new Promise((resolve, reject) => {\n const fileKey = getFileKey(file);\n\n if (!file.stream && !file.buffer) {\n reject(new Error('Missing file stream or buffer'));\n return;\n }\n\n const params = {\n Key: fileKey,\n Bucket: config.params.Bucket,\n Body: file.stream || file.buffer,\n ACL,\n ContentType: file.mime,\n ...customParams,\n };\n\n const onUploaded = (err: Error, data: AWS.S3.ManagedUpload.SendData) => {\n if (err) {\n return reject(err);\n }\n\n // set the bucket file url\n if (baseUrl) {\n // Construct the url with the baseUrl\n file.url = `${baseUrl}/${fileKey}`;\n } else {\n // Add the protocol if it is missing\n // Some providers like DigitalOcean Spaces return the url without the protocol\n file.url = hasUrlProtocol(data.Location) ? data.Location : `https://${data.Location}`;\n }\n resolve();\n };\n\n S3.upload(params, onUploaded);\n });\n\n return {\n isPrivate() {\n return ACL === 'private';\n },\n async getSignedUrl(file: File): 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\n const signedUrlExpires: string = getOr(15 * 60, ['params', 'signedUrlExpires'], config); // 15 minutes\n\n return new Promise((resolve, reject) => {\n const fileKey = getFileKey(file);\n\n S3.getSignedUrl(\n 'getObject',\n {\n Bucket: config.params.Bucket,\n Key: fileKey,\n Expires: parseInt(signedUrlExpires, 10),\n },\n (err, url) => {\n if (err) {\n return reject(err);\n }\n resolve({ url });\n }\n );\n });\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<void> {\n return new Promise((resolve, reject) => {\n // delete file on S3 bucket\n const fileKey = getFileKey(file);\n S3.deleteObject(\n {\n Key: fileKey,\n Bucket: config.params.Bucket,\n ...customParams,\n },\n (err) => {\n if (err) {\n return reject(err);\n }\n\n resolve();\n }\n );\n });\n },\n };\n },\n};\n"],"names":["index"],"mappings":";;AAAA,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,IACpD;AACA,WAAO,EAAE,OAAO;AAAA,EAClB;AAEI,MAAA,CAAC,IAAI,MAAM;AACb,WAAO,EAAE,KAAK,gCAAgC,GAAG,GAAG;AAAA,EACtD;AAEA,QAAM,UAAU,IAAI,KAAK,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YACxC;AAAA,YACA,CAAC,KAAK,QAAQ;AACZ,kBAAI,KAAK;AACP,uBAAO,OAAO,GAAG;AAAA,cACnB;AACQ,sBAAA,EAAE,KAAK;AAAA,YACjB;AAAA,UAAA;AAAA,QACF,CACD;AAAA,MACH;AAAA,MACA,aAAa,MAAY,eAAe,IAAI;AACnC,eAAA,OAAO,MAAM,YAAY;AAAA,MAClC;AAAA,MACA,OAAO,MAAY,eAAe,IAAI;AAC7B,eAAA,OAAO,MAAM,YAAY;AAAA,MAClC;AAAA,MACA,OAAO,MAAY,eAAe,IAAmB;AACnD,eAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AAEhC,gBAAA,UAAU,WAAW,IAAI;AAC5B,aAAA;AAAA,YACD;AAAA,cACE,KAAK;AAAA,cACL,QAAQ,OAAO,OAAO;AAAA,cACtB,GAAG;AAAA,YACL;AAAA,YACA,CAAC,QAAQ;AACP,kBAAI,KAAK;AACP,uBAAO,OAAO,GAAG;AAAA,cACnB;AAEQ;YACV;AAAA,UAAA;AAAA,QACF,CACD;AAAA,MACH;AAAA,IAAA;AAAA,EAEJ;AACF;"}
package/dist/utils.d.ts CHANGED
@@ -1,15 +1,2 @@
1
- interface BucketInfo {
2
- bucket?: string | null;
3
- err?: string;
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
+ export declare function isUrlFromBucket(fileUrl: string, bucketName: string, baseUrl?: string): boolean;
2
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAOA,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,SAAK,GAAG,OAAO,CAoB1F"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/provider-upload-aws-s3",
3
- "version": "0.0.0-next.f45143c5e2a8a9d85691d0abf79a3f42024a0c71",
3
+ "version": "0.0.0-next.f5b09a8e61e059f02784478e27c310c6290be088",
4
4
  "description": "AWS S3 provider for strapi upload",
5
5
  "keywords": [
6
6
  "upload",
@@ -30,32 +30,34 @@
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": "run -T tsc",
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
+ "lint": "run -T eslint .",
42
43
  "prepublishOnly": "yarn clean && yarn build",
43
44
  "test:unit": "run -T jest",
44
45
  "test:unit:watch": "run -T jest --watch",
45
- "lint": "run -T eslint ."
46
+ "watch": "pack-up watch"
46
47
  },
47
48
  "dependencies": {
48
- "aws-sdk": "2.1351.0",
49
+ "aws-sdk": "2.1472.0",
49
50
  "lodash": "4.17.21"
50
51
  },
51
52
  "devDependencies": {
52
- "@types/jest": "29.2.0",
53
- "eslint-config-custom": "0.0.0-next.f45143c5e2a8a9d85691d0abf79a3f42024a0c71",
54
- "tsconfig": "0.0.0-next.f45143c5e2a8a9d85691d0abf79a3f42024a0c71"
53
+ "@strapi/pack-up": "0.0.0-next.f5b09a8e61e059f02784478e27c310c6290be088",
54
+ "@types/jest": "29.5.2",
55
+ "eslint-config-custom": "0.0.0-next.f5b09a8e61e059f02784478e27c310c6290be088",
56
+ "tsconfig": "0.0.0-next.f5b09a8e61e059f02784478e27c310c6290be088"
55
57
  },
56
58
  "engines": {
57
- "node": ">=14.19.1 <=18.x.x",
59
+ "node": ">=18.0.0 <=20.x.x",
58
60
  "npm": ">=6.0.0"
59
61
  },
60
- "gitHead": "f45143c5e2a8a9d85691d0abf79a3f42024a0c71"
62
+ "gitHead": "f5b09a8e61e059f02784478e27c310c6290be088"
61
63
  }
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,GAAG,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAC;IACjD,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,EAAE,GAAG,EAAE,uEAAuE,GAAG,EAAE,EAAE,CAAC;KAC9F;IAED,MAAM,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IAC1B,yCAAyC;IACzC,IAAI,CAAC,MAAM,EAAE;QACX,IAAI,GAAG,CAAC,QAAQ,KAAK,GAAG,EAAE;YACxB,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;SACzB;QAED,MAAM,KAAK,GAAG,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,EAAE,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