GAE PHP application: Unable to find the wrapper “g

2019-07-30 00:02发布

问题:

I am writing some very simple code in the standard php73 Google App engine environment, following the documentation here: https://cloud.google.com/appengine/docs/standard/php/googlestorage/ and https://cloud.google.com/appengine/docs/standard/php/googlestorage/setup

php.ini (not required according to docs for this scenario, but just in case)

google_app_engine.allow_include_gs_buckets = "#default#"

index.php:

file_put_contents("gs://#default#/hello.txt", "some text");

and getting the following error from Google App Engine

file_put_contents(): Unable to find the wrapper "gs" - did you forget to enable it when you configured PHP?

As far as I can see from the documentation there should be no other configuration required, as GAE registers the file stream wrapper automatically in their environment.

What am I missing? Thanks!

回答1:

So it turns out the documentation is for the PHP 5 environment not PHP 7 (though that's not stated). The way to get this working in PHP 7 is documented here:

https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/appengine/php72/storage/src

I just did this:

use Google\Cloud\Storage\StorageClient; 
function register_stream_wrapper($projectId) {   
    $client = new StorageClient(['projectId' => $projectId]);
    $client->registerStreamWrapper();
}
register_stream_wrapper("projectId");

to register the wrapper.



回答2:

An App Engine application, you cannot write to the file-system where your application is deployed. Your application can read any files from the deployed directory structure, but it can’t write to that file-system. Instead, the application can use Google Cloud Storage (GCS) for both reading and writing files.

Modify some app file paths to point to a GCS bucket in your Cloud Project Modify app file upload forms so that they use GCS to receive the uploaded files

You can optionally read static files uploaded with your app using PHP filesystem functions such as file_get_contents.

$fileContents = file_get_contents($filePath);

where the path specified must be a path relative to the script accessing them.

You must upload the file or files in an application subdirectory when you deploy your app to App Engine, and must configure the app.yaml file so your app can access those files. For complete details, see PHP Application Configuration with app.yaml.

https://cloud.google.com/appengine/docs/standard/php/googlestorage/

In the app.yaml configuration, notice that if you use a static file or directory handler (static_files or static_dir) you must specify application_readable set to true or your app won't be able to read the files. However, if the files are served by a script handler, this isn't necessary, because these files are readable by script handlers by default.

The second option is to run your application outside of App Engine, Google provide XML and JSON APIs. Documentation, libraries and a sample application are available on the GCS developer website.