It is easier to use Google Cloud Storage for performing storage management functions. The uses of Goggle Cloud Platform Console include a series of activities. The first include activating the Google Cloud Storage App for a particular project. Second, create new buckets and delete irrelevant buckets. Third, upload or download or delete objects and finally manage ACLs or access control lists for particular buckets and objects. The instructions below elaborate on how to upload media with the help of Google cloud storage.
To create a bucket, first create default App engine bucket, go to https://console.developers.google.com/appengine/settings. The App Engine bucket is something like YOUR_PROJECT_ID.appspot.com. Now change it to $ gsutil defacl ch -u AllUsers:R gs://YOUR_PROJECT_ID.appspot.com. By using this command, the new files found in the bucket turns by default publicly readable. The is suitable to use for WordPress Media Storage.
Activating the stream wrapper and installing the app engine PHP SDK
Using the composer you can install the App Engine PHP SDK. It goes like $ composer require google/appengine-php-sdk.
Now add the required extension, it is php.ini:.
extension=bcmath.so
Once this step is done it is time to add the lines given below to wordpress/wp-config.php:
They are as follows:
set_include_path(__DIR__ . '/../vendor/google/appengine-php-sdk');
require_once(__DIR__ . '/../vendor/autoload.php');
stream_wrapper_register(
'gs',
'\google\appengine\ext\cloud_storage_streams\CloudStorageStreamWrapper',
0);
define('MY_BUCKET', 'YOUR_PROJECT_ID.appspot.com');
On the production environment use : gs:// stream wrapper. The disadvantage with the stream wrapper is that it does not work locally.
How to create simple plugin especially for tweaking upload?
To create new file using `wordpress/wp-content/plugins/gcs-media.php` do the following:
<?php
/**
* @package GCS media
* @version 0.1
*/
/*
Plugin Name: GCS media
Plugin URI: https://wp.gaeflex.ninja/
Description: Use Google Cloud Storage for media upload.
Author: Takashi Matsuo
Version: 0.1
Author URI: https://wp.gaeflex.ninja/
License: Apache 2.0
*/
namespace GCS\Media;
defined( 'ABSPATH' ) or die('No direct access!');
add_filter('upload_dir', 'GCS\Media\filter_upload_dir');
function filter_upload_dir( $values ) {
$basedir = 'gs://' . MY_BUCKET;
$baseurl = 'https://storage.googleapis.com/' . MY_BUCKET;
$values = array(
'path' => $basedir . $values['subdir'],
'subdir' => $values['subdir'],
'error' => false,
);
$values['url'] = rtrim($baseurl . $values['subdir'], '/');
$values['basedir'] = $basedir;
$values['baseurl'] = $baseurl;
return $values;
}
Once this is done position the app, then activate the plugin using the ‘activate’ GCS Media plug in. Upload the images if any and then you can see the images on the Google Cloud Storage bucket. But still the image editor does not work. This is not a major issue. Moreover, the stream wrapper does not work in any other environment except Managed VMs. The advantage of using App Engine PHP SDK is the usage of good features of App Engine with managed VMs.
No comments:
Post a Comment