Skip to content

cloud_storage

YYBartT edited this page Dec 11, 2024 · 7 revisions

Cloud Storage Overview

Setup

Before starting to use any Firebase extensions, you are required to follow some initial configuration steps.

Functions

The following functions are provided for working with the Firebase Cloud Storage extension:



Back To Top

FirebaseStorage_Cancel

This function is used to cancel an upload (see FirebaseStorage_Upload) or download (see FirebaseStorage_Download) process.


Syntax:

FirebaseStorage_Cancel(listener)
Argument Type Description
listener Real The upload/download asynchronous listener



Returns:

N/A


Example:

listener = FirebaseStorage_Upload(localfile, path);

// After some time
FirebaseStorage_Cancel(listener);

The code above will start a file upload to the given path (using the FirebaseStorage_Upload function) and after a while cancel it.




Back To Top

FirebaseStorage_Delete

This function deletes a path in the Firebase Cloud Storage and returns a listener identifier that can be used for tracking progress on the request.

This function operates asynchronously, which means that it does not immediately return the requested result. Instead, upon completion of the task, it will trigger the Social Async Event.


Syntax:

FirebaseStorage_Delete(firebasePath, bucket=undefined)
Argument Type Description
firebasePath String The remote path on the Firebase Cloud Storage server
bucket String Other Firebase Storage bucket



Returns:

Real


Triggers:

Social Async Event

Key Type Description
type String The string "FirebaseStorage_Delete"
listener Real The asynchronous listener ID.
path String The remote path.
success Boolean Whether or not the delete succeeded

Example:

var _uid = FirebaseAuthentication_GetUID();
listenerId = FirebaseStorage_Delete("UserProfiles/" + _uid + "/img.png");

The code above uses the user identifier (provided by the function FirebaseAuthentication_GetUID from the Firebase Authentication extension) to create a remote path and then deletes a file in that path. The function call will then return a listener ID (listenerId) that can be used inside a Social Async Event.

if (async_load[? "type"] == "FirebaseStorage_Delete")
{
    if (async_load[? "success"])
    {
        show_debug_message("File was deleted");
    }
    else
    {
        show_debug_message("File could not be deleted");
    }
}

The code above matches the response against the correct event type and logs whether the task was completed successfully or not.




Back To Top

FirebaseStorage_Download

This function is used to download files from a remote path and returns a listener identifier that can be used for tracking progress on the request.

Warning

This function is not available on the Web target.

This function operates asynchronously, which means that it does not immediately return the requested result. Instead, upon completion of the task, it will trigger the Social Async Event.


Syntax:

FirebaseStorage_Download(localPath, firebasePath, bucket=undefined)
Argument Type Description
localPath String The local path where the files will be downloaded
firebasePath String The remote path on the Firebase Cloud Storage server
bucket String Other Firebase Storage bucket



Returns:

Real


Triggers:

Social Async Event

Key Type Description
type String The string "FirebaseStorage_Download"
listener Real The asynchronous listener ID.
path String The remote file path.
localPath String The local file path.
success Boolean Whether or not the download succeeded (if finished)
error String A string describing the error that occurred (if any)
transferred Real Number of transferred bytes (if NOT finished)
total Real Total number of bytes (if NOT finished)

Example:

var _uid = FirebaseAuthentication_GetUID();
listenerId = FirebaseStorage_Download("profilePic.png", "UserProfiles/" + _uid + "/img.png");

The code above uses the user identifier (provided by the function FirebaseAuthentication_GetUID from the Firebase Authentication extension) to create a remote path string, then downloads a file from the server to a local path. The function call will then return a listener ID (listenerId) that can be used inside a Social Async Event.

if (async_load[? "type"] == "FirebaseStorage_Download")
{
    if (ds_map_exists(async_load[? "success"]))
    {
        if (async_load[? "success"])
        {
            global.percent = 100;
            img = sprite_add(async_load[?"localPath"], 0, 0, 0, 0, 0);
            show_message_async("Download SUCCESS");
        }
        else
        {
            global.percent = 0;
            show_message_async("Download FAILED");
        }
    }
    else
    {
        global.percent = 100 * async_load[? "transferred"] / async_load[? "total"];
    }
}

The code above matches the response against the correct event type and if the download has not finished yet ("success" key is not present) then calculates the download percentage and stores it inside a global variable (global.percent). When the download finishes it checks if it was a successful download and if so it adds the downloaded file as a sprite (using the sprite_add function).

Note

Always remember to free sprites added this way when you no longer need them (using sprite_delete), otherwise it can lead to memory leaks.




Back To Top

FirebaseStorage_GetURL

This function asynchronously retrieves a long lived download URL with a revocable token. This can be used to share the file with others, but can be revoked by the developer in the Firebase Console if desired.

This function operates asynchronously, which means that it does not immediately return the requested result. Instead, upon completion of the task, it will trigger the Social Async Event.


Syntax:

