Back

How to use storage.creatFile from nextjs 13 (API) server side

  • 0
  • Self Hosted
  • Web
  • Storage
snowcode
29 Jul, 2023, 19:11

I was trying to use nextjs13 which gives you both front end and back end (API and server routing ) out of the box.

TL;DR
TL;DR: User is trying to use the `storage.createFile` function from Next.js 13 (API) server-side. They realize that the file stream cannot be directly processed and need to configure middleware. Suggestions are given to use `multer` or `formidable`. User also asks for examples of using Appwrite in both client and server-side in Next.js 13.
snowcode
29 Jul, 2023, 19:13

I could simply move the back end to a standalone node service running something like express, (or similar libs) which I assume I would simply npm install node-apprwrite or something like that, and use the "server" getting started notes, and set ... the server API key

snowcode
29 Jul, 2023, 19:14

but I didn't want to do that, if I didn't have to. That would mean maintaining two sets of code for my front end and back end projects, and keeping them in sync. Ideally I have just 1 project, nedxtjs 13 and use it's built in separation of client and server and built in powerful support for rapidly building REST API's.

Drake
29 Jul, 2023, 19:14

So you want to build this?

TypeScript
[ Client app ] <=> [ next js ] <=> [ Appwrite ]
Drake
29 Jul, 2023, 19:16

Soo I'm not totally sure you can pull file out like form.get('image') as File 🧐

snowcode
29 Jul, 2023, 19:16

what I want is changing depending on the information I'm discovering. I want to know how everything works. I rekon I have a good idea of how the front end jwt / auth all works, but a lot of work I do involves building API's for corporate enterprise clients to integrate with...so I need to find examples of "server side" code that can publish "images/files" to appwrite storage.

Drake
29 Jul, 2023, 19:17

The error you get...is there a stack trace to say what line that error comes from?

snowcode
29 Jul, 2023, 19:17

server side you definately can...because that's exactly how I'd process (on the server) the submitted POST/d image file ... and then save that file to AWS S3 or Cloudflare R2 ...or azure blob storage

Drake
29 Jul, 2023, 19:18

So what can you do with that File? How do you do it against S3?

Drake
29 Jul, 2023, 19:18

And how have you authenticated with Appwrite server side?

snowcode
29 Jul, 2023, 19:19

ok, we're going round in circles, I have a few very basic yes/no questions

  1. Are there any nextjs 13 or even 12 examples of using appwrite in both the client and server side?

I assume the answer 1 is NO. hence the long discussion we're having.

if NO to 1 ...above, then the next question is

  1. what is the best server side example code that I can look at, for creating a** server side REST web api using a node library like Express**?
snowcode
29 Jul, 2023, 19:19

I can figure the rest out on my own if I can get answer to 1 and 2 above. (I think)

snowcode
29 Jul, 2023, 19:20

how I will do whatever I do ...will totally depend on what the example documentation says. I will simply follow the docs, or copy the reference project. Hence why I'm asking if there's an example project anywhere?

snowcode
29 Jul, 2023, 19:21

The examples I did find were all client side using JWT.. and that's great I will need those later, AFTER i've finished building some test REST Api endpoints.

Drake
29 Jul, 2023, 19:27
  1. https://github.com/Meldiron/appwrite-ssr-next-js
  2. It depends on what you need to do. You can just use the node SDK with an API key
snowcode
29 Jul, 2023, 19:38

txs Steven, I'm looking at that now

snowcode
29 Jul, 2023, 20:05
snowcode
29 Jul, 2023, 20:05

Hi Steven this is more advanced than the immediate problem I was trying to solve, but is massively helpful for what I will need to look at soon. (I've looked at the project briefly, and have forked it so that I have a permanent marker to come back to!) regarding my other problems, i will start a fresh blank project and just use the node SDK with API key. I suspect my "File" issue has nothing to do with appwrite perse. Txs for the time and for the link to appwrite-ssr-next-js!

snowcode
29 Jul, 2023, 20:20

apologies, just re-read the thread and you're right. Out of the box the File from browser and server are not the same, and you can't just "process" the stream of the file, that would be handled by middleware.

Which I have not configured, and is probably exactly the problem I'm experiencing.

Thank you! ChatGPT is telling me to look at multer and formidable : I had mistakenly assumed that latest or recent nextjs libs included some opinionated default middleware for stuff like this and if I copied a reference getting started nextjs project it would all be clear and simple.

Lots to learn, phew!

txs again

Drake
29 Jul, 2023, 20:22

Ya I saw formidable in my quick search too

Drake
29 Jul, 2023, 20:23

If you want to use the web sdk, you can look at how we handle the input file. If what you pass in can be used that way, it might work: https://github.com/appwrite/sdk-for-web/blob/7b37a2bec1dbe47b55e2c962996046f56b763192/src/services/storage.ts#L76

snowcode
29 Jul, 2023, 20:26

txs, looking at that now...again, very helpful!

snowcode
29 Jul, 2023, 20:27

I don't think I will "hack" the web sdk, i think i'll use the server sdk, and when i've got the basics right, i will switch to using the web sdk as-is with auth ... which is the real purpose for it any way...

snowcode
29 Jul, 2023, 20:27

client side image uploading works well ... out of the box.

snowcode
30 Jul, 2023, 13:13

(deleted some comments about formidable package because it's off topic and after re-reading realise it's not helpful to other appwrite users or this thread! ciao all, love the support, A)

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