Cannot Use Google Cloud Storage on Local Environment with PHP Standard Env

1,208 views
Skip to first unread message

Thomas Valadez

unread,
Aug 7, 2017, 8:08:13 PM8/7/17
to Google App Engine
Link to SO: https://stackoverflow.com/questions/45539115/use-google-cloud-storage-on-local-environment-with-php-standard-env

I am running PHP Standard Env on Google App Engine. I am trying to get files from my production storage in my local environment.

Here is what my code looks like:

$json = json_decode(file_get_contents("gs://$bucket/$email_id/envelope.json"), true);

It works great in production, however in development I am getting this error:

Warning: file_get_contents(gs://sendgrid-inbound/12/envelope.json): failed to open stream: "\google\appengine\ext\cloud_storage_streams\CloudStorageStreamWrapper::stream_open" call failed in /Users/thom/Engine/site-com/main.php on line 10

I tried downloading GCS API tools composer require google/cloud

require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
echo  __DIR__ .'/appdocs-com.json';
$storage = new StorageClient([
   'keyFilePath' => __DIR__ .'/site-com.json',
]);

$storage->registerStreamWrapper();

This method works great for Flex Env, however in Standard this returns the same error as before.

I have seen comments about setting up an Emulator, but I have found nothing on how to do this. If anyone has a working example of how to do this, I would really like to see how it is done.

George (Cloud Platform Support)

unread,
Aug 8, 2017, 11:35:11 AM8/8/17
to Google App Engine
Hello Thomas, 

In case of the local development server, the Google Cloud Storage stream wrapper is built in to the run time, and is used when you supply a file name starting with gs://. The wrapper requires the name of the bucket or file object to be in the form:

gs://bucket_name/desired_object_name 

You can gather more detail from the "Reading and Writing Files" documentation file

Thomas Valadez

unread,
Aug 8, 2017, 4:22:36 PM8/8/17
to Google App Engine
Thanks for the reply. But it certainly isn't working the way you described. I recently changed to a flex environment to test GCS with the API tools, and file paths worked fine. However, in the standard environment it doesn't work. As stated I get this error:


Warning: file_get_contents(gs://sendgrid-inbound/12/envelope.json): failed to open stream: "\google\appengine\ext\cloud_storage_streams\CloudStorageStreamWrapper::stream_open" call failed in /Users/thom/Engine/site-com/main.php on line 10

I think you may have a bug with the Google Cloud Storage stream wrapper. It works in production just fine, but like I said, no luck on local machine. 

George (Cloud Platform Support)

unread,
Aug 9, 2017, 4:50:30 PM8/9/17
to Google App Engine
Hi Thomas, 

What is the exact command you run? The wrapper requires a certain format for the name of the bucket or file object.

The standard environments offers other running conditions for apps, by reference to flex, there is no expectation to get similar results always. 

Thomas Valadez

unread,
Aug 9, 2017, 7:49:56 PM8/9/17
to Google App Engine
The exact command I am writing is `echo file_get_contents("gs://test-appdocs-sendgrid-inbound/12/envelope.json");` where the bucket title is "test-appdocs-sendgrid-inbound" where "12" is a folder in the bucket and "envelope.json" is the actual file. File options are type is set to "application/json" and "Share Publicly" is set to "Public Link".

George (Cloud Platform Support)

unread,
Aug 10, 2017, 3:05:05 PM8/10/17
to Google App Engine
Hi Thomas, 

Is the same error triggered if you run the `echo file_get_contents("gs://test-appdocs-sendgrid-inbound/envelope.json");` command, which assumes you had copied the envelope.json file in the root directory of the bucket, i.e. under gs://test-appdocs-sendgrid-inbound/envelope.json? 

Thomas Valadez

unread,
Aug 10, 2017, 3:36:55 PM8/10/17
to Google App Engine
Yeah I am getting the same error. I moved the target json file to the root directory of the bucket, and used `echo file_get_contents("gs://test-appdocs-sendgrid-inbound/envelope.json");` as suggested. However I get the same error: 
`Warning: file_get_contents(gs://test-appdocs-sendgrid-inbound/envelope.json): failed to open stream: "\google\appengine\ext\cloud_storage_streams\CloudStorageStreamWrapper::stream_open" call failed in /Users/thom/Engine/appdocs-com/main.php on line 18`

Yannick (Cloud Platform Support)

unread,
Aug 14, 2017, 3:59:14 PM8/14/17
to Google App Engine
Hello Thomas,

At this time, this behavior is working as intended. The error message you are getting means in this situation that no file by that name could be found. That is because the PHP local development server emulates Cloud Storage streams by reading and writing to local storage on your machine and doesn't touch the production Cloud Storage.

Thomas Valadez

unread,
Aug 14, 2017, 4:02:18 PM8/14/17
to google-a...@googlegroups.com
I see... So how do I know where the `("gs://test-appdocs-sendgrid-inbound/envelope.json");` is looking on my local system. 

--
You received this message because you are subscribed to a topic in the Google Groups "Google App Engine" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-appengine/WMA1F5wEK8I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-appengine+unsubscribe@googlegroups.com.
To post to this group, send email to google-appengine@googlegroups.com.
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine/f3b77eb9-2d6f-4feb-b094-431643023314%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Thomas Valadez

unread,
Aug 14, 2017, 4:02:30 PM8/14/17
to Google App Engine
I see... So how do I know where the `("gs://test-appdocs-sendgrid-inbound/envelope.json");` is looking on my local system. 

Yannick (Cloud Platform Support)

unread,
Aug 14, 2017, 4:22:33 PM8/14/17
to Google App Engine
The files are actually stored in the datastore emulator as blobs and are in your system's temp folder. You can specify which folder should be used using the --storage_path option when launching the dev server.

Thomas Valadez

unread,
Aug 14, 2017, 4:32:19 PM8/14/17
to Google App Engine
Cool. Thanks that helps a ton.
Reply all
Reply to author
Forward
0 new messages