简体   繁体   中英

appengine std PHP stream wrapper from storage

i'm coding a php web page with a form where the users can upload an excel file.

i'd need to get this file and read it by SpreadsheetReader PHP library

In appengine it's not possible write on filesystem, so i send this file to a Cloud Storage bucket by streamwrapper (work well).

But i don't how read from storage file-like object and load on SpreadsheetReader...

First of all the stream wrapper from storage doesn't work, following my partial code:

$client = new StorageClient(['projectId' => $projectId,

                                ]);
$client->registerStreamWrapper();
$contents = file_get_contents($location);

Return this error: "PHP Fatal error: Uncaught InvalidArgumentException: json_encode error: Malformed UTF-8 characters, possibly incorrectly encoded"

2) how can create a file-like object to do this:

$reader = new SpreadsheetReader($file);
$sheets = $reader->Sheets();

Thank You

$client = new StorageClient(['projectId' => $projectId,

                                ]);
$client->registerStreamWrapper();
$contents = file_get_contents($location);

Return this error: "PHP Fatal error: Uncaught InvalidArgumentException: json_encode error: Malformed UTF-8 characters, possibly incorrectly encoded"

you should provide keyFile in StorageClient in JSON.

see https://github.com/googleapis/google-cloud-php-storage/blob/master/src/StorageClient.php#L78

I understand that you are writing the file to a Cloud Storage bucket by streamwrapper.

Then you try to read the file using:

 $contents = file_get_contents($location);

According to oficial documentation link

However, if an app needs to read files, and these files are static, you can optionally read static files uploaded with your app using PHP filesystem functions such as file_get_contents.

For example:

$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 5 Application Configuration with app.yaml.

I think you want to read files from GCS and not from Application Subdirectory link .

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