Skip to content

Commit fc42c40

Browse files
authored
fix(storage-s3, storage-azure, storage-gcs): client uploads when a collection has prefix configured (#11436)
### What? Fixes client uploads when storage collection config has the `prefix` property configured. Previously, it failed with "Object key was not found". ### Why? This is expected to work. ### How? The client upload handler now receives to its props `prefix`. Then it threads it to the server-side `staticHandler` through `clientUploadContext` and then to `getFilePrefix`, which checks for `clientUploadContext.prefix` and returns if there is. Previously, `staticHandler` tried to load the file without including prefix consideration. This changes only these adapters: * S3 * Azure * GCS With the Vercel Blob adapter, `prefix` works correctly.
1 parent 83b4548 commit fc42c40

File tree

9 files changed

+45
-9
lines changed

9 files changed

+45
-9
lines changed

packages/plugin-cloud-storage/src/client/createClientUploadHandler.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type ClientUploadHandlerProps<T extends Record<string, unknown>> = {
1010
collectionSlug: UploadCollectionSlug
1111
enabled?: boolean
1212
extra: T
13+
prefix?: string
1314
serverHandlerPath: string
1415
}
1516

@@ -21,6 +22,7 @@ export const createClientUploadHandler = <T extends Record<string, unknown>>({
2122
collectionSlug: UploadCollectionSlug
2223
extra: T
2324
file: File
25+
prefix?: string
2426
serverHandlerPath: string
2527
serverURL: string
2628
updateFilename: (value: string) => void
@@ -31,6 +33,7 @@ export const createClientUploadHandler = <T extends Record<string, unknown>>({
3133
collectionSlug,
3234
enabled,
3335
extra,
36+
prefix,
3437
serverHandlerPath,
3538
}: ClientUploadHandlerProps<T>) {
3639
const { setUploadHandler } = useUploadHandlers()
@@ -51,6 +54,7 @@ export const createClientUploadHandler = <T extends Record<string, unknown>>({
5154
collectionSlug,
5255
extra,
5356
file,
57+
prefix,
5458
serverHandlerPath,
5559
serverURL,
5660
updateFilename,

packages/plugin-cloud-storage/src/utilities/getFilePrefix.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
import type { CollectionConfig, PayloadRequest, UploadConfig } from 'payload'
22

33
export async function getFilePrefix({
4+
clientUploadContext,
45
collection,
56
filename,
67
req,
78
}: {
9+
clientUploadContext?: unknown
810
collection: CollectionConfig
911
filename: string
1012
req: PayloadRequest
1113
}): Promise<string> {
14+
// Prioritize from clientUploadContext if there is:
15+
if (
16+
clientUploadContext &&
17+
typeof clientUploadContext === 'object' &&
18+
'prefix' in clientUploadContext &&
19+
typeof clientUploadContext.prefix === 'string'
20+
) {
21+
return clientUploadContext.prefix
22+
}
23+
1224
const imageSizes = (collection?.upload as UploadConfig)?.imageSizes || []
1325

1426
const files = await req.payload.find({

packages/plugin-cloud-storage/src/utilities/initClientUploads.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,23 @@ export const initClientUploads = <ExtraProps extends Record<string, unknown>, T>
6363
for (const collectionSlug in collections) {
6464
const collection = collections[collectionSlug]
6565

66+
let prefix: string | undefined
67+
68+
if (
69+
collection &&
70+
typeof collection === 'object' &&
71+
'prefix' in collection &&
72+
typeof collection.prefix === 'string'
73+
) {
74+
prefix = collection.prefix
75+
}
76+
6677
config.admin.components.providers.push({
6778
clientProps: {
6879
collectionSlug,
6980
enabled,
7081
extra: extraClientHandlerProps ? extraClientHandlerProps(collection) : undefined,
82+
prefix,
7183
serverHandlerPath,
7284
},
7385
path: clientHandler,

packages/storage-azure/src/client/AzureClientUploadHandler.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { createClientUploadHandler } from '@payloadcms/plugin-cloud-storage/client'
33

44
export const AzureClientUploadHandler = createClientUploadHandler({
5-
handler: async ({ apiRoute, collectionSlug, file, serverHandlerPath, serverURL }) => {
5+
handler: async ({ apiRoute, collectionSlug, file, prefix, serverHandlerPath, serverURL }) => {
66
const response = await fetch(`${serverURL}${apiRoute}${serverHandlerPath}`, {
77
body: JSON.stringify({
88
collectionSlug,
@@ -25,5 +25,7 @@ export const AzureClientUploadHandler = createClientUploadHandler({
2525
},
2626
method: 'PUT',
2727
})
28+
29+
return { prefix }
2830
},
2931
})

packages/storage-azure/src/staticHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ interface Args {
1313
}
1414

1515
export const getHandler = ({ collection, getStorageClient }: Args): StaticHandler => {
16-
return async (req, { params: { filename } }) => {
16+
return async (req, { params: { clientUploadContext, filename } }) => {
1717
try {
18-
const prefix = await getFilePrefix({ collection, filename, req })
18+
const prefix = await getFilePrefix({ clientUploadContext, collection, filename, req })
1919
const blockBlobClient = getStorageClient().getBlockBlobClient(
2020
path.posix.join(prefix, filename),
2121
)

packages/storage-gcs/src/client/GcsClientUploadHandler.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { createClientUploadHandler } from '@payloadcms/plugin-cloud-storage/client'
33

44
export const GcsClientUploadHandler = createClientUploadHandler({
5-
handler: async ({ apiRoute, collectionSlug, file, serverHandlerPath, serverURL }) => {
5+
handler: async ({ apiRoute, collectionSlug, file, prefix, serverHandlerPath, serverURL }) => {
66
const response = await fetch(`${serverURL}${apiRoute}${serverHandlerPath}`, {
77
body: JSON.stringify({
88
collectionSlug,
@@ -20,5 +20,9 @@ export const GcsClientUploadHandler = createClientUploadHandler({
2020
headers: { 'Content-Length': file.size.toString(), 'Content-Type': file.type },
2121
method: 'PUT',
2222
})
23+
24+
return {
25+
prefix,
26+
}
2327
},
2428
})

packages/storage-gcs/src/staticHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ interface Args {
1212
}
1313

1414
export const getHandler = ({ bucket, collection, getStorageClient }: Args): StaticHandler => {
15-
return async (req, { params: { filename } }) => {
15+
return async (req, { params: { clientUploadContext, filename } }) => {
1616
try {
17-
const prefix = await getFilePrefix({ collection, filename, req })
17+
const prefix = await getFilePrefix({ clientUploadContext, collection, filename, req })
1818
const file = getStorageClient().bucket(bucket).file(path.posix.join(prefix, filename))
1919

2020
const [metadata] = await file.getMetadata()

packages/storage-s3/src/client/S3ClientUploadHandler.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { createClientUploadHandler } from '@payloadcms/plugin-cloud-storage/client'
33

44
export const S3ClientUploadHandler = createClientUploadHandler({
5-
handler: async ({ apiRoute, collectionSlug, file, serverHandlerPath, serverURL }) => {
5+
handler: async ({ apiRoute, collectionSlug, file, prefix, serverHandlerPath, serverURL }) => {
66
const response = await fetch(`${serverURL}${apiRoute}${serverHandlerPath}`, {
77
body: JSON.stringify({
88
collectionSlug,
@@ -20,5 +20,7 @@ export const S3ClientUploadHandler = createClientUploadHandler({
2020
headers: { 'Content-Length': file.size.toString(), 'Content-Type': file.type },
2121
method: 'PUT',
2222
})
23+
24+
return { prefix }
2325
},
2426
})

packages/storage-s3/src/staticHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ const streamToBuffer = async (readableStream: any) => {
3535
}
3636

3737
export const getHandler = ({ bucket, collection, getStorageClient }: Args): StaticHandler => {
38-
return async (req, { params: { filename } }) => {
38+
return async (req, { params: { clientUploadContext, filename } }) => {
3939
try {
40-
const prefix = await getFilePrefix({ collection, filename, req })
40+
const prefix = await getFilePrefix({ clientUploadContext, collection, filename, req })
4141

4242
const key = path.posix.join(prefix, filename)
4343

0 commit comments

Comments
 (0)