Search code examples
phpbucketclever-cloud

FS Bucket - Clever Cloud, how to properly use it?


We're trying to implementate a bucket system in our apps.

enter image description here

FS-Bucket is an alternative provided also by Clever Cloud, that is our PaaS/IaaS provider.

The issue might be in the documentation, that is not really good telling or extensive about the tool at all.

How can we interact with our bucket properly?


Solution

    1. Using the CC_FS_BUCKET mentioned in documentation

    2. Using FTP (boring and slow, but it works).

    Once you adquire the FS Bucket, it will provide you a HOST and other credentials.

    The idea is to set the environment variable in the one app the we need to use the bucket.

    something like this:

    CC_FS_BUCKET=/my-bucket-folder:bucket-xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx-fsbucket.services.clever-cloud.com
    

    That will mount a folder to access directly from our app (** In the execution environmet **)

    enter image description here

    once this is done and deployed, we can try like this SIMPLE example:

    <?php
    
    // this script is located one folder under root, check if your case is similar and modificate the realpath(...) parameter
    define("ROOT_DIR_APP", realpath(__DIR__ ."/../"));
    
    try {
    
        $file = $_GET['file'];
    
        if (empty($file) || !isset($file)) {
            echo "No file specified or included";
            exit;
        }
    
        // check if file exists in current folder
        if (file_exists(ROOT_DIR_APP . "/my-bucket-folder" . $file))
            usingLocalFile(ROOT_DIR_APP . "/my-bucket-folder" . $file); // dont forget to define the ROOT_DIR_APP constant.
    
    
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    
    function usingLocalFile($file = '')
    {
    
        $ext = pathinfo($file, PATHINFO_EXTENSION);
        if (in_array($ext, ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'svg']))
            header('Content-Type: image/' . $ext);
        else if (in_array($ext, ['pdf', 'json']))
            header('Content-Type: application/' . $ext);
        else
            header('Content-Type: application/octet-stream');
    
        header('Content-Disposition: inline; filename="' . basename($file) . '"');
    
        header('Content-Length: ' . filesize($file));
        header('Accept-Ranges: bytes');
    
        @readfile($file);
    
        echo "File Downloaded";
    }
    

    note: This will not work locally unless you create a (git-ignored) folder to simulate the bucket.

    Follow this good part only in the doc

    Don’t forget to replace the path of the mounted folder and the fs-bucket host with the targeted folder path (make sure the folder not exists) and your fs-bucket host