how to get file from internet and save to SPIFFS file system?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
User avatar
AmirJ
Posts: 26
Joined: Tue Sep 03, 2019 9:48 am

how to get file from internet and save to SPIFFS file system?

Post by AmirJ » Thu Sep 19, 2019 8:11 pm

hi
how to get file from web and save to SPIFFS file system?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: how to get file from internet and save to SPIFFS file system?

Post by jimmo » Thu Sep 19, 2019 11:26 pm

MicroPython doesn't use spiffs. However I keep hearing people refer to it, so would be curious if there's some documentation we need to update or something?

Saving the file to the filesystem is the same as any regular file access in Python, using the standard open/read/write methods. (This isn't specific to MicroPython).

Downloading the file is very simple if your file fits in RAM first - you use urequests to get the data, then you can do whatever you like with it (including saving it to a file).

But if it's a big file, you're going to need to save it chunk at a time.

User avatar
AmirJ
Posts: 26
Joined: Tue Sep 03, 2019 9:48 am

Re: how to get file from internet and save to SPIFFS file system?

Post by AmirJ » Fri Sep 20, 2019 5:23 am

thank you
I was wrong.
I mean is the micropython file system.

how to use urequests?
please a example.
Last edited by AmirJ on Fri Sep 20, 2019 7:28 am, edited 1 time in total.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: how to get file from internet and save to SPIFFS file system?

Post by jimmo » Fri Sep 20, 2019 6:04 am

I found these two tutorials with a quick google search: https://techtutorialsx.com/tag/urequests/
They look quite good.

The other thing to remember is that the "urequests" library is based on the Python "requests" library. (This is true of most MicroPython libraries starting with "u", especially the ones in micropython-lib).

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: how to get file from internet and save to SPIFFS file system?

Post by jimmo » Fri Sep 20, 2019 6:04 am

There's also lots of discussion by people using urequests in this forum.

DevNerdChic
Posts: 11
Joined: Wed Feb 23, 2022 5:58 pm

Re: how to get file from internet and save to SPIFFS file system?

Post by DevNerdChic » Fri Mar 25, 2022 11:04 am

jimmo wrote:
Thu Sep 19, 2019 11:26 pm
MicroPython doesn't use spiffs. However I keep hearing people refer to it, so would be curious if there's some documentation we need to update or something?

Saving the file to the filesystem is the same as any regular file access in Python, using the standard open/read/write methods. (This isn't specific to MicroPython).

Downloading the file is very simple if your file fits in RAM first - you use urequests to get the data, then you can do whatever you like with it (including saving it to a file).

But if it's a big file, you're going to need to save it chunk at a time.
I can't seem to get it to work this way, it seems to not have an issue writing the file... maybe... but it won't do anything else with it just keeps giving me the 'could not find file' message if I try to do anything other than create it... which I'm not even sure if it does that since I can do anything else with it to check?

marcidy
Posts: 133
Joined: Sat Dec 12, 2020 11:07 pm

Re: how to get file from internet and save to SPIFFS file system?

Post by marcidy » Sat Mar 26, 2022 7:04 pm

at the lowest level, something like this needs to occur:

Code: Select all

with open('/path/to/filename', 'wb') as file_handle:  # open as binary since socket produces binary data
    file_handle.write(sock.read())
and use the os module to work withi the filesystem:

Code: Select all

>>> import os
>>> os.listdir("/")
path

>>> os.listdir("/path/to")
filename

>>> with open('/path/to/filename', 'r') as f:  # open for reading as text
    print(r.read())
hello!

>>>
it can/should be more complicated than reading the socket directly into the file handle, but at some point, you need to write data into a filehandle that you received from a socket. urequests reads data from the socket into the 'content' property. you can write that directly to the filesystem.

Code: Select all

>>> resp = request(method, url, ...)
>>> with open(path_to_file, 'rb') as f:
    f.write(resp.content)
>>> 
Can you post a reduced example of what you are doing, how saving the data as a file, and how you are trying to access the file?

Post Reply