如何在运行PHP 7.2的localhost Google App Engine上上传文件?

时间:2018-12-20 19:09:34

标签: php google-cloud-platform localhost google-cloud-storage php-7.2

我正在尝试实现用户上传图像的功能。我将该图像保存到Google Storage。然后,我将使用GoogleStorageTools获取该图像的公共可服务URL,以便将其显示在我的网站上。

我需要在本地主机上对此进行测试。当我以前使用php5.5时,我可以使用dev_appserver.py,它可以模拟gs://集成,让我像在云中运行一样创建文件并与之交互。

但是,我正在为此应用程序使用php7.2,该应用程序不能使用dev_appserver.py,我只需要通过php的内置网络服务器运行即可。尝试上传文件时,出现Google存储错误。这是我脚本中接收上载文件的代码:

$storage = new StorageClient();
$storage->registerStreamWrapper();

$basefilename = uniqid().'-'.sanitize_string($_FILES['imagefile']['name']);

$gsfilepath = 'gs://#default#/images/'.$basefilename;

if ( ! move_uploaded_file($_FILES[ 'imagefile' ]['tmp_name'], $gsfilepath)) {
    error_log('Failed to move uploaded file. ' . $_FILES[ 'imagefile' ]['tmp_name'] . ' => ' . $gsfilepath);
}

这些是我尝试运行move_uploaded_file()代码时遇到的错误:

  

Google \ Cloud \ Core \ Exception \ NotFoundException:客户端错误:POST https://www.googleapis.com/upload/storage/v1/b//o?uploadType=resumable   导致404 Not Found响应:找不到   /Users/kenny/myproject/trunk/server/gae/vendor/google/cloud-core/src/RequestWrapper.php   在第263行

     

致命错误:未捕获的Google \ Cloud \ Core \ Exception \ NotFoundException:   客户端错误:POST https://www.googleapis.com/upload/storage/v1/b//o?uploadType=resumable   导致404 Not Found响应:找不到   /Users/kenny/myproject/trunk/server/gae/vendor/google/cloud-core/src/RequestWrapper.php   在第263行

     

警告:move_uploaded_file():无法移动   '/ private / var / folders / 1k / 8xt74j593ss9yxbtqrv925j40000gp / T / phpKsSN6q'   到“ gs://#default#/images/5c1be56fd752d-hold-on-to-your-butts.png”中   /Users/kenny/myproject/trunk/server/gae/testupload-upload.php在线   11

1 个答案:

答案 0 :(得分:0)

存储桶名称/** @test */ public function a_admin_can_create_a_department() { // $this->withoutExceptionHandling(); $attributes = [ 'description' => 'Service', 'accessible_by_depart' => true ]; $this->post('/tools/api/storeDepartment', $attributes); $this->assertDatabaseHas('departments', $attributes); } 在Cloud Storage Stream Wrapper中无效。正如您在错误中看到的那样,库要发布到的URI缺少存储桶ID。

要了解原因,请参考#default#中的StreamWrapper::openPath()方法。

存储桶名称在返回值google/cloud-storage中用host键表示。如下所示,您提供的gs URI导致parse_url失败:

parse_url

我假设php > $url = 'gs://#default/foo'; php > var_dump(parse_url($url)); bool(false) 是在某些情况下有效的助手。尝试在gs URI中提供真实的存储桶ID。