# Using nakama storage for user uploaded images

**URL:** https://forum.heroiclabs.com/t/using-nakama-storage-for-user-uploaded-images/4477
**Category:** Game Design
**Created:** [October 18, 2023, 8:01am UTC](https://forum.heroiclabs.com/t/using-nakama-storage-for-user-uploaded-images/4477 "2023-10-18T08:01:15Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![rpparede](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@rpparede](https://forum.heroiclabs.com/u/rpparede)
#### Post date: [October 18, 2023, 8:01am UTC](https://forum.heroiclabs.com/t/using-nakama-storage-for-user-uploaded-images/4477/1 "2023-10-18T08:01:15Z")

</div>

Hello folks, I wanted to pick your brain on a decision I have to make around image uploads. I have private rooms all set up and working with nakama. My game involves uploading multiple images before starting the game. These images will likely be large as they can come from the user’s gallery (expect most users to be mobile users). I found this thread which seems relevant and aligns with what I had in mind.

> [@Suggestion about Avatars storage](https://forum.heroiclabs.com/t/suggestion-about-avatars-storage/762):
>
> Hello ! We are building a mobile game with Unity and Nakama, we need to upload custom profile avatars for each user, as I see in the documentation there is an “avatar\_url” field but there is no way to upload the avatar picture using nakama, so I think we could handle this by our own. We are working on Google Cloud so we decided to use Google Cloud Storage, but it has the drawback to allow users’ authentication only through Firebase, so we can’t upload pictures directly from the client. We thou…

Currently, I’m base64’ing the string on my client and using nakama client to upload those images directly to the nakama storage. The problem is that the image has no validation or pre-processing yet. Ideally, I would like to do this from the server side (probably with a custom rpc) where I can receive the image, pre-process it, bucket it to the right collection, and then store it. The current approach only works with small images, I expect to handle 15 photos per private room which will eventually increase. I wanted to get your thoughts on this. My only experience storing images has been using a S3 buckets, so I’m not sure if storing it in a base64 string is a good practice or not. I also considered storing these images directly on memory and storing a path-to-image on the database but I worry this might get too expensive in the long term.

Thanks!

1. Versions: Nakama {3.5}, {Windows, Mac, Linux binary or Docker}, {client library (SDK) and version}
2. Server Framework Runtime language (If relevant) {Go, TS/JS, Lua}

```auto
{code or log snippet}

```

📺 **Media:**

---

<div class="post-metadata">

### Author: ![tom](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/tom/32/676_2.png) [@tom](https://forum.heroiclabs.com/u/tom)
#### Post date: [October 18, 2023, 8:30am UTC](https://forum.heroiclabs.com/t/using-nakama-storage-for-user-uploaded-images/4477/2 "2023-10-18T08:30:06Z")

</div>

Hi @rpparede,

As Andrei mentioned in the thread you linked to, you can certainly store base64 encoded images in the Nakama Storage engine. Though you should consider placing appropriate file size limitations. I would also recommend using an RPC as you’ve mentioned to authoritatively process the image before storing (verify it is an image etc etc).

The alternative of course would be to use a dedicated image storage service for this instead and then store the URL for the image in Nakama storage. This would come with the benefit of the service provider doing all of the validation for you.

---

<div class="post-metadata">

### Author: ![rpparede](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@rpparede](https://forum.heroiclabs.com/u/rpparede)
#### Post date: [October 23, 2023, 7:53am UTC](https://forum.heroiclabs.com/t/using-nakama-storage-for-user-uploaded-images/4477/3 "2023-10-23T07:53:24Z")

</div>

Thanks for the reply @tom . I’m trying to implement the RPC way. However I’m kinda stuck in getting this to work.  
I figured that calling the RPC function directly from the socket does not work, I was trying to pass the body to the RPC without success.  
await ctx.client.rpc(ctx.session, rpcid, {“image”:images});

I’m now making a POST request using fetch that lets me send the body to the RPC and receive it on the backend. I’m currently using a FormData to append all the images I need to send to the server after the user uploads them. I’m then sending the form data in the body of the request as follows:

```
fetch('http://127.0.0.1:7350/v2/rpc/image-upload?http_key=defaulthttpkey&unwrap', {
  method: 'POST',
  body: data,
  headers: {
     'Content-type': 'multipart/form-data',
  },

```

})

However, I keep running into unmarshalling errors on the go lang backend. I’m also not sure if nakama rpc handles multipart/form-data transfers. In the docs I only saw json being passed around.

Could you please provide an example if possible on how to submit multiple (\<1GB) images to the RPC endpoint. It would be much appreciated. Thanks!

---

<div class="post-metadata">

### Author: ![tom](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/tom/32/676_2.png) [@tom](https://forum.heroiclabs.com/u/tom)
#### Post date: [October 23, 2023, 8:23am UTC](https://forum.heroiclabs.com/t/using-nakama-storage-for-user-uploaded-images/4477/4 "2023-10-23T08:23:25Z")

</div>

You need to ensure you’re base 64 encoding the images as a string and then pass the payload as a JSON string. For example, the payload should look something like:

```auto
{
  "images": [
    "aGVsbG8=",
    "d29ybGQ="
  ]
}

```

Note: We don’t support `multipart/form-data` content via RPC.

I would advise using the standard `client.rpc()` function and passing the payload as a string as intended.

---

<div class="post-metadata">

### Author: ![rpparede](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@rpparede](https://forum.heroiclabs.com/u/rpparede)
#### Post date: [October 27, 2023, 5:17am UTC](https://forum.heroiclabs.com/t/using-nakama-storage-for-user-uploaded-images/4477/5 "2023-10-27T05:17:21Z")

</div>

Thanks for the answer @tom, can this be also done using gRPC streaming possibly?  
I had to override the yaml configs to accept large messages. I worry this approach seems very hacky or it may open a door for performance issues in the future.  
local.yml  
name: nakama-node-1  
data\_dir: “./data/”  
socket:  
max\_message\_size\_bytes: 1000000  
max\_request\_size\_bytes: 1000000  
read\_buffer\_size\_bytes: 1000000  
write\_buffer\_size\_bytes: 1000000  
cluster:  
max\_message\_size\_bytes: 1000000  
console:  
max\_message\_size\_bytes: 1000000

---

<div class="post-metadata">

### Author: ![tom](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/tom/32/676_2.png) [@tom](https://forum.heroiclabs.com/u/tom)
#### Post date: [October 27, 2023, 9:35am UTC](https://forum.heroiclabs.com/t/using-nakama-storage-for-user-uploaded-images/4477/6 "2023-10-27T09:35:33Z")

</div>

I’m not aware of anyone using the realtime socket in this way, so I would need to know more about how you have implemented this to comment. Again, if your images are very large I would definitely recommend using a dedicated image hosting service rather than storing them in the Nakama storage engine.
