简体   繁体   English

appengine std PHP stream 来自存储的包装器

[英]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.我正在编写一个 php web 页面,用户可以在其中上传 excel 文件。

i'd need to get this file and read it by SpreadsheetReader PHP library我需要获取此文件并通过 SpreadsheetReader PHP 库读取它

In appengine it's not possible write on filesystem, so i send this file to a Cloud Storage bucket by streamwrapper (work well).在 appengine 中,不可能在文件系统上写入,所以我通过 streamwrapper 将此文件发送到 Cloud Storage 存储桶(运行良好)。

But i don't how read from storage file-like object and load on SpreadsheetReader...但我不知道如何从 object 之类的存储文件中读取并加载到 SpreadsheetReader...

First of all the stream wrapper from storage doesn't work, following my partial code:首先,按照我的部分代码,存储中的 stream 包装器不起作用:

$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"返回此错误:“PHP 致命错误:未捕获的 InvalidArgumentException:json_encode 错误:格式错误的 UTF-8 字符,可能编码错误”

2) how can create a file-like object to do this: 2)如何创建一个类似 object 的文件来做到这一点:

$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"返回此错误:“PHP 致命错误:未捕获的 InvalidArgumentException:json_encode 错误:格式错误的 UTF-8 字符,可能编码错误”

you should provide keyFile in StorageClient in JSON.您应该在 JSON 的 StorageClient 中提供 keyFile。

see https://github.com/googleapis/google-cloud-php-storage/blob/master/src/StorageClient.php#L78https://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.我了解到您正在通过 streamwrapper 将文件写入 Cloud Storage 存储分区。

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.但是,如果一个应用程序需要读取文件,并且这些文件是 static,您可以选择使用 PHP 文件系统功能如 file_get_contents 读取随您的应用程序上传的 static 文件。

For example:例如:

$fileContents = file_get_contents($filePath); $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.将应用程序部署到 App Engine 时,您必须上传应用程序子目录中的一个或多个文件,并且必须配置 app.yaml 文件,以便您的应用程序可以访问这些文件。 For complete details, see PHP 5 Application Configuration with app.yaml.有关完整的详细信息,请参阅 PHP 5 使用 app.yaml 的应用程序配置。

I think you want to read files from GCS and not from Application Subdirectory link .我认为您想从 GCS 而不是从 Application Subdirectory链接读取文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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