1

I have files in my Google Cloud store that I would like to serve embedded and for download on my site set up with GAE and PHP but I can't seem to get them to serve.

I've been looking at: https://developers.google.com/appengine/docs/php/refdocs/files/api.cloud_storage.CloudStorageTools#\google\appengine\api\cloud_storage\CloudStorageTools

The serve function doesn't seem to work for me, or likely I'm not employing it properly.

Should I not just go serve($gs_filename); to grab the file? Any help would be appreciated! Thanks.

UPDATE:

require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;

$bucket = 'raven-bucket';
$recording_name = '/194-14-02-2014rec.ogg';
$recording_data = CloudStorageTools::serve('gs://'.$bucket.$recording_name, ['content_type' => 'audio/ogg']);

This is what I've got so far, but I'm not sure what serve(); is actually returning. How would I go about embedding this audio file in an HTML 5 audio element or making a download link?

UPDATE 2: This works!

<audio controls>
    <source src="test.php" type="audio/ogg">
</audio>

test.php:

<?php

require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;
CloudStorageTools::serve('gs://raven-bucket/194-14-02-2014rec.ogg', 
    ['content_type' => 'audio/ogg']);
Guy
  • 876
  • 1
  • 10
  • 28

2 Answers2

2

You can access files stored in cloud storage buckets natively, without using the CloudStorageTools.

You will need to:

  1. Add permissions to the bucket / files for your App Engine apps service account e.g. your-app@appspot.gserviceaccount.com
  2. Create a php.ini file and put in a google_app_engine.allow_include_gs_buckets directive specifying the buckets you wish to access (See https://developers.google.com/appengine/docs/php/config/php_ini#GAE_directives)

  3. finally you can then just:

Include:

<? include("gs://your-bucket/file.php"); ?>

Or read the contents of the files:

<? $content = file_get_contents("gs://your-bucket/file.txt"); ?>
IanGSY
  • 3,664
  • 1
  • 22
  • 40
2

CloudStorageTools::serve() is the right way to do this, for a few reasons

  1. It is more efficient - when you use CloudStorageTools::serve() the file is served by the App Engine infrastructure, so you do not occur the cost of reading the file in your instance.
  2. You can return files of any size using serve(). Reading the file using file_get_contents() and echoing that limits you to files of 32MB or less.

You can see the technique being used in our blogpost for generating dynamic sitemaps for WordPress - under the heading "Create a request handler", the code is essentially.

Option 1.

If you want to return the file '194-14-02-2014rec.ogg' in the bucket 'raven-bucket' as the response to a request then your php script looks like.

<?php

use google\appengine\api\cloud_storage\CloudStorageTools;
CloudStorageTools::serve('gs://raven-bucket/194-14-02-2014rec.ogg', 
                         ['content_type' => 'audio/ogg']);

Option 2.

You want the user to download the file '194-14-02-2014rec.ogg' in the bucket 'raven-bucket', so that it is saved on their local computer.

<?php

use google\appengine\api\cloud_storage\CloudStorageTools;
CloudStorageTools::serve('gs://raven-bucket/194-14-02-2014rec.ogg', 
                         ['save_as' => '194-14-02-2014rec.ogg', 
                          'content_type' => 'audio/ogg']);
Stuart Langley
  • 7,044
  • 1
  • 20
  • 20
  • This doesn't seem to work for me... I've even tried setting `$bucket` with the `get_option();` function like the example. Still nothing. – Guy Mar 06 '14 at 05:43
  • No it doesn't. The variable (`$recording_data`) doesn't get set and if I use the `$get_option();` the page doesn't load. – Guy Mar 06 '14 at 06:43
  • CLoudStorageTools::serve doesn't return anything - you just call it and exit the script. It sets a header that we pick up in the serving infrastructure to serve the file. – Stuart Langley Mar 06 '14 at 06:45
  • So how do I go about getting a `.ogg` file from Cloud Storage and making it available for download and playback? – Guy Mar 06 '14 at 07:08
  • BTW here's a site that uses this technique to serve file from GCS -> http://audio-test.appspot.com/ – Stuart Langley Mar 06 '14 at 08:43