简体   繁体   中英

How to include Google App Engine for PHP in my scripts / autoloading?

I have a website on an Ubuntu webserver (not an app and not hosted at App Engine) and I want to use google cloud storage for the upload/download of large files. I am trying to upload a file directly to the Google Cloud Storage which isn't working (maybe because I made some basic errors).

I have installed the Google Cloud SDK and downloaded and unzipped Google App Engine . If I now include CloudStorageTools.php I get an 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 );

The Google API PHP Client allows you to connect to any Google API, including the Cloud Storage API . Here 's an example, and here 'sa getting-started guide.

If you want to use the functionality of Google App Engine (gae), you will need to host on gae, which will likely have a larger impact on your app architecture (it uses a custom google compiled php version with limited libraries and no local file handling, so all that functionality needs to be into blob store or gcs - Google Cloud Storage).

With a PHP app running on ubuntu, your best bet is to use the google-api-php-client to connect to the storage JSON api. Unfortunately the documentation is not very good for php. You can check my answer in How to rename or move a file in Google Cloud Storage (PHP API) to see how to GET / COPY / DELETE an object. To upload I would suggest to retrieve a pre signed Upload 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 that URL sent to your client side, you can use it to PUT the file directly onto your bucket with an upload script that generates an appropriate PUT request. Here 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 a tough one if you don't want to go google all the way with app engine!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM