Back

[SOLVED] Facing issue while uploading file to the storage bucket

  • 0
  • Web
Ayondip
30 Jun, 2023, 15:46

I am trying to upload a file from my backend to the storage bucket. An user will upload the file from the frontend and I am trying to upload that same to the appwrite storage bucket.

TypeScript
import multer from 'multer';
import sdk from 'node-appwrite';
import { appwrite } from '../../config/appwrite.config';

const router = Router();
const upload = multer();

router.post(
  '/',
  upload.single('photo'),
  async (req: Request, res: Response, next: NextFunction) => {
    try {
      console.log(req.file?.buffer);
      const storage = new sdk.Storage(appwrite);
      const r = await storage.createFile(
        process.env.APPWRITE_BUCKET_ID || '',
        '122212',
        sdk.InputFile.fromBuffer(
          req.file?.buffer as Buffer,
          req.file?.filename || ''
        )
      );
      res.send(new ResponseHandler({r}));
    } catch (error) {
      next(error);
    }
  }
);

This is the code that I have written but while testing this I am getting back the below error

TypeScript
"error": {
        "code": 400,
        "type": "storage_file_empty",
        "response": {
            "message": "Empty file passed to the endpoint.",
            "code": 400,
            "type": "storage_file_empty",
            "version": "0.10.44"
        }
    }
TL;DR
The user was facing an issue while uploading a file to a storage bucket. The issue was due to an empty `req.file.filename` value being passed to a function. The solution was to check if `req.file.filename` is empty, and if so, provide a default filename. The user confirmed that the issue was resolved after implementing this solution.
Binyamin
30 Jun, 2023, 15:49

What is the log of this function?

TypeScript
console.log(sdk.InputFile.fromBuffer(
          req.file?.buffer as Buffer,
          req.file?.filename || ''
        ));
Ayondip
30 Jun, 2023, 15:52
TypeScript
InputFile {
  stream: Readable {
    _readableState: ReadableState {
      objectMode: true,
      highWaterMark: 16,
      buffer: BufferList { head: null, tail: null, length: 0 },
      length: 0,
      pipes: [],
      flowing: null,
      ended: false,
      endEmitted: false,
      reading: false,
      constructed: true,
      sync: true,
      needReadable: false,
      emittedReadable: false,
      readableListening: false,
      resumeScheduled: false,
      errorEmitted: false,
      emitClose: true,
      autoDestroy: true,
      destroyed: false,
      errored: null,
      closed: false,
      closeEmitted: false,
      defaultEncoding: 'utf8',
      awaitDrainWriters: null,
      multiAwaitDrain: false,
      readingMore: false,
      dataEmitted: false,
      decoder: null,
      encoding: null,
      [Symbol(kPaused)]: null
    },
    _read: [Function: read],
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    [Symbol(kCapture)]: false
  },
  size: 241397,
  filename: ''
}
Ayondip
30 Jun, 2023, 15:52

The function is returning back this

Binyamin
30 Jun, 2023, 15:55

Okay, that's looks good, maybe the buffer is empty

Binyamin
30 Jun, 2023, 15:56

What you have here

TypeScript
console.log(req.file?.buffer)
Ayondip
30 Jun, 2023, 15:56

I have tried printing req.file?.buffer

Ayondip
30 Jun, 2023, 15:56

Let me show you the output

Ayondip
30 Jun, 2023, 15:57
TypeScript
<Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 03 ac 00 00 03 14 08 06 00 00 00 b8 a2 aa 5c 00 00 00 01 73 52 47 42 00 ae ce 1c e9 00 00 00 04 ... 241347 more bytes>
Binyamin
30 Jun, 2023, 15:57

Okay, So it's good Let me check something

Binyamin
30 Jun, 2023, 15:59

Okay

Binyamin
30 Jun, 2023, 16:00

I thing you must give a filename for the file

Binyamin
30 Jun, 2023, 16:00

Change to this

TypeScript
  sdk.InputFile.fromBuffer(
          req.file?.buffer as Buffer,
          req.file?.filename || 'filename'
        )
Binyamin
30 Jun, 2023, 16:00

And, test it

Ayondip
30 Jun, 2023, 16:00

well I have not tried printing req.file.filename let me check

Binyamin
30 Jun, 2023, 16:01

You can see here - at the end - it's empty

Binyamin
30 Jun, 2023, 16:01

I think

Ayondip
30 Jun, 2023, 16:02

Resolved.

Ayondip
30 Jun, 2023, 16:02

req.file.filename was empty so eventually an empty string was getting passed to the function

Binyamin
30 Jun, 2023, 16:03

<a:agooglethumbsup:635256484682530825>

Ayondip
30 Jun, 2023, 16:04

[Resolved] Facing issue while uploading file to the storage bucket

Ayondip
30 Jun, 2023, 16:04

[SOLVED] Facing issue while uploading file to the storage bucket

Drake
30 Jun, 2023, 16:56
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more