Get content from storage file #292
Replies: 3 comments 1 reply
-
More info: I tried to access the downloaded physical file but I get an error when opening it. Is this the correct way to do it, or have I configured something incorrectly? New code : bool downloadStatus = storage.download(aClient, FirebaseStorage::Parent(STORAGE_BUCKET_ID, "file.txt"), getFile(file));
}else{ |
Beta Was this translation helpful? Give feedback.
-
The file is already downloaded to your device if there is no error while downloading. You should not define the File class object in any function to prevent the memory allocation failure in File class because of stack overflow. You should define the File object globally or as static variable as in the examples. |
Beta Was this translation helpful? Give feedback.
-
You should set the file path and the file operation callback with FileConfig object like this. Here is the complete example of ESP32 for file downloading and file uploading of the download file synchronously. You can verify the file in your device after downloaded and file in the console after uploaded. #define ENABLE_USER_AUTH
#define ENABLE_STORAGE
#define ENABLE_FS
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <FirebaseClient.h>
#include <FS.h>
File myFile;
void processData(AsyncResult &aResult);
UserAuth user_auth(API_KEY, USER_EMAIL, USER_PASSWORD, 3000);
FirebaseApp app;
WiFiClientSecure ssl_client;
using AsyncClient = AsyncClientClass;
AsyncClient aClient(ssl_client);
Storage storage;
void authCb(AsyncResult &aResult)
{
if (aResult.isEvent())
Firebase.printf("Event task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.eventLog().message().c_str(), aResult.eventLog().code());
if (aResult.isDebug())
Firebase.printf("Debug task: %s, msg: %s\n", aResult.uid().c_str(), aResult.debug().c_str());
if (aResult.isError())
Firebase.printf("Error task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.error().message().c_str(), aResult.error().code());
}
void fileCb(File &file, const char *filename, file_operating_mode mode)
{
switch (mode)
{
case file_mode_open_read:
myFile = SPIFFS.open(filename, FILE_OPEN_MODE_READ);
break;
case file_mode_open_write:
myFile = SPIFFS.open(filename, FILE_OPEN_MODE_WRITE);
break;
case file_mode_open_append:
myFile = SPIFFS.open(filename, FILE_OPEN_MODE_APPEND);
break;
case file_mode_remove:
SPIFFS.remove(filename);
break;
default:
break;
}
file = myFile;
}
void setup()
{
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
Firebase.printf("Firebase Client v%s\n", FIREBASE_CLIENT_VERSION);
ssl_client.setInsecure();
Serial.println("Initializing app...");
initializeApp(aClient, app, getAuth(user_auth), 120 * 1000, authCb);
app.getApp<Storage>(storage);
if (app.ready())
{
SPIFFS.begin(true);
FileConfig media_file("/media.mp4", fileCb);
Serial.print("Downloading file...");
bool status = storage.download(aClient, FirebaseStorage::Parent(STORAGE_BUCKET_ID, "media.mp4"), getFile(media_file));
if (status)
Serial.println(" complete!");
else
Firebase.printf(" error, msg: %s, code: %d\n", aClient.lastError().message().c_str(), aClient.lastError().code());
Serial.print("Uploading file...");
status = storage.upload(aClient, FirebaseStorage::Parent(STORAGE_BUCKET_ID, "media1.mp4"), getFile(media_file), "video/mp4");
if (status)
Serial.println(" complete!");
else
Firebase.printf(" error, msg: %s, code: %d\n", aClient.lastError().message().c_str(), aClient.lastError().code());
}
}
void loop()
{
app.loop();
}
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello developers,
I have managed to download a file from Firebase storage, but I can't figure out how to access the file's content. Which variable is it in? aClient, file,...
FileConfig file(“file”);
bool downloadStatus = storage.download(aClient, FirebaseStorage::Parent(STORAGE_BUCKET_ID, “file.txt”), getFile(file));
if(downloadStatus){
Serial.println(“🔽 Download complete!✅️”);
Serial.println(XXX);
}else{
Firebase.printf(“Error, msg: %s, code: %d\n”, aClient.lastError().message().c_str(), aClient.lastError().code());
}
I need to obtain the file content and store it in a String or char[] variable.
Regards
Beta Was this translation helpful? Give feedback.
All reactions