Skip to content

Commit 210de57

Browse files
fix(storage-vercel-blob): upload processed client files from temp file (#18388)
## Summary Fixes #18333, together with #18913. If Payload processed a direct-client upload, Vercel Blob, GCS, and R2 could store a zero-byte object. The document still reported the processed file size. These adapters now upload from the temporary file that holds the processed bytes. On 3.x, a crop with Vercel Blob first fails with a 500 error, so the crop case from the issue also needs #18913. ## What - For client uploads, Payload keeps the file on disk and does not load it into memory. After processing, the new bytes are in the temporary file and `file.data` is empty. - When `tempFilePath` is present, the Vercel Blob, GCS, and R2 adapters read the bytes from that file. S3 and Azure already do this. - R2 imports `fs` only when it needs the temporary file, because `fs` is not always available in Workers. <img width="882" height="1598" alt="CleanShot 2026-10-22 at 09 45 35" src="https://github.com/user-attachments/assets/a5472ae7-a0b8-4b62-beb3-d9eed96c2003" /> --------- Co-authored-by: Jarrod Flesch <[email protected]>
1 parent 779f06f commit 210de57

13 files changed

Lines changed: 218 additions & 11 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { randomUUID } from 'crypto'
2+
import { rm, writeFile } from 'fs/promises'
3+
import { tmpdir } from 'os'
4+
import path from 'path'
5+
import { Writable } from 'stream'
6+
import { expect, it } from 'vitest'
7+
8+
import { createGcsAdapter } from './adapter.js'
9+
10+
it('should upload the temporary file contents', async () => {
11+
const bytes = Buffer.from('processed image contents')
12+
const tempFilePath = path.join(tmpdir(), `gcs-upload-${randomUUID()}`)
13+
const chunks: Buffer[] = []
14+
const client = {
15+
bucket: () => ({
16+
file: () => ({
17+
createWriteStream: () =>
18+
new Writable({
19+
write(chunk, _encoding, callback) {
20+
chunks.push(chunk)
21+
callback()
22+
},
23+
}),
24+
}),
25+
}),
26+
}
27+
const adapter = createGcsAdapter({ bucket: 'media', getStorageClient: () => client as never })({
28+
collection: { slug: 'media' },
29+
} as never)
30+
31+
await writeFile(tempFilePath, bytes)
32+
33+
try {
34+
await adapter.handleUpload({
35+
file: { buffer: Buffer.alloc(0), mimeType: 'image/png', tempFilePath },
36+
storageFilePath: 'processed.png',
37+
} as never)
38+
39+
expect(Buffer.concat(chunks)).toEqual(bytes)
40+
} finally {
41+
await rm(tempFilePath, { force: true })
42+
}
43+
})

‎packages/storage-gcs/src/adapter.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export function createGcsAdapter({
5555
client: getStorageClient(),
5656
mimeType: file.mimeType,
5757
storageFilePath,
58+
tempFilePath: file.tempFilePath,
5859
})
5960

6061
return data

‎packages/storage-gcs/src/uploadFile.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import type { Storage } from '@google-cloud/storage'
22

3+
import { createReadStream } from 'fs'
4+
import { pipeline } from 'stream/promises'
5+
36
interface UploadFileArgs {
47
acl?: 'Private' | 'Public'
58
bucket: string
69
buffer: Buffer
710
client: Storage
811
mimeType: string
912
storageFilePath: string
13+
tempFilePath?: string
1014
}
1115

1216
export async function uploadFile({
@@ -16,14 +20,16 @@ export async function uploadFile({
1620
client,
1721
mimeType,
1822
storageFilePath,
23+
tempFilePath,
1924
}: UploadFileArgs): Promise<void> {
2025
const gcsFile = client.bucket(bucket).file(storageFilePath)
26+
const metadata = { contentType: mimeType }
2127

22-
await gcsFile.save(buffer, {
23-
metadata: {
24-
contentType: mimeType,
25-
},
26-
})
28+
if (tempFilePath) {
29+
await pipeline(createReadStream(tempFilePath), gcsFile.createWriteStream({ metadata }))
30+
} else {
31+
await gcsFile.save(buffer, { metadata })
32+
}
2733

2834
if (acl) {
2935
await gcsFile[`make${acl}`]()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { randomUUID } from 'crypto'
2+
import { rm, writeFile } from 'fs/promises'
3+
import { tmpdir } from 'os'
4+
import path from 'path'
5+
import { expect, it } from 'vitest'
6+
7+
import { createR2Adapter } from './adapter.js'
8+
9+
it('should upload the temporary file contents', async () => {
10+
const bytes = Buffer.from('processed image contents')
11+
const tempFilePath = path.join(tmpdir(), `r2-upload-${randomUUID()}`)
12+
let storedBody: Blob | Buffer | undefined
13+
const bucket = {
14+
put: (_key: string, body: Blob | Buffer) => {
15+
storedBody = body
16+
},
17+
}
18+
const adapter = createR2Adapter({ bucket: bucket as never })({
19+
collection: { slug: 'media' },
20+
} as never)
21+
22+
await writeFile(tempFilePath, bytes)
23+
24+
try {
25+
await adapter.handleUpload({
26+
file: { buffer: Buffer.alloc(0), mimeType: 'image/png', tempFilePath },
27+
storageFilePath: 'processed.png',
28+
} as never)
29+
30+
expect(Buffer.from(await new Response(storedBody).arrayBuffer())).toEqual(bytes)
31+
} finally {
32+
await rm(tempFilePath, { force: true })
33+
}
34+
})

‎packages/storage-r2/src/adapter.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function createR2Adapter({
3838
buffer: file.buffer,
3939
mimeType: file.mimeType,
4040
storageFilePath,
41+
tempFilePath: file.tempFilePath,
4142
}),
4243

4344
staticHandler: (req, { doc, headers, params: { clientUploadContext, filename } }) =>

‎packages/storage-r2/src/uploadFile.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,24 @@ interface UploadFileArgs {
77
buffer: Buffer
88
mimeType: string
99
storageFilePath: string
10+
tempFilePath?: string
1011
}
1112

1213
export async function uploadFile({
1314
bucket,
1415
buffer,
1516
mimeType,
1617
storageFilePath,
18+
tempFilePath,
1719
}: UploadFileArgs): Promise<void> {
1820
// Read more: https://github.com/cloudflare/workers-sdk/issues/6047#issuecomment-2691217843
19-
const body = isMiniflare ? new Blob([buffer]) : buffer
21+
let body = isMiniflare ? new Blob([buffer]) : buffer
22+
23+
if (tempFilePath) {
24+
// Workers require filesystem compatibility to use temp files, so keep this import conditional.
25+
const { openAsBlob } = await import('fs')
26+
body = await openAsBlob(tempFilePath)
27+
}
2028

2129
await bucket.put(storageFilePath, body, {
2230
httpMetadata: { contentType: mimeType },

‎packages/storage-vercel-blob/src/adapter.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ export function createVercelBlobAdapter({
4949
token,
5050
}),
5151

52-
handleUpload: async ({ data, file: { buffer, mimeType }, storageFilePath }) => {
52+
handleUpload: async ({ data, file: { buffer, mimeType, tempFilePath }, storageFilePath }) => {
5353
const result = await uploadFile({
5454
access,
5555
addRandomSuffix,
5656
buffer,
5757
cacheControlMaxAge,
5858
mimeType,
5959
storageFilePath,
60+
tempFilePath,
6061
token,
6162
})
6263

‎packages/storage-vercel-blob/src/uploadFile.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { put } from '@vercel/blob'
2+
import { openAsBlob } from 'fs'
23
import path from 'path'
34

45
interface UploadFileArgs {
@@ -8,6 +9,7 @@ interface UploadFileArgs {
89
cacheControlMaxAge?: number
910
mimeType: string
1011
storageFilePath: string
12+
tempFilePath?: string
1113
token: string
1214
}
1315

@@ -22,9 +24,11 @@ export async function uploadFile({
2224
cacheControlMaxAge,
2325
mimeType,
2426
storageFilePath,
27+
tempFilePath,
2528
token,
2629
}: UploadFileArgs): Promise<UploadFileResult> {
27-
const result = await put(storageFilePath, buffer, {
30+
const body = tempFilePath ? await openAsBlob(tempFilePath) : buffer
31+
const result = await put(storageFilePath, body, {
2832
access,
2933
addRandomSuffix,
3034
allowOverwrite: true,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { CollectionConfig } from 'payload'
2+
3+
import { resizedMediaSlug } from '../../shared.js'
4+
5+
export const ResizedMedia: CollectionConfig = {
6+
slug: resizedMediaSlug,
7+
fields: [],
8+
upload: { resizeOptions: { height: 10, width: 10 } },
9+
}

‎test/storage-vercel-blob/client-uploads/config.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import { devUser } from '../../credentials.js'
1010
import { Media } from '../collections/Media.js'
1111
import { MediaWithPrefix } from '../collections/MediaWithPrefix.js'
1212
import { Users } from '../collections/Users.js'
13-
import { mediaSlug, mediaWithPrefixSlug, prefix } from '../shared.js'
13+
import { mediaSlug, mediaWithPrefixSlug, prefix, resizedMediaSlug } from '../shared.js'
1414
import { MediaContainer } from './collections/MediaContainer.js'
15+
import { ResizedMedia } from './collections/ResizedMedia.js'
1516

1617
const filename = fileURLToPath(import.meta.url)
1718
const dirname = path.dirname(filename)
@@ -32,7 +33,7 @@ export default buildConfigWithDefaults({
3233
baseDir: path.resolve(dirname, '..'),
3334
},
3435
},
35-
collections: [Media, MediaWithPrefix, MediaContainer, LegacyMedia, Users],
36+
collections: [Media, MediaWithPrefix, MediaContainer, LegacyMedia, ResizedMedia, Users],
3637
onInit: async (payload) => {
3738
await payload.create({
3839
collection: 'users',
@@ -53,6 +54,7 @@ export default buildConfigWithDefaults({
5354
[mediaWithPrefixSlug]: {
5455
prefix,
5556
},
57+
[resizedMediaSlug]: true,
5658
},
5759
token: process.env.BLOB_READ_WRITE_TOKEN,
5860
}),

0 commit comments

Comments
 (0)