Back

AppwriteException in Storage Bucket Upload for Empty File

  • 0
  • Web
  • Storage
  • Cloud
Ryan
15 Jan, 2024, 13:52

I tried this: (python)

TypeScript
file = "some data of file passed by frontend/client"
filepath = InputFile.from_bytes(file)
        print(filepath)
        response = storage.create_file('attachments', id_class.unique(), filepath)

and it gives the appwrite.exception.AppwriteException: Empty file passed to the endpoint. error. I double checked the file var does contain the contents of the upload file from the frontend

TL;DR
Title: AppwriteException in Storage Bucket Upload for Empty File The error is due to a missing filename in the SDK. The file needs to be encoded properly before sending it over HTTP. If the file is a string, encode it using "utf-8". If it's a different type, handle it accordingly. Check the data type you're receiving from the client and convert it to a byte array if necessary. Make sure the file is uploaded correctly and accessible. Additionally, ensure that the bucket accepts all file types by default. Solution: - If the file is a string: - text_data = "Text data";
Ryan
15 Jan, 2024, 13:53

the bucket has all perms for "Any" role

Ryan
15 Jan, 2024, 14:46

wait, if the file is not binary, does appwrite storage not see it as a file?

Aditya Oberai
15 Jan, 2024, 14:50

InputFile.from_bytes needs your input to be a byte array

In case your input is of a different type, you'll have to convert it first

Ryan
15 Jan, 2024, 14:51

ohh, any idea on how to do that?

Ryan
15 Jan, 2024, 14:51

also, does the bucket accept all file types by default?

Aditya Oberai
15 Jan, 2024, 14:52

This depends on the data type you want to convert

For strings, this might help: https://blog.enterprisedna.co/python-string-to-bytes-3-easy-methods/

Aditya Oberai
15 Jan, 2024, 14:53

Yes

Ryan
15 Jan, 2024, 14:54

ok, thanks, I'll try this and get back to u

Ryan
15 Jan, 2024, 14:56

so I encode the file data to UTF-8? will appwrite accept it?

Ryan
15 Jan, 2024, 15:10
TypeScript
file = str(file).encode("utf-8")
        print(f"FILE: {bytearray(file)}")
        filepath = InputFile.from_bytes(bytearray(file))
        print(f"FILEPATH: {filepath}")
        response = storage.create_file('attachments', id_class.unique(), filepath)

is this correct?

Aditya Oberai
16 Jan, 2024, 12:18

The initial encoding depends on what datatype file was present in, in the first place

For example, if it was a string

TypeScript
text_data = "Text data";
file = bytearray(text_data, "utf-8");
filepath = InputFile.from_bytes(file);
response = storage.create_file("attachments", id_class.unique(), filepath)

That being said, if instead of text_data, there's a variable representing a different type, you'll have to handle it accordingly

Ryan
16 Jan, 2024, 14:37

well, the data is going to be in string format. so I will try this method

Ryan
16 Jan, 2024, 14:45

I printed file and it shows this type of bytearray, still appwrite classifies it at "empty": (message.txt)

Aditya Oberai
16 Jan, 2024, 15:32

I'll try testing on my end too once, if that's the case

Ryan
16 Jan, 2024, 15:33

Ok, Thanks

VincentGe
16 Jan, 2024, 16:23

πŸ˜† Oh yeah this is very common. Python has to many different types of bin, byte, byte array, hex, all kinds of stuff

VincentGe
16 Jan, 2024, 16:24

I don't remember the exact encoding you need for it to work properly when sent over HTTP

VincentGe
16 Jan, 2024, 16:24

Lemme check past projects to see if we have anything that would help

ideclon
16 Jan, 2024, 17:34

Oof, I've dealt with byteencoding before and it was always a pain

VincentGe
16 Jan, 2024, 17:36

@Ryan You know what it is?

VincentGe
16 Jan, 2024, 17:36

InputFile.from_bytes(file) is missing filename

VincentGe
16 Jan, 2024, 17:36

🀣

VincentGe
16 Jan, 2024, 17:38

The error message is confusing but it's because filename is optional in our SDK, but it shouldn't be 😬

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