简体   繁体   English

结合使用XMLReader和Google App Engine云存储

[英]Using XMLReader with Google App Engine Cloud Storage

I have a 30MB XML file which I'd like to process using Google App Engine (PHP). 我有一个30MB的XML文件,我想使用Google App Engine(PHP)处理。 Because the file is so big, the suggested storage is Google Cloud Storage, so I've placed it there. 由于文件太大,建议的存储空间是Google Cloud Storage,因此我将其放置在此处。 Because of memory constraints, I can't parse the whole file at once, but it contains 5000 nodes which are all very reasonably-sized, so I'm trying to use XML Reader to pull in one node at a time. 由于内存的限制,我无法一次解析整个文件,但是它包含5000个节点,这些节点的大小都非常合理,因此我试图使用XML Reader一次引入一个节点。

The process works perfectly locally, but the issue I'm having is that XMLReader keeps failing to read from my cloud storage with the message "unable to open source data" . 该过程在本地完美运行,但是我遇到的问题是XMLReader始终无法从云存储中读取消息“无法打开源数据”

Here's an example of my code: 这是我的代码示例:

$path = "gs://my_bucket/my_file.xml";
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;
$public_url = CloudStorageTools::getPublicUrl($path, true);

$reader = new XMLReader;
$reader->open( $path ); // fails
$reader->open( $public_url ); // fails

Both the "internal" and the public URL fail with the same error: “内部” URL和公共URL均失败,并显示相同的错误:

XMLReader::open(): Unable to open source data in /[gaepath]/myapp.php on line X XMLReader :: open():无法在X行的/[gaepath]/myapp.php中打开源数据

Having read around, there are suggestions about permissions, but the file is not restricted and the following does work: 阅读后,有关于权限的建议,但是文件不受限制,并且可以正常工作:

$xml = file_get_contents($path); // $xml contains the file contents as a string

Two solutions would help me: 两种解决方案对我有帮助:

  1. Some way to have XMLReader open a Google Cloud Storage URL 使XMLReader打开Goog​​le Cloud Storage URL的某种方法
  2. Some way to pass a string to XMLReader, which does not appear to be possible (and writing a temporary local file also appears to be forbidden on GAE) 将字符串传递给XMLReader的某种方法,这似乎是不可能的(并且在GAE上似乎禁止写入临时本地文件)

I had this same problem. 我有同样的问题。 Looks like we need to manually enable a " Disabled Function " by creating a php.ini file in the root of our app. 看来我们需要通过在应用程序的根目录中创建php.ini文件来手动启用“ 禁用的功能 ”。

php.ini: php.ini:

google_app_engine.enable_functions = "libxml_disable_entity_loader"

Then, in the code, we need to enable the entity loader before loading the file: 然后,在代码中,我们需要在加载文件之前启用实体加载器:

libxml_disable_entity_loader(false);
$xml = new XMLReader();
$res = $xml->open($file);

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

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