@payloadcms/storage-s3 3.24.0 → 3.25.0-canary.3d5c763
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 +1 -0
- package/dist/client/S3ClientUploadHandler.d.ts +8 -0
- package/dist/client/S3ClientUploadHandler.d.ts.map +1 -0
- package/dist/client/S3ClientUploadHandler.js +26 -0
- package/dist/client/S3ClientUploadHandler.js.map +1 -0
- package/dist/exports/client.d.ts +2 -0
- package/dist/exports/client.d.ts.map +1 -0
- package/dist/exports/client.js +3 -0
- package/dist/exports/client.js.map +1 -0
- package/dist/generateSignedURL.d.ts +14 -0
- package/dist/generateSignedURL.d.ts.map +1 -0
- package/dist/generateSignedURL.js +39 -0
- package/dist/generateSignedURL.js.map +1 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +26 -10
- package/dist/index.js.map +1 -1
- package/package.json +10 -4
package/README.md
CHANGED
|
@@ -15,6 +15,7 @@ pnpm add @payloadcms/storage-s3
|
|
|
15
15
|
- Configure the `collections` object to specify which collections should use the AWS S3 adapter. The slug _must_ match one of your existing collection slugs.
|
|
16
16
|
- The `config` object can be any [`S3ClientConfig`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/s3) object (from [`@aws-sdk/client-s3`](https://github.com/aws/aws-sdk-js-v3)). _This is highly dependent on your AWS setup_. Check the AWS documentation for more information.
|
|
17
17
|
- When enabled, this package will automatically set `disableLocalStorage` to `true` for each collection.
|
|
18
|
+
- When deploying to Vercel, server uploads are limited with 4.5MB. Set `clientUploads` to `true` to do uploads directly on the client. You must allow CORS PUT method for the bucket to your website.
|
|
18
19
|
|
|
19
20
|
```ts
|
|
20
21
|
import { s3Storage } from '@payloadcms/storage-s3'
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const S3ClientUploadHandler: ({ children, collectionSlug, enabled, extra, serverHandlerPath, }: {
|
|
2
|
+
children: import("react").ReactNode;
|
|
3
|
+
collectionSlug: import("payload").UploadCollectionSlug;
|
|
4
|
+
enabled?: boolean;
|
|
5
|
+
extra: Record<string, unknown>;
|
|
6
|
+
serverHandlerPath: string;
|
|
7
|
+
}) => import("react").JSX.Element;
|
|
8
|
+
//# sourceMappingURL=S3ClientUploadHandler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"S3ClientUploadHandler.d.ts","sourceRoot":"","sources":["../../src/client/S3ClientUploadHandler.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,qBAAqB;;;;;;aAqB4B,OAAO,aADnE,CAAA"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { createClientUploadHandler } from '@payloadcms/plugin-cloud-storage/client';
|
|
3
|
+
export const S3ClientUploadHandler = createClientUploadHandler({
|
|
4
|
+
handler: async ({ apiRoute, collectionSlug, file, serverHandlerPath, serverURL })=>{
|
|
5
|
+
const response = await fetch(`${serverURL}${apiRoute}${serverHandlerPath}`, {
|
|
6
|
+
body: JSON.stringify({
|
|
7
|
+
collectionSlug,
|
|
8
|
+
filename: file.name,
|
|
9
|
+
mimeType: file.type
|
|
10
|
+
}),
|
|
11
|
+
credentials: 'include',
|
|
12
|
+
method: 'POST'
|
|
13
|
+
});
|
|
14
|
+
const { url } = await response.json();
|
|
15
|
+
await fetch(url, {
|
|
16
|
+
body: file,
|
|
17
|
+
headers: {
|
|
18
|
+
'Content-Length': file.size.toString(),
|
|
19
|
+
'Content-Type': file.type
|
|
20
|
+
},
|
|
21
|
+
method: 'PUT'
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=S3ClientUploadHandler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client/S3ClientUploadHandler.ts"],"sourcesContent":["'use client'\nimport { createClientUploadHandler } from '@payloadcms/plugin-cloud-storage/client'\n\nexport const S3ClientUploadHandler = createClientUploadHandler({\n handler: async ({ apiRoute, collectionSlug, file, serverHandlerPath, serverURL }) => {\n const response = await fetch(`${serverURL}${apiRoute}${serverHandlerPath}`, {\n body: JSON.stringify({\n collectionSlug,\n filename: file.name,\n mimeType: file.type,\n }),\n credentials: 'include',\n method: 'POST',\n })\n\n const { url } = await response.json()\n\n await fetch(url, {\n body: file,\n headers: { 'Content-Length': file.size.toString(), 'Content-Type': file.type },\n method: 'PUT',\n })\n },\n})\n"],"names":["createClientUploadHandler","S3ClientUploadHandler","handler","apiRoute","collectionSlug","file","serverHandlerPath","serverURL","response","fetch","body","JSON","stringify","filename","name","mimeType","type","credentials","method","url","json","headers","size","toString"],"mappings":"AAAA;AACA,SAASA,yBAAyB,QAAQ,0CAAyC;AAEnF,OAAO,MAAMC,wBAAwBD,0BAA0B;IAC7DE,SAAS,OAAO,EAAEC,QAAQ,EAAEC,cAAc,EAAEC,IAAI,EAAEC,iBAAiB,EAAEC,SAAS,EAAE;QAC9E,MAAMC,WAAW,MAAMC,MAAM,GAAGF,YAAYJ,WAAWG,mBAAmB,EAAE;YAC1EI,MAAMC,KAAKC,SAAS,CAAC;gBACnBR;gBACAS,UAAUR,KAAKS,IAAI;gBACnBC,UAAUV,KAAKW,IAAI;YACrB;YACAC,aAAa;YACbC,QAAQ;QACV;QAEA,MAAM,EAAEC,GAAG,EAAE,GAAG,MAAMX,SAASY,IAAI;QAEnC,MAAMX,MAAMU,KAAK;YACfT,MAAML;YACNgB,SAAS;gBAAE,kBAAkBhB,KAAKiB,IAAI,CAACC,QAAQ;gBAAI,gBAAgBlB,KAAKW,IAAI;YAAC;YAC7EE,QAAQ;QACV;IACF;AACF,GAAE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/exports/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/exports/client.ts"],"sourcesContent":["export { S3ClientUploadHandler } from '../client/S3ClientUploadHandler.js'\n"],"names":["S3ClientUploadHandler"],"mappings":"AAAA,SAASA,qBAAqB,QAAQ,qCAAoC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ClientUploadsAccess } from '@payloadcms/plugin-cloud-storage/types';
|
|
2
|
+
import type { PayloadHandler } from 'payload';
|
|
3
|
+
import * as AWS from '@aws-sdk/client-s3';
|
|
4
|
+
import type { S3StorageOptions } from './index.js';
|
|
5
|
+
interface Args {
|
|
6
|
+
access?: ClientUploadsAccess;
|
|
7
|
+
acl?: 'private' | 'public-read';
|
|
8
|
+
bucket: string;
|
|
9
|
+
collections: S3StorageOptions['collections'];
|
|
10
|
+
getStorageClient: () => AWS.S3;
|
|
11
|
+
}
|
|
12
|
+
export declare const getGenerateSignedURLHandler: ({ access, acl, bucket, collections, getStorageClient, }: Args) => PayloadHandler;
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=generateSignedURL.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateSignedURL.d.ts","sourceRoot":"","sources":["../src/generateSignedURL.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wCAAwC,CAAA;AACjF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAE7C,OAAO,KAAK,GAAG,MAAM,oBAAoB,CAAA;AAKzC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAElD,UAAU,IAAI;IACZ,MAAM,CAAC,EAAE,mBAAmB,CAAA;IAC5B,GAAG,CAAC,EAAE,SAAS,GAAG,aAAa,CAAA;IAC/B,MAAM,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,gBAAgB,CAAC,aAAa,CAAC,CAAA;IAC5C,gBAAgB,EAAE,MAAM,GAAG,CAAC,EAAE,CAAA;CAC/B;AAID,eAAO,MAAM,2BAA2B,4DAMrC,IAAI,KAAG,cAgCT,CAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import * as AWS from '@aws-sdk/client-s3';
|
|
2
|
+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { APIError, Forbidden } from 'payload';
|
|
5
|
+
const defaultAccess = ({ req })=>!!req.user;
|
|
6
|
+
export const getGenerateSignedURLHandler = ({ access = defaultAccess, acl, bucket, collections, getStorageClient })=>{
|
|
7
|
+
return async (req)=>{
|
|
8
|
+
if (!req.json) {
|
|
9
|
+
throw new APIError('Content-Type expected to be application/json', 400);
|
|
10
|
+
}
|
|
11
|
+
const { collectionSlug, filename, mimeType } = await req.json();
|
|
12
|
+
const collectionS3Config = collections[collectionSlug];
|
|
13
|
+
if (!collectionS3Config) {
|
|
14
|
+
throw new APIError(`Collection ${collectionSlug} was not found in S3 options`);
|
|
15
|
+
}
|
|
16
|
+
const prefix = typeof collectionS3Config === 'object' && collectionS3Config.prefix || '';
|
|
17
|
+
if (!await access({
|
|
18
|
+
collectionSlug,
|
|
19
|
+
req
|
|
20
|
+
})) {
|
|
21
|
+
throw new Forbidden();
|
|
22
|
+
}
|
|
23
|
+
const fileKey = path.posix.join(prefix, filename);
|
|
24
|
+
const url = await getSignedUrl(// @ts-expect-error mismatch versions or something
|
|
25
|
+
getStorageClient(), new AWS.PutObjectCommand({
|
|
26
|
+
ACL: acl,
|
|
27
|
+
Bucket: bucket,
|
|
28
|
+
ContentType: mimeType,
|
|
29
|
+
Key: fileKey
|
|
30
|
+
}), {
|
|
31
|
+
expiresIn: 600
|
|
32
|
+
});
|
|
33
|
+
return Response.json({
|
|
34
|
+
url
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
//# sourceMappingURL=generateSignedURL.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/generateSignedURL.ts"],"sourcesContent":["import type { ClientUploadsAccess } from '@payloadcms/plugin-cloud-storage/types'\nimport type { PayloadHandler } from 'payload'\n\nimport * as AWS from '@aws-sdk/client-s3'\nimport { getSignedUrl } from '@aws-sdk/s3-request-presigner'\nimport path from 'path'\nimport { APIError, Forbidden } from 'payload'\n\nimport type { S3StorageOptions } from './index.js'\n\ninterface Args {\n access?: ClientUploadsAccess\n acl?: 'private' | 'public-read'\n bucket: string\n collections: S3StorageOptions['collections']\n getStorageClient: () => AWS.S3\n}\n\nconst defaultAccess: Args['access'] = ({ req }) => !!req.user\n\nexport const getGenerateSignedURLHandler = ({\n access = defaultAccess,\n acl,\n bucket,\n collections,\n getStorageClient,\n}: Args): PayloadHandler => {\n return async (req) => {\n if (!req.json) {\n throw new APIError('Content-Type expected to be application/json', 400)\n }\n\n const { collectionSlug, filename, mimeType } = await req.json()\n\n const collectionS3Config = collections[collectionSlug]\n if (!collectionS3Config) {\n throw new APIError(`Collection ${collectionSlug} was not found in S3 options`)\n }\n\n const prefix = (typeof collectionS3Config === 'object' && collectionS3Config.prefix) || ''\n\n if (!(await access({ collectionSlug, req }))) {\n throw new Forbidden()\n }\n\n const fileKey = path.posix.join(prefix, filename)\n\n const url = await getSignedUrl(\n // @ts-expect-error mismatch versions or something\n getStorageClient(),\n new AWS.PutObjectCommand({ ACL: acl, Bucket: bucket, ContentType: mimeType, Key: fileKey }),\n {\n expiresIn: 600,\n },\n )\n\n return Response.json({ url })\n }\n}\n"],"names":["AWS","getSignedUrl","path","APIError","Forbidden","defaultAccess","req","user","getGenerateSignedURLHandler","access","acl","bucket","collections","getStorageClient","json","collectionSlug","filename","mimeType","collectionS3Config","prefix","fileKey","posix","join","url","PutObjectCommand","ACL","Bucket","ContentType","Key","expiresIn","Response"],"mappings":"AAGA,YAAYA,SAAS,qBAAoB;AACzC,SAASC,YAAY,QAAQ,gCAA+B;AAC5D,OAAOC,UAAU,OAAM;AACvB,SAASC,QAAQ,EAAEC,SAAS,QAAQ,UAAS;AAY7C,MAAMC,gBAAgC,CAAC,EAAEC,GAAG,EAAE,GAAK,CAAC,CAACA,IAAIC,IAAI;AAE7D,OAAO,MAAMC,8BAA8B,CAAC,EAC1CC,SAASJ,aAAa,EACtBK,GAAG,EACHC,MAAM,EACNC,WAAW,EACXC,gBAAgB,EACX;IACL,OAAO,OAAOP;QACZ,IAAI,CAACA,IAAIQ,IAAI,EAAE;YACb,MAAM,IAAIX,SAAS,gDAAgD;QACrE;QAEA,MAAM,EAAEY,cAAc,EAAEC,QAAQ,EAAEC,QAAQ,EAAE,GAAG,MAAMX,IAAIQ,IAAI;QAE7D,MAAMI,qBAAqBN,WAAW,CAACG,eAAe;QACtD,IAAI,CAACG,oBAAoB;YACvB,MAAM,IAAIf,SAAS,CAAC,WAAW,EAAEY,eAAe,4BAA4B,CAAC;QAC/E;QAEA,MAAMI,SAAS,AAAC,OAAOD,uBAAuB,YAAYA,mBAAmBC,MAAM,IAAK;QAExF,IAAI,CAAE,MAAMV,OAAO;YAAEM;YAAgBT;QAAI,IAAK;YAC5C,MAAM,IAAIF;QACZ;QAEA,MAAMgB,UAAUlB,KAAKmB,KAAK,CAACC,IAAI,CAACH,QAAQH;QAExC,MAAMO,MAAM,MAAMtB,aAChB,kDAAkD;QAClDY,oBACA,IAAIb,IAAIwB,gBAAgB,CAAC;YAAEC,KAAKf;YAAKgB,QAAQf;YAAQgB,aAAaV;YAAUW,KAAKR;QAAQ,IACzF;YACES,WAAW;QACb;QAGF,OAAOC,SAAShB,IAAI,CAAC;YAAES;QAAI;IAC7B;AACF,EAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CollectionOptions } from '@payloadcms/plugin-cloud-storage/types';
|
|
1
|
+
import type { ClientUploadsConfig, CollectionOptions } from '@payloadcms/plugin-cloud-storage/types';
|
|
2
2
|
import type { Plugin, UploadCollectionSlug } from 'payload';
|
|
3
3
|
import * as AWS from '@aws-sdk/client-s3';
|
|
4
4
|
export type S3StorageOptions = {
|
|
@@ -12,6 +12,10 @@ export type S3StorageOptions = {
|
|
|
12
12
|
* Must follow [AWS S3 bucket naming conventions](https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html).
|
|
13
13
|
*/
|
|
14
14
|
bucket: string;
|
|
15
|
+
/**
|
|
16
|
+
* Do uploads directly on the client to bypass limits on Vercel. You must allow CORS PUT method for the bucket to your website.
|
|
17
|
+
*/
|
|
18
|
+
clientUploads?: ClientUploadsConfig;
|
|
15
19
|
/**
|
|
16
20
|
* Collection options to apply the S3 adapter to.
|
|
17
21
|
*/
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,mBAAmB,EAEnB,iBAAiB,EAElB,MAAM,wCAAwC,CAAA;AAC/C,OAAO,KAAK,EAAU,MAAM,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAEnE,OAAO,KAAK,GAAG,MAAM,oBAAoB,CAAA;AAUzC,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;OAEG;IAEH,GAAG,CAAC,EAAE,SAAS,GAAG,aAAa,CAAA;IAC/B;;;;OAIG;IAEH,MAAM,EAAE,MAAM,CAAA;IAEd;;OAEG;IACH,aAAa,CAAC,EAAE,mBAAmB,CAAA;IACnC;;OAEG;IACH,WAAW,EAAE,OAAO,CAAC,MAAM,CAAC,oBAAoB,EAAE,IAAI,CAAC,iBAAiB,EAAE,SAAS,CAAC,GAAG,IAAI,CAAC,CAAC,CAAA;IAE7F;;;;OAIG;IACH,MAAM,EAAE,GAAG,CAAC,cAAc,CAAA;IAE1B;;;;OAIG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAA;IAE7B;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB,CAAA;AAED,KAAK,eAAe,GAAG,CAAC,aAAa,EAAE,gBAAgB,KAAK,MAAM,CAAA;AAElE,eAAO,MAAM,SAAS,EAAE,eAwErB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import * as AWS from '@aws-sdk/client-s3';
|
|
2
2
|
import { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage';
|
|
3
|
+
import { initClientUploads } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
4
|
+
import { getGenerateSignedURLHandler } from './generateSignedURL.js';
|
|
3
5
|
import { getGenerateURL } from './generateURL.js';
|
|
4
6
|
import { getHandleDelete } from './handleDelete.js';
|
|
5
7
|
import { getHandleUpload } from './handleUpload.js';
|
|
@@ -8,7 +10,29 @@ export const s3Storage = (s3StorageOptions)=>(incomingConfig)=>{
|
|
|
8
10
|
if (s3StorageOptions.enabled === false) {
|
|
9
11
|
return incomingConfig;
|
|
10
12
|
}
|
|
11
|
-
|
|
13
|
+
let storageClient = null;
|
|
14
|
+
const getStorageClient = ()=>{
|
|
15
|
+
if (storageClient) {
|
|
16
|
+
return storageClient;
|
|
17
|
+
}
|
|
18
|
+
storageClient = new AWS.S3(s3StorageOptions.config ?? {});
|
|
19
|
+
return storageClient;
|
|
20
|
+
};
|
|
21
|
+
initClientUploads({
|
|
22
|
+
clientHandler: '@payloadcms/storage-s3/client#S3ClientUploadHandler',
|
|
23
|
+
collections: s3StorageOptions.collections,
|
|
24
|
+
config: incomingConfig,
|
|
25
|
+
enabled: !!s3StorageOptions.clientUploads,
|
|
26
|
+
serverHandler: getGenerateSignedURLHandler({
|
|
27
|
+
access: typeof s3StorageOptions.clientUploads === 'object' ? s3StorageOptions.clientUploads.access : undefined,
|
|
28
|
+
acl: s3StorageOptions.acl,
|
|
29
|
+
bucket: s3StorageOptions.bucket,
|
|
30
|
+
collections: s3StorageOptions.collections,
|
|
31
|
+
getStorageClient
|
|
32
|
+
}),
|
|
33
|
+
serverHandlerPath: '/storage-s3-generate-signed-url'
|
|
34
|
+
});
|
|
35
|
+
const adapter = s3StorageInternal(getStorageClient, s3StorageOptions);
|
|
12
36
|
// Add adapter to each collection option object
|
|
13
37
|
const collectionsWithAdapter = Object.entries(s3StorageOptions.collections).reduce((acc, [slug, collOptions])=>({
|
|
14
38
|
...acc,
|
|
@@ -37,16 +61,8 @@ export const s3Storage = (s3StorageOptions)=>(incomingConfig)=>{
|
|
|
37
61
|
collections: collectionsWithAdapter
|
|
38
62
|
})(config);
|
|
39
63
|
};
|
|
40
|
-
function s3StorageInternal({ acl, bucket, config = {} }) {
|
|
64
|
+
function s3StorageInternal(getStorageClient, { acl, bucket, config = {} }) {
|
|
41
65
|
return ({ collection, prefix })=>{
|
|
42
|
-
let storageClient = null;
|
|
43
|
-
const getStorageClient = ()=>{
|
|
44
|
-
if (storageClient) {
|
|
45
|
-
return storageClient;
|
|
46
|
-
}
|
|
47
|
-
storageClient = new AWS.S3(config);
|
|
48
|
-
return storageClient;
|
|
49
|
-
};
|
|
50
66
|
return {
|
|
51
67
|
name: 's3',
|
|
52
68
|
generateURL: getGenerateURL({
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type {\n Adapter,\n PluginOptions as CloudStoragePluginOptions,\n CollectionOptions,\n GeneratedAdapter,\n} from '@payloadcms/plugin-cloud-storage/types'\nimport type { Config, Plugin, UploadCollectionSlug } from 'payload'\n\nimport * as AWS from '@aws-sdk/client-s3'\nimport { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage'\n\nimport { getGenerateURL } from './generateURL.js'\nimport { getHandleDelete } from './handleDelete.js'\nimport { getHandleUpload } from './handleUpload.js'\nimport { getHandler } from './staticHandler.js'\n\nexport type S3StorageOptions = {\n /**\n * Access control list for uploaded files.\n */\n\n acl?: 'private' | 'public-read'\n /**\n * Bucket name to upload files to.\n *\n * Must follow [AWS S3 bucket naming conventions](https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html).\n */\n\n bucket: string\n\n /**\n * Collection options to apply the S3 adapter to.\n */\n collections: Partial<Record<UploadCollectionSlug, Omit<CollectionOptions, 'adapter'> | true>>\n /**\n * AWS S3 client configuration. Highly dependent on your AWS setup.\n *\n * [AWS.S3ClientConfig Docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/interfaces/s3clientconfig.html)\n */\n config: AWS.S3ClientConfig\n\n /**\n * Whether or not to disable local storage\n *\n * @default true\n */\n disableLocalStorage?: boolean\n\n /**\n * Whether or not to enable the plugin\n *\n * Default: true\n */\n enabled?: boolean\n}\n\ntype S3StoragePlugin = (storageS3Args: S3StorageOptions) => Plugin\n\nexport const s3Storage: S3StoragePlugin =\n (s3StorageOptions: S3StorageOptions) =>\n (incomingConfig: Config): Config => {\n if (s3StorageOptions.enabled === false) {\n return incomingConfig\n }\n\n const adapter = s3StorageInternal(s3StorageOptions)\n\n // Add adapter to each collection option object\n const collectionsWithAdapter: CloudStoragePluginOptions['collections'] = Object.entries(\n s3StorageOptions.collections,\n ).reduce(\n (acc, [slug, collOptions]) => ({\n ...acc,\n [slug]: {\n ...(collOptions === true ? {} : collOptions),\n adapter,\n },\n }),\n {} as Record<string, CollectionOptions>,\n )\n\n // Set disableLocalStorage: true for collections specified in the plugin options\n const config = {\n ...incomingConfig,\n collections: (incomingConfig.collections || []).map((collection) => {\n if (!collectionsWithAdapter[collection.slug]) {\n return collection\n }\n\n return {\n ...collection,\n upload: {\n ...(typeof collection.upload === 'object' ? collection.upload : {}),\n disableLocalStorage: true,\n },\n }\n }),\n }\n\n return cloudStoragePlugin({\n collections: collectionsWithAdapter,\n })(config)\n }\n\nfunction s3StorageInternal({ acl, bucket, config = {} }: S3StorageOptions): Adapter {\n return ({ collection, prefix }): GeneratedAdapter => {\n
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type {\n Adapter,\n ClientUploadsConfig,\n PluginOptions as CloudStoragePluginOptions,\n CollectionOptions,\n GeneratedAdapter,\n} from '@payloadcms/plugin-cloud-storage/types'\nimport type { Config, Plugin, UploadCollectionSlug } from 'payload'\n\nimport * as AWS from '@aws-sdk/client-s3'\nimport { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage'\nimport { initClientUploads } from '@payloadcms/plugin-cloud-storage/utilities'\n\nimport { getGenerateSignedURLHandler } from './generateSignedURL.js'\nimport { getGenerateURL } from './generateURL.js'\nimport { getHandleDelete } from './handleDelete.js'\nimport { getHandleUpload } from './handleUpload.js'\nimport { getHandler } from './staticHandler.js'\n\nexport type S3StorageOptions = {\n /**\n * Access control list for uploaded files.\n */\n\n acl?: 'private' | 'public-read'\n /**\n * Bucket name to upload files to.\n *\n * Must follow [AWS S3 bucket naming conventions](https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html).\n */\n\n bucket: string\n\n /**\n * Do uploads directly on the client to bypass limits on Vercel. You must allow CORS PUT method for the bucket to your website.\n */\n clientUploads?: ClientUploadsConfig\n /**\n * Collection options to apply the S3 adapter to.\n */\n collections: Partial<Record<UploadCollectionSlug, Omit<CollectionOptions, 'adapter'> | true>>\n\n /**\n * AWS S3 client configuration. Highly dependent on your AWS setup.\n *\n * [AWS.S3ClientConfig Docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/interfaces/s3clientconfig.html)\n */\n config: AWS.S3ClientConfig\n\n /**\n * Whether or not to disable local storage\n *\n * @default true\n */\n disableLocalStorage?: boolean\n\n /**\n * Whether or not to enable the plugin\n *\n * Default: true\n */\n enabled?: boolean\n}\n\ntype S3StoragePlugin = (storageS3Args: S3StorageOptions) => Plugin\n\nexport const s3Storage: S3StoragePlugin =\n (s3StorageOptions: S3StorageOptions) =>\n (incomingConfig: Config): Config => {\n if (s3StorageOptions.enabled === false) {\n return incomingConfig\n }\n\n let storageClient: AWS.S3 | null = null\n\n const getStorageClient: () => AWS.S3 = () => {\n if (storageClient) {\n return storageClient\n }\n storageClient = new AWS.S3(s3StorageOptions.config ?? {})\n return storageClient\n }\n\n initClientUploads({\n clientHandler: '@payloadcms/storage-s3/client#S3ClientUploadHandler',\n collections: s3StorageOptions.collections,\n config: incomingConfig,\n enabled: !!s3StorageOptions.clientUploads,\n serverHandler: getGenerateSignedURLHandler({\n access:\n typeof s3StorageOptions.clientUploads === 'object'\n ? s3StorageOptions.clientUploads.access\n : undefined,\n acl: s3StorageOptions.acl,\n bucket: s3StorageOptions.bucket,\n collections: s3StorageOptions.collections,\n getStorageClient,\n }),\n serverHandlerPath: '/storage-s3-generate-signed-url',\n })\n\n const adapter = s3StorageInternal(getStorageClient, s3StorageOptions)\n\n // Add adapter to each collection option object\n const collectionsWithAdapter: CloudStoragePluginOptions['collections'] = Object.entries(\n s3StorageOptions.collections,\n ).reduce(\n (acc, [slug, collOptions]) => ({\n ...acc,\n [slug]: {\n ...(collOptions === true ? {} : collOptions),\n adapter,\n },\n }),\n {} as Record<string, CollectionOptions>,\n )\n\n // Set disableLocalStorage: true for collections specified in the plugin options\n const config = {\n ...incomingConfig,\n collections: (incomingConfig.collections || []).map((collection) => {\n if (!collectionsWithAdapter[collection.slug]) {\n return collection\n }\n\n return {\n ...collection,\n upload: {\n ...(typeof collection.upload === 'object' ? collection.upload : {}),\n disableLocalStorage: true,\n },\n }\n }),\n }\n\n return cloudStoragePlugin({\n collections: collectionsWithAdapter,\n })(config)\n }\n\nfunction s3StorageInternal(\n getStorageClient: () => AWS.S3,\n { acl, bucket, config = {} }: S3StorageOptions,\n): Adapter {\n return ({ collection, prefix }): GeneratedAdapter => {\n return {\n name: 's3',\n generateURL: getGenerateURL({ bucket, config }),\n handleDelete: getHandleDelete({ bucket, getStorageClient }),\n handleUpload: getHandleUpload({\n acl,\n bucket,\n collection,\n getStorageClient,\n prefix,\n }),\n staticHandler: getHandler({ bucket, collection, getStorageClient }),\n }\n }\n}\n"],"names":["AWS","cloudStoragePlugin","initClientUploads","getGenerateSignedURLHandler","getGenerateURL","getHandleDelete","getHandleUpload","getHandler","s3Storage","s3StorageOptions","incomingConfig","enabled","storageClient","getStorageClient","S3","config","clientHandler","collections","clientUploads","serverHandler","access","undefined","acl","bucket","serverHandlerPath","adapter","s3StorageInternal","collectionsWithAdapter","Object","entries","reduce","acc","slug","collOptions","map","collection","upload","disableLocalStorage","prefix","name","generateURL","handleDelete","handleUpload","staticHandler"],"mappings":"AASA,YAAYA,SAAS,qBAAoB;AACzC,SAASC,kBAAkB,QAAQ,mCAAkC;AACrE,SAASC,iBAAiB,QAAQ,6CAA4C;AAE9E,SAASC,2BAA2B,QAAQ,yBAAwB;AACpE,SAASC,cAAc,QAAQ,mBAAkB;AACjD,SAASC,eAAe,QAAQ,oBAAmB;AACnD,SAASC,eAAe,QAAQ,oBAAmB;AACnD,SAASC,UAAU,QAAQ,qBAAoB;AAiD/C,OAAO,MAAMC,YACX,CAACC,mBACD,CAACC;QACC,IAAID,iBAAiBE,OAAO,KAAK,OAAO;YACtC,OAAOD;QACT;QAEA,IAAIE,gBAA+B;QAEnC,MAAMC,mBAAiC;YACrC,IAAID,eAAe;gBACjB,OAAOA;YACT;YACAA,gBAAgB,IAAIZ,IAAIc,EAAE,CAACL,iBAAiBM,MAAM,IAAI,CAAC;YACvD,OAAOH;QACT;QAEAV,kBAAkB;YAChBc,eAAe;YACfC,aAAaR,iBAAiBQ,WAAW;YACzCF,QAAQL;YACRC,SAAS,CAAC,CAACF,iBAAiBS,aAAa;YACzCC,eAAehB,4BAA4B;gBACzCiB,QACE,OAAOX,iBAAiBS,aAAa,KAAK,WACtCT,iBAAiBS,aAAa,CAACE,MAAM,GACrCC;gBACNC,KAAKb,iBAAiBa,GAAG;gBACzBC,QAAQd,iBAAiBc,MAAM;gBAC/BN,aAAaR,iBAAiBQ,WAAW;gBACzCJ;YACF;YACAW,mBAAmB;QACrB;QAEA,MAAMC,UAAUC,kBAAkBb,kBAAkBJ;QAEpD,+CAA+C;QAC/C,MAAMkB,yBAAmEC,OAAOC,OAAO,CACrFpB,iBAAiBQ,WAAW,EAC5Ba,MAAM,CACN,CAACC,KAAK,CAACC,MAAMC,YAAY,GAAM,CAAA;gBAC7B,GAAGF,GAAG;gBACN,CAACC,KAAK,EAAE;oBACN,GAAIC,gBAAgB,OAAO,CAAC,IAAIA,WAAW;oBAC3CR;gBACF;YACF,CAAA,GACA,CAAC;QAGH,gFAAgF;QAChF,MAAMV,SAAS;YACb,GAAGL,cAAc;YACjBO,aAAa,AAACP,CAAAA,eAAeO,WAAW,IAAI,EAAE,AAAD,EAAGiB,GAAG,CAAC,CAACC;gBACnD,IAAI,CAACR,sBAAsB,CAACQ,WAAWH,IAAI,CAAC,EAAE;oBAC5C,OAAOG;gBACT;gBAEA,OAAO;oBACL,GAAGA,UAAU;oBACbC,QAAQ;wBACN,GAAI,OAAOD,WAAWC,MAAM,KAAK,WAAWD,WAAWC,MAAM,GAAG,CAAC,CAAC;wBAClEC,qBAAqB;oBACvB;gBACF;YACF;QACF;QAEA,OAAOpC,mBAAmB;YACxBgB,aAAaU;QACf,GAAGZ;IACL,EAAC;AAEH,SAASW,kBACPb,gBAA8B,EAC9B,EAAES,GAAG,EAAEC,MAAM,EAAER,SAAS,CAAC,CAAC,EAAoB;IAE9C,OAAO,CAAC,EAAEoB,UAAU,EAAEG,MAAM,EAAE;QAC5B,OAAO;YACLC,MAAM;YACNC,aAAapC,eAAe;gBAAEmB;gBAAQR;YAAO;YAC7C0B,cAAcpC,gBAAgB;gBAAEkB;gBAAQV;YAAiB;YACzD6B,cAAcpC,gBAAgB;gBAC5BgB;gBACAC;gBACAY;gBACAtB;gBACAyB;YACF;YACAK,eAAepC,WAAW;gBAAEgB;gBAAQY;gBAAYtB;YAAiB;QACnE;IACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/storage-s3",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.25.0-canary.3d5c763",
|
|
4
4
|
"description": "Payload storage adapter for Amazon S3",
|
|
5
5
|
"homepage": "https://payloadcms.com",
|
|
6
6
|
"repository": {
|
|
@@ -23,6 +23,11 @@
|
|
|
23
23
|
"import": "./dist/index.js",
|
|
24
24
|
"types": "./dist/index.d.ts",
|
|
25
25
|
"default": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./client": {
|
|
28
|
+
"import": "./dist/exports/client.js",
|
|
29
|
+
"types": "./dist/exports/client.d.ts",
|
|
30
|
+
"default": "./dist/exports/client.js"
|
|
26
31
|
}
|
|
27
32
|
},
|
|
28
33
|
"main": "./dist/index.js",
|
|
@@ -33,13 +38,14 @@
|
|
|
33
38
|
"dependencies": {
|
|
34
39
|
"@aws-sdk/client-s3": "^3.614.0",
|
|
35
40
|
"@aws-sdk/lib-storage": "^3.614.0",
|
|
36
|
-
"@
|
|
41
|
+
"@aws-sdk/s3-request-presigner": "^3.614.0",
|
|
42
|
+
"@payloadcms/plugin-cloud-storage": "3.25.0-canary.3d5c763"
|
|
37
43
|
},
|
|
38
44
|
"devDependencies": {
|
|
39
|
-
"payload": "3.
|
|
45
|
+
"payload": "3.25.0-canary.3d5c763"
|
|
40
46
|
},
|
|
41
47
|
"peerDependencies": {
|
|
42
|
-
"payload": "3.
|
|
48
|
+
"payload": "3.25.0-canary.3d5c763"
|
|
43
49
|
},
|
|
44
50
|
"engines": {
|
|
45
51
|
"node": "^18.20.2 || >=20.9.0"
|