R2 put object throws 'Network connection lost' error

while put object to r2 ,always get error ‘Network connection lost’,tried many times

const bucket = c.env.FILE_BUCKET as R2Bucket
try {
        await bucket.put(id, file, {
            httpMetadata: {
                contentType: file.type
            },
            customMetadata: {
                createdAt: now.toString(),
               
            }
        })

        
    } catch (e) {
        console.log( e)
    }

Need a bit more information than that.

What is file, where did you get file from, how big is file, etc

didn’t change anything, but it’s working now :sweat_smile:

Notably FormData buffers the file into the Worker’s memory and is prone to hitting resource limits. I’d recommend passing the ReadableStream body straight to R2.put

OK,I will try that ,thanks

Hi, this error happens again.

export const AddFile = async (c: Context): Promise<Response> => {

    //save object
    const data = await c.req.formData();
    const file: File = data.get("file") as File;
    const id = await md5(file) as string;
    const bucket = c.env.DRIVE as R2Bucket
    let suffix = ''

    try {
        console.log(id)
        console.log(file.size)
        console.log(file.type)
        console.log(file.name)
        const saved = await bucket.put(id, file, {
            httpMetadata: {
                contentType: file.type
            },
            customMetadata: {
                createdAt: Date.now().toString(),
            }
        })

    } catch (e) {
        console.log(e)
        return Error(c, CODE_CLOUDFLARE_SERVICE_FAILED, 'service unavaiable,please try again later')
    }

    return Success(c, 'ok',)
}

above is the function

Successfully created tail, expires at 2022-10-31T13:28:26Z
Connected to worker, waiting for logs...
POST https://test.top/api/drive/file - Ok @ 10/31/2022, 4:11:38 PM
  (log)   <-- POST /api/drive/file
  (log) 99914b932bd37a50b983c5e7c90ae93b
  (log) 32200
  (log) image/jpeg
  (log) 400x400.jpeg
  (log) Error: Network connection lost.
  (log)   --> POST /api/drive/file 200 0ms

here is log

This needs to be answered

It’s a jpeg file, 32kb

It’s a jpeg file, 32kb

I’m not along. After searching and testing for hours ,found a solution in discord thread

https://discord.com/channels/595317990191398933/779390076219686943/1026995624475885598

just change it to

const saved = await bucket.put(id, file, {
            httpMetadata: {
                contentType: file.type
            },
            customMetadata: {
                createdAt: Date.now().toString(),
            }
        })

to

const saved = await bucket.put(id, file.stream(), {
            httpMetadata: {
                contentType: file.type
            },
            customMetadata: {
                createdAt: Date.now().toString(),
            }
        })

fix the problem.

But as the thread stuff said

  1. Wouldn’t recommend FormData personally since you’re buffering it in memory

Sending it as binary & just passing request.body to R2 is the ideal way

Not an issue with 38KB but will limit larger files

but now read a 32kb file into memory cause the R2 bucket Network connection lost error