@strapi/provider-upload-aws-s3 4.9.0-exp.90df253ba90fd6879eb56a720a1f80d04ff745b8 → 4.10.0-beta.0

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.
Files changed (3) hide show
  1. package/README.md +12 -6
  2. package/lib/index.js +23 -8
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -29,6 +29,8 @@ npm install @strapi/provider-upload-aws-s3 --save
29
29
 
30
30
  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
31
 
32
+ 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.
33
+
32
34
  ### Provider Configuration
33
35
 
34
36
  `./config/plugins.js` or `./config/plugins.ts` for TypeScript projects:
@@ -40,12 +42,16 @@ module.exports = ({ env }) => ({
40
42
  config: {
41
43
  provider: 'aws-s3',
42
44
  providerOptions: {
43
- accessKeyId: env('AWS_ACCESS_KEY_ID'),
44
- secretAccessKey: env('AWS_ACCESS_SECRET'),
45
- region: env('AWS_REGION'),
46
- params: {
47
- Bucket: env('AWS_BUCKET'),
48
- },
45
+ baseUrl: env('CDN_URL'),
46
+ rootPath: env('CDN_ROOT_PATH'),
47
+ s3Options: {
48
+ accessKeyId: env('AWS_ACCESS_KEY_ID'),
49
+ secretAccessKey: env('AWS_ACCESS_SECRET'),
50
+ region: env('AWS_REGION'),
51
+ params: {
52
+ Bucket: env('AWS_BUCKET'),
53
+ },
54
+ }
49
55
  },
50
56
  actionOptions: {
51
57
  upload: {},
package/lib/index.js CHANGED
@@ -15,19 +15,34 @@ function assertUrlProtocol(url) {
15
15
  }
16
16
 
17
17
  module.exports = {
18
- init(config) {
18
+ init({ baseUrl = null, rootPath = null, s3Options, ...legacyS3Options }) {
19
+ if (legacyS3Options) {
20
+ process.emitWarning(
21
+ "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."
22
+ );
23
+ }
24
+
19
25
  const S3 = new AWS.S3({
20
26
  apiVersion: '2006-03-01',
21
- ...config,
27
+ ...s3Options,
28
+ ...legacyS3Options,
22
29
  });
23
30
 
31
+ const filePrefix = rootPath ? `${rootPath.replace(/\/+$/, '')}/` : '';
32
+
33
+ const getFileKey = (file) => {
34
+ const path = file.path ? `${file.path}/` : '';
35
+
36
+ return `${filePrefix}${path}${file.hash}${file.ext}`;
37
+ };
38
+
24
39
  const upload = (file, customParams = {}) =>
25
40
  new Promise((resolve, reject) => {
26
41
  // upload file on S3 bucket
27
- const path = file.path ? `${file.path}/` : '';
42
+ const fileKey = getFileKey(file);
28
43
  S3.upload(
29
44
  {
30
- Key: `${path}${file.hash}${file.ext}`,
45
+ Key: fileKey,
31
46
  Body: file.stream || Buffer.from(file.buffer, 'binary'),
32
47
  ACL: 'public-read',
33
48
  ContentType: file.mime,
@@ -40,7 +55,7 @@ module.exports = {
40
55
 
41
56
  // set the bucket file url
42
57
  if (assertUrlProtocol(data.Location)) {
43
- file.url = data.Location;
58
+ file.url = baseUrl ? `${baseUrl}/${fileKey}` : data.Location;
44
59
  } else {
45
60
  // Default protocol to https protocol
46
61
  file.url = `https://${data.Location}`;
@@ -61,13 +76,13 @@ module.exports = {
61
76
  delete(file, customParams = {}) {
62
77
  return new Promise((resolve, reject) => {
63
78
  // delete file on S3 bucket
64
- const path = file.path ? `${file.path}/` : '';
79
+ const fileKey = getFileKey(file);
65
80
  S3.deleteObject(
66
81
  {
67
- Key: `${path}${file.hash}${file.ext}`,
82
+ Key: fileKey,
68
83
  ...customParams,
69
84
  },
70
- (err, data) => {
85
+ (err) => {
71
86
  if (err) {
72
87
  return reject(err);
73
88
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/provider-upload-aws-s3",
3
- "version": "4.9.0-exp.90df253ba90fd6879eb56a720a1f80d04ff745b8",
3
+ "version": "4.10.0-beta.0",
4
4
  "description": "AWS S3 provider for strapi upload",
5
5
  "keywords": [
6
6
  "upload",
@@ -44,5 +44,5 @@
44
44
  "node": ">=14.19.1 <=18.x.x",
45
45
  "npm": ">=6.0.0"
46
46
  },
47
- "gitHead": "366eb8a0d0f06935914854c6d9c4b3fe859468e0"
47
+ "gitHead": "1519ef0e56d27b738f24fc88223797651ad47aaf"
48
48
  }