#🔒 Gemini file upload without local storage
232 messages · Page 1 of 1 (latest)
@regal ivy
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Learn how to upload files with only a few lines of Python code, including cloud storage, CDN delivery, and dynamic effects for images and media.
import os
import uuid
from django.core.files.storage import default_storage
from .converter import Convert_To_Test
def upload_file(file, quizz_id):
# Valid extensions and maximum file size in bytes
valid_extensions = ['doc', 'docx', 'pdf', 'png', 'jpg']
max_file_size = 10 * 1024 * 1024 # 10 MB
if not file:
return {'error': 'No file provided.'}
# Validate file extension
file_extension = file.name.split('.')[-1].lower()
if file_extension not in valid_extensions:
return {'error': 'Invalid file extension.'}
# Validate file size
if file.size > max_file_size:
return {'error': 'File size exceeds the maximum limit of 10 MB.'}
# Generate a unique filename using UUID
unique_filename = f"{uuid.uuid4().hex}.{file_extension}"
folder_path = 'tmp'
file_path = default_storage.save(os.path.join(folder_path, unique_filename), file)
download_link = default_storage.url(file_path)
print(download_link)
Convert_To_Test(file_path,quizz_id)
return {'success': True, 'download_link': download_link}
I had many
thsi one works
beacuse it installs the file in y local server
but if i send it in vercel it will display an error saying that u can't write
and that it read only
i need a way
to send the file to gemini api without having to save file in my local serv
Is this where you get that error?
what first one
.
def upload_to_gemini(path, mime_type=None):
file = genai.upload_file(path, mime_type=mime_type)
print(f"Uploaded file '{file.display_name}' as: {file.uri}")
return file
Hey @regal ivy!
It looks like you're trying to paste code into this channel.
Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
Where in it?
Uploading to Gemini causes Vercel to say that you can't write?
Didn't you already write when you uploaded the file?
No
cause i use cloudinary
it saves the file for u
it like aws S3 bucket
from my modest understanding
Makes sense
So when i didn't knew how to uplaod file to gimini from cloudinary i tried directly uplaoding it from user submittinghis file i get teh post variable and i send but it does not work cause gemini do not support it
idk how i can like transfer the file from cloudinary to gimini without having to save it in my serv
i tried tos tram it but gemini don't support it either
when i do they respond with a 500 error of their own
then i went to chat gbt and i tired to force the file to go threw with some weird code that i don't understand then it simply denied EOF occurred in violation of protocol (_ssl.c:2396)
https://inspector.pypi.io/project/google-generativeai/0.7.2/packages/f7/92/766c19a6ccdc3e5272ecb831e131672e290d1ca4ec5cd6a4040a78454707/google_generativeai-0.7.2-py3-none-any.whl/google/generativeai/files.py#line.71
https://inspector.pypi.io/project/google-generativeai/0.7.2/packages/f7/92/766c19a6ccdc3e5272ecb831e131672e290d1ca4ec5cd6a4040a78454707/google_generativeai-0.7.2-py3-none-any.whl/google/generativeai/client.py#line.73
oh wow that's hard to read
what is this i'm not a programmer
What are you then?
i just read in google then i try to apply
that the library ?
yea
I was trying to track down what it was using, but I can't see anywhere it's actually writing to disk
What's the actual error you're getting?
!traceback
Please provide the full traceback for your exception in order to help us identify your issue.
While the last line of the error message tells us what kind of error you got,
the full traceback will tell us which line, and other critical information to solve your problem.
Please avoid screenshots so we can copy and paste parts of the message.
A full traceback could look like:
Traceback (most recent call last):
File "my_file.py", line 5, in <module>
add_three("6")
File "my_file.py", line 2, in add_three
a = num + 3
~~~~^~~
TypeError: can only concatenate str (not "int") to str
If the traceback is long, use our pastebin.
No error cause with this code i save to local serv but when i deploy i can not write so my code is uselless
when i deploy i can not write
How do you know that?
Do you get an error telling you that you can't write?
cause it serverless function
yes it a serverless function vercel
wait what
You're using Django in a serverless function?
what is the error
WTF
[ERROR] 2024-09-09T20:40:52.848Z 1dd03ac5-87e2-4282-8d4c-c81349b1ad58 Internal Server Error: /editor
Traceback (most recent call last):
File "/var/task/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/var/task/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/views.py", line 112, in editor
upload_file(file, Actual_Quizz_id)
File "/var/task/quizzy_app/uploader.py", line 30, in upload_file
Convert_To_Test(download_link,quizz_id)
File "/var/task/quizzy_app/converter.py", line 61, in Convert_To_Test
local_file_path = fetch_from_cloudinary(cloudinary_url)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/converter.py", line 20, in fetch_from_cloudinary
with open(temp_filename, 'wb') as f:
^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 30] Read-only file system: 'temp_732c698e24b1453aa309dd5e8f6d64f4.pdf'
- That code doesn't match the code you shared
- That's not in upload, that's in download
File "/var/task/quizzy_app/converter.py", line 61, in Convert_To_Test
? it the same code
Line 61 is blank: https://paste.pythondiscord.com/IQCA#1L61-L61
fetch_from_cloudinary doesn't exist in your paste
quizzy_app/converter.py lines 61 to 64
local_file_path = fetch_from_cloudinary(cloudinary_url)
# Upload file and wait for it to be processed
files = [upload_to_gemini(local_file_path, mime_type="application/pdf")]```
that a line in teh end it not important line it basically deletes the file in cloudinary but it does not even work
That is the line where the error happens
So if you want to fix your problem
Then that is a very important line
ok le me remove it and try i'm pretty sure i tried without it and crashed anyways
[ERROR] 2024-09-09T20:53:27.504Z 63efb40c-e553-4813-816f-3459412462e3 Internal Server Error: /editor
Traceback (most recent call last):
File "/var/task/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/var/task/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/views.py", line 112, in editor
upload_file(file, Actual_Quizz_id)
File "/var/task/quizzy_app/uploader.py", line 30, in upload_file
Convert_To_Test(download_link,quizz_id)
File "/var/task/quizzy_app/converter.py", line 61, in Convert_To_Test
local_file_path = fetch_from_cloudinary(cloudinary_url)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/converter.py", line 20, in fetch_from_cloudinary
with open(temp_filename, 'wb') as f:
^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 30] Read-only file system: 'temp_e38caa575996402a8a6aaa897f6ef46c.pdf'
Clean up temporary file
storage = MediaCloudinaryStorage()
# Delete the file from Cloudinary
storage.delete(local_file_path)
u talking about that line right ?
the repo is public
this code basically tries to downoald the file locally from cloudinary but that not waht i need
i can't write
+ from io import BytesIO
def fetch_from_cloudinary(cloudinary_url):
response = requests.get(cloudinary_url)
if response.status_code == 200:
- # Create a temporary file
- temp_filename = f"temp_{uuid.uuid4().hex}.pdf"
- with open(temp_filename, 'wb') as f:
- f.write(response.content)
- return temp_filename
+ return BytesIO(response.content)
else:
raise Exception("Failed to fetch file from Cloudinary")
how to upload file to gimini then ?
If it works, it will work the same way
?
This code can't work in a serverless function because it trying to downoald file in local serv then upload it
it needs to be uploaded directly
i try this ?
yes
it building
[ERROR] 2024-09-09T21:09:08.310Z 4ee02480-706d-48d0-a8c7-2819ad6cc957 Internal Server Error: /editor
Traceback (most recent call last):
File "/var/task/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/var/task/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/views.py", line 112, in editor
upload_file(file, Actual_Quizz_id)
File "/var/task/quizzy_app/uploader.py", line 30, in upload_file
Convert_To_Test(download_link,quizz_id)
File "/var/task/quizzy_app/converter.py", line 61, in Convert_To_Test
local_file_path = fetch_from_cloudinary(cloudinary_url)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/converter.py", line 20, in fetch_from_cloudinary
with open(temp_filename, 'wb') as f:
^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 30] Read-only file system: 'temp_31d936da441044a48d707b468feee831.pdf'
You didn't change anything
- with open(temp_filename, 'wb') as f:
Should have been removed
But it's still there
i dump
sry
let me retry
def fetch_from_cloudinary(cloudinary_url):
response = requests.get(cloudinary_url)
if response.status_code == 200:
# Create a temporary file
temp_filename = f"temp_{uuid.uuid4().hex}.pdf"
with open(temp_filename, 'wb') as f:
f.write(response.content)
return temp_filename
return BytesIO(response.content)
else:
raise Exception("Failed to fetch file from Cloudinary")
it building
!code
Wait what
You need to remove the red parts
That’s what’s causing your errors
ah lol
okay i did it buuilding
[ERROR] 2024-09-09T21:20:02.052Z ae36dfc9-98b3-435d-bf76-ddee74ed90a1 Internal Server Error: /editor
Traceback (most recent call last):
File "/var/task/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/var/task/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/views.py", line 112, in editor
upload_file(file, Actual_Quizz_id)
File "/var/task/quizzy_app/uploader.py", line 30, in upload_file
Convert_To_Test(download_link,quizz_id)
File "/var/task/quizzy_app/converter.py", line 61, in Convert_To_Test
files = [upload_to_gemini(local_file_path, mime_type="application/pdf")]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/converter.py", line 24, in upload_to_gemini
file = genai.upload_file(path, mime_type=mime_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/google/generativeai/files.py", line 60, in upload_file
path = pathlib.Path(os.fspath(path))
^^^^^^^^^^^^^^^
TypeError: expected str, bytes or os.PathLike object, not BytesIO
so ?
any idea sir ?

Google is forcing you to use a file on disk
See if there’s a different function on Google’s side that lets you upload a file from bytes
That what i'm trying to figure out the hole day
aparently there isn't
The only other thing I can think of is https://docs.python.org/3/library/tempfile.html#tempfile.SpooledTemporaryFile
where does it create tmp files ?
Wdym?
if it going to use the server it won't work
u can't uplaod anything in a serverlesss function
Does it create temporary files in disk ?
it uses ram instead of diak ?
TemporaryFile uses the disk
SpooledTemporaryFile uses memory
well it won't work i can't use disk it a serverless function
ah
how do i use this ?
It should give you a pretend file that stays in memory
The docs have a couple of warnings, I don’t know if they’ll affect you, you’ll have to try it and see
There’s examples in the documentation
ok thx i will try
I’m on mobile now, so I can’t type code very easily
ah okay thank u i will try it
i mean i'm trying it i just wainting the build to end
[ERROR] 2024-09-09T21:40:53.892Z 8505e479-1551-45b3-81e7-b6a9743ad24e Internal Server Error: /editor
Traceback (most recent call last):
File "/var/task/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/var/task/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/views.py", line 112, in editor
upload_file(file, Actual_Quizz_id)
File "/var/task/quizzy_app/uploader.py", line 30, in upload_file
Convert_To_Test(download_link,quizz_id)
File "/var/task/quizzy_app/converter.py", line 64, in Convert_To_Test
files = [upload_to_gemini(local_file_path, mime_type="application/pdf")]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/converter.py", line 27, in upload_to_gemini
file = genai.upload_file(path, mime_type=mime_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/google/generativeai/files.py", line 60, in upload_file
path = pathlib.Path(os.fspath(path))
^^^^^^^^^^^^^^^
TypeError: expected str, bytes or os.PathLike object, not SpooledTemporaryFile ?
idk why it did this

gemini couldn't just take links 😮💨
Why are you trying to use serverless functions anyways?
I do not
i think maybe i will try to upload to drive with bytes if they allow and then i think i can use file in drive to prompt it into ai
idk if it will work
if not i think i will do my own ocr
and txt extraction
it can not be done
#python-discussion message
Go back to the code you had at the start
And set location you’re saving files to to /tmp
it did not work
how big is that temp file?
between 5 and 10 kb usually
but it ddin't work
Because you're using SpooledTemporaryFile
what will that do
will it save file in disk ?
def fetch_from_cloudinary(cloudinary_url):
response = requests.get(cloudinary_url)
if response.status_code == 200:
file = SpooledTemporaryFile()
file.write(response.content)
file.seek(0) # Ensure the file pointer is at the start of the file
return file
else:
raise Exception("Failed to fetch file from Cloudinary")
can u show me what to modify sir ?
!code
!d tempfile.mktemp is probably the best option
tempfile.mktemp(suffix='', prefix='tmp', dir=None)```
Deprecated since version 2\.3: Use [`mkstemp()`](https://docs.python.org/3/library/tempfile.html#tempfile.mkstemp) instead.
Return an absolute pathname of a file that did not exist at the time the call is made. The *prefix*, *suffix*, and *dir* arguments are similar to those of [`mkstemp()`](https://docs.python.org/3/library/tempfile.html#tempfile.mkstemp), except that bytes file names, `suffix=None` and `prefix=None` are not supported.
The deprecated one?
https://paste.pythondiscord.com/EEYQ like this ?
Since 2.3!?
if you use NamedTemporaryFile, you can get the file name via f.name
[ERROR] 2024-09-09T22:12:53.725Z aaf1ff84-f00f-429a-af2a-e17d088a3ce5 Internal Server Error: /editor
Traceback (most recent call last):
File "/var/task/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/var/task/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/views.py", line 112, in editor
upload_file(file, Actual_Quizz_id)
File "/var/task/quizzy_app/uploader.py", line 30, in upload_file
Convert_To_Test(download_link,quizz_id)
File "/var/task/quizzy_app/converter.py", line 69, in Convert_To_Test
local_file_path = fetch_from_cloudinary(cloudinary_url)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Convert_To_Test.<locals>.fetch_from_cloudinary() missing 1 required positional argument: 'filename'
with tempfile.NamedTemporaryFile() as f:
f.write("hahahahah")
f.seek(0)
upload_file(f.name, ...)
man please write the code for me i don't understand shit 🥺
that the gihub ripo
waittttt
it worked but why did it crash ?
504 gateway timeout what does this even mean ?
The HTTP 504 Gateway Timeout server error response status code indicates that the server, while acting as a gateway or proxy, did not get a response in time from the upstream server in order to complete the request.
This is similar to a 502 Bad Gateway, except that in a 504 status, the proxy or gateway did not receive any HTTP response from th...
.httpstatus 504
So how to solve lol ?
go faster
do less
hh what ?
Because you're using serverless functions, you only have a certain number of seconds to finish everything you're doing before the server gets shut down and you get kicked off
ahhhh
okay okay
Can i like chunck my requests or something ?
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.