How do I enable Google App Engine for PHP in my scripts / autoloading?
I have a website on an Ubuntu web server (not an application and not hosted on App Engine) and I want to use google cloud storage to upload / download large files. I'm trying to upload a file directly to Google Cloud Storage which doesn't work (possibly because I made some basic mistakes).
I have installed Google Cloud SDK and downloaded and unpacked the Google Engine app . If enabled now CloudStorageTools.php
I get the error:
Class 'google \ appengine \ CreateUploadURLRequest' not found "
My script looks like this:
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;
$options = [ 'gs_bucket_name' => 'test' ];
$upload_url = CloudStorageTools::createUploadUrl( '/test.php' , $options );
source to share
If you want to use Google App Engine (gae) functionality, you will need to host on gae, which will likely have a greater impact on the architecture of your application (it uses a custom compiled PHP version of PHP with limited libraries and no local file handling. so all this functionality should be in blob or gcs storage - Google Cloud Storage).
With a PHP application running on ubuntu, it is best to use google-api-php client to connect to the JSON api store. Unfortunately, the documentation is not very good for php. You can check my answer at How to Rename or Move a File to Google Cloud Storage (PHP API) to see how to GET / COPY / DELETE create an object. For download, I would suggest downloading a pre-signed download url like so:
//get google client and auth token for request
$gc = \Google::getClient();
if($gc->isAccessTokenExpired())
$gc->getAuth()->refreshTokenWithAssertion();
$googleAccessToken = json_decode($gc->getAccessToken(), true)['access_token'];
//compose url and headers for upload url request
$initUploadURL = "https://www.googleapis.com/upload/storage/v1/b/"
.$bucket."/o?uploadType=resumable&name="
.urlencode($file_dest);
//Compose headers
$initUploadHeaders = [
"Authorization" =>"Bearer ".$googleAccessToken,
"X-Upload-Content-Type" => $mimetype,
"X-Upload-Content-Length" => $filesize,
"Content-Length" => 0,
"Origin" => env('APP_ADDRESS')
];
//send request to retrieve upload url
$req = $gc->getIo()->makeRequest(new \Google_Http_Request($initUploadURL, 'POST', $initUploadHeaders));
// pre signed upload url that allows client side upload
$presigned_upload_URL = $req->getResponseHeader('location');
With this url sent to your client side, you can use it to put the file directly in your bucket with a downloadable script that generates the appropriate PUT request. Here's an example in AngularJS with ng-file-upload:
file.upload = Upload.http({
url: uploadurl.url,
skipAuthorization: true,
method: 'PUT',
filename: file.name,
headers: {
"Content-Type": file.type !== '' ? file.type : 'application/octet-stream'
},
data: file
});
Good luck - gcs is tough unless you want to google completely using the app engine!
source to share
The Google API Client allows you to connect to any Google API, including Cloud Storage API . Here's an example and here's a getting started guide.
source to share