0
votes

So I'll preface this by stating that my boss is asking me to set up a Google Cloud Storage back up process without having the Google Bucket instance or the .json key file for Google Cloud Platform. Basically, I'm writing a script to batch the process to our Linux and Windows instances of Google Cloud Platform machines to take the back up file that will be stored daily, send it to the Google Cloud Storage, then the delete the file from the system (so that the backup file doesn't take up space on the instance). Since all of this is relatively newer to me, I was hoping to get some advice on whether the key (.json private key) is needed for this process and how to include it; obviously, I don't have the key but I want to add a placeholder until we set that up.

I found Google's documentation pretty helpful but a tad overwhelming. Here is the link to what I have used for reference: https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-code-sample

Here is my code:

    <?php
require_once 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;

// Set up storage client with keyfilepath or .json key necessary?


// Set the parameters for Google cloud bucket name, filename, and path to file.
$bucketName = "fishbowl-storage";
$fileNameA = "backup_filename";
$fileNameB = "quickbook_filename";

$dataBackupSource = "";
$dataQBBackupSource = "";

// Set the function that will upload the file to google Cloud Storage.
function upload_object($bucketName, $fileName, $source)
{

    // Create the storage client and store the file in the cloud.
    $storage = new StorageClient();
    $file = fopen($source, 'r');
    $bucket = $storage->bucket($bucketName);
    $file = $bucket->upload($file, [
        'name' => $fileName
    ]);
    // Print the success message to the user.
    printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $fileName);

}


// Check the OS to determine whether Linux or Windows.
if(php_uname('s') == "Linux"){

    // Set the file path to grab back up.
    $dataBackupSource = "../Fishbowl/backup";
    $dataQBBackupSource = "../Fishbowl/otherbackup";

    // Use try/catch statement to catch issues with private key.
    try{

        // run the function to place both backup files in the Google Cloud Storage Bucket instance.
        upload_object($bucketName, $fileNameA, $dataBackupSource);
        upload_object($bucketName, $fileNameB, $dataQBBackupSource);

        // Delete the old file stored in the path.
        if(!unlink($fileNameA)){
            print ("The file cannot be deleteted or isn't present in this directory...");
        }
        else{
            print ("The file " . $fileNameA . " has been deleted.");
        }
    } 
    catch (Exception $e){
        print "This process failed for the following reason: " . $e;
        return false;
    }
}
else if(php_uname('s') == "Windows NT"){

    // Set the file path to grab back up.
    $dataBackupSource = "../Fishbowl/backup";
    $dataQBBackupSource = "../Fishbowl/otherbackup";

    // Use try/catch statement to catch issues with private key.
    try{

        // run the function to place both backup files in the Google Cloud Storage Bucket instance.
        upload_object($bucketName, $fileNameA, $dataBackupSource);
        upload_object($bucketName, $fileNameB, $dataQBBackupSource);

        // Delete the old file stored in the path.
        if(!unlink($fileNameA)){
            print ("The file cannot be deleteted or isn't present in this directory...");
        }
        else{
            print ("The file " . $fileNameA . " has been deleted.");
        }
    } 
    catch (Exception $e){
        print "This process failed for the following reason: " . $e;
        return false;
    }
}
else{
    print "The operating system has another name...";
}

?>

TLDR: Do I need the JSON Key to be included in my script to pass a file to the Google Cloud Storage and, if so, where do I put it?

1
I found the solution after doing a bit of research and gaining access to the .JSON file needed. Essentially, I needed two additional pieces that reference the authorization. I am placing this here in the instance that others come across a similar problem and need the solution.Josh Greenert
Use this site: zatackcoder.com/upload-file-to-google-cloud-storage-using-php and create the privateKeyFileContent using the .JSON information you download from Google. Then, set your upload function similar to this in the next comment...Josh Greenert
``` lang-php function upload_object($bucketName, $fileName, $source) { $privateKeyFileContent = $GLOBALS['privateKeyFileContent']; // Create the storage client and store the file in the cloud. $storage = new StorageClient([ 'keyFile' => json_decode($privateKeyFileContent, true) ]); $file = fopen($source, 'r'); $bucket = $storage->bucket($bucketName); $object = $bucket->upload($file, [ 'name' => $fileName ]); printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $fileName); } ```Josh Greenert
@Andie, thanks for commentingJosh Greenert

1 Answers

0
votes

I think that this is the documentation that you are looking for. Basically you need to use the Cloud Client Libraries for the Google Cloud Storage API.These are the steps that you need to follow

1.Installing the client library

composer require google/cloud-storage

2.Setting up authentication. In this step you will create the service account and the Key

After setting the environment variable, you don't need to explicitly specify your credentials in code when using a Google Cloud client library as is mention here

3.Use the Client library. In this case you want to upload a file.

use Google\Cloud\Storage\StorageClient;

/**
 * Upload a file.
 *
 * @param string $bucketName the name of your Google Cloud bucket.
 * @param string $objectName the name of the object.
 * @param string $source the path to the file to upload.
 *
 * @return Psr\Http\Message\StreamInterface
 */
function upload_object($bucketName, $objectName, $source)
{
    $storage = new StorageClient();
    $file = fopen($source, 'r');
    $bucket = $storage->bucket($bucketName);
    $object = $bucket->upload($file, [
        'name' => $objectName
    ]);
    printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), 
$bucketName, $objectName);
}