FirebaseStorage_GetURL(firebasePath, bucket=undefined)
Argument Type Description
firebasePath String The remote path on Firebase Cloud Storage server.
bucket String Other Firebase Storage bucket



Returns:

Real


Triggers:

Social Async Event

Key Type Description
type String The constant "FirebaseStorage_GetURL"
listener Real The asynchronous listener ID.
path String The remote file path.
success Boolean Whether or not the task succeeded
value String The URL of the file (if task succeeded)

Example:

listenerId = FirebaseStorage_GetURL(path);

The code above tries to retrieve an URL from a remote path on the Firebase Cloud Storage server. The function call will then return a listener ID (listenerId) that can be used inside a Social Async Event.

if (async_load[? "type"] == "FirebaseStorage_GetURL")
{
    if (async_load[? "success"])
    {
        show_debug_message(async_load[? "path"] + " URL: " + async_load[? "value"]);
    }
    else
    {
        show_debug_message("There was an error retrieving the URL");
    }
}

The code above matches the response against the correct event type and if the task was successful it logs the retrieved URL for the specific path.




Back To Top

FirebaseStorage_List

This function queries the contents of a Firebase Cloud Storage path, if pageToken is provided the query will start after the page number provided. It also returns a listener identifier that can be used for tracking progress on the request.

This function operates asynchronously, which means that it does not immediately return the requested result. Instead, upon completion of the task, it will trigger the Social Async Event.


Syntax:

FirebaseStorage_List(path, maxResults, pageToken=undefined, bucket=undefined)
Argument Type Description
path String The remote Firebase path to query
maxResults Real The max number of results
pageToken String Token of the last request
bucket String Other Firebase Storage bucket



Returns:

Real


Triggers:

Social Async Event

Key Type Description
type String The string "FirebaseStorage_List"
listener Real The asynchronous listener ID.
path String The remote path.
success Boolean Whether or not the task succeeded
files String A JSON formatted string of an array of file paths.
folders String A JSON formatted string of an array of folder paths.
pageToken Real The current query page.

Example:

listenerId = FirebaseStorage_List("/testing", 5);

The code above queries for files and folders inside the path "/testing", returning a maximum of 5 results. The function call will then return a listener ID (listenerId) that can be used inside a Social Async Event.

if (async_load[? "type"] == "FirebaseStorage_List")
{
    if (async_load[? "success"])
    {
        global.arrayOfFiles = json_parse(async_load[? "files"]);
        global.arrayOfFolders = json_parse(async_load[? "folders"]);
    }
}

The code above matches the response against the correct event type and if the task was successful it stores the files and folders paths parsed as arrays (using the json_parse function) inside global variables (global.arrayOfFiles and global.arrayOfFolders).




Back To Top

FirebaseStorage_ListAll

This function queries all the contents of a Firebase Cloud Storage path and returns a listener identifier that can be used for tracking progress on the request.

This function operates asynchronously, which means that it does not immediately return the requested result. Instead, upon completion of the task, it will trigger the Social Async Event.


Syntax:

FirebaseStorage_ListAll(path, bucket)
Argument Type Description
path String The remote Firebase path to query
bucket String Other Firebase Storage bucket



Returns:

Real


Triggers:

Social Async Event

Key Type Description
type String The string "FirebaseStorage_ListAll"
listener Real The asynchronous listener ID.
path String The remote path.
success Boolean Whether or not the task succeeded
files String A JSON formatted string of an array of file paths.
folders String A JSON formatted string of an array of folder paths.

Example:

listenerId = FirebaseStorage_ListAll("/testing");

The code above queries for all files and folders inside the path "/testing". The function call will then return a listener ID (listenerId) that can be used inside a Social Async Event.

if (async_load[? "type"] == "FirebaseStorage_ListAll")
{
    if (async_load[? "success"])
    {
        global.arrayOfFiles = json_parse(async_load[? "files"]);
        global.arrayOfFolders = json_parse(async_load[? "folders"]);
    }
}

The code above matches the response against the correct event type and if the task was successful it stores the files and folders paths parsed as arrays (using the json_parse function) inside global variables (global.arrayOfFiles and global.arrayOfFolders).




Back To Top

FirebaseStorage_Upload

This function is used to upload files to a remote path. It returns a listener identifier that can be used for tracking progress.

A Social Async Event is triggered at various points during the upload providing information on upload progress, completion or any errors that occurred.

This function operates asynchronously, which means that it does not immediately return the requested result. Instead, upon completion of the task, it will trigger the Social Async Event.


Syntax:

FirebaseStorage_Upload(localPath, firebasePath, bucket=undefined)
Argument Type Description
localPath String The local path
firebasePath String The remote path on the Firebase Cloud Storage server
bucket String Other Firebase Storage bucket



Returns:

Real


Triggers:

Social Async Event

Key Type Description
type String The string "FirebaseStorage_Upload"
listener Real The listener for which this event is triggered
path String The remote file path
localPath String The local file path
success Boolean Whether the upload succeeded
error String An error message describing the error that occurred
transferred Real The number of bytes transferred
total Real The total number of bytes