How to store wordpress media in google cloud storage

I have a Linode server running a wordpress site, the media in /wp-content/uploads its getting big and filling up my server SSD drive. So I want to move it to Google Cloud Storage.

I have checkout WP2CLOUD but requires ClouSE wich does not support MySQL server 5.1.73-1. Im running Debian with linux 2.6.32-5-amd64.

Related posts

Leave a Reply

3 comments

  1. Seems that WP2Cloud (WordPress plugin) may be a perfect solution for you.
    It allows you to either

    • Take the whole website data to cloud storage.
    • Only upload media files to cloud storage.
    • Only migrate website content to cloud storage while store media files in uploads folder on Web server running WordPress.

    Supports both Amazon S3 and Google Cloud Storage

  2. First you need to create a bucket ex. cloud.my-domain.com.


    Get Google Client PHP

    There are plenty of examples already in there for how to authenticate.

    WordPress

    // Run this function on every image upload
    
    add_action('wp_handle_upload', 'process_images');
    function process_images($results) {
    
       if( $results['type'] === 'image/jpeg' || $results['type'] === 'image/png' || $results['type'] === 'image/gif' ) {
           $imgfilename = $results[ 'file' ];
           $imgurl = $results[ 'url' ];
    
            if ( ! is_wp_error( $theme_image ) ) {
              $imageName = wp_basename($imgurl);
              // Save image to cloud storage
              saveToStorage($imgurl, $imageName, $results['type']);
            }
    
           return $results;
      }
    }
    
    function saveToStorage($url, $imageName, $type){
        $objects = callGoogleClient(); 
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $imgData = curl_exec($ch);
    
        $postbody = array('mimeType' => $type, "data" => $imgData);
        $gso = new Google_StorageObject();
        $gso->setName(A_CLOUD_FOLDER.'/'.$imageName);
        $resp = $objects->insert(MY_BUCKET, $gso, $postbody);
    }
    
    // Delete an image from cloud storage
    add_filter('wp_delete_file', 'on_remove_file');
    function on_remove_file($file) {
        $objects = callGoogleClient();
        $name = wp_basename($file);
        $resp = $objects->delete(MY_BUCKET, A_CLOUD_FOLDER.'/'. $name);
      return $file;
    }
    
    
    function callGoogleClient(){
        $client = new Google_Client();
        $client->setClientId('your_id.apps.googleusercontent.com');
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, KEY_FILE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $key = curl_exec($ch);
    
        $client->setClientId(CLIENT_ID);
        $client->setAssertionCredentials(new Google_AssertionCredentials(
          SERVICE_ACCOUNT_NAME,
          array('https://www.googleapis.com/auth/devstorage.full_control'),
          $key)
        );
    
        $StorageService = new Google_StorageService($client);
        $objects = $StorageService->objects;
    
        return $objects;
    }
    

    I have disabled the option Organize my uploads into month- and year-based folders therefore right after an image is uploaded to the storage the wp-upload get’s cleared on every upload.