【问题标题】:Hosting project with dev_appserver.py gives different results to deploying to Google Cloud使用 dev_appserver.py 托管项目会为部署到 Google Cloud 提供不同的结果
【发布时间】:2020-03-30 15:16:59
【问题描述】:

该项目旨在获取一些用户输入并检查连接的 Google 数据存储是否匹配。当使用 dev_appserver.py 托管时,一切都按预期工作。

现场版不是这种情况,使用gcloud app deploy上传。尝试使用 lookup 的键检索实体时会出现问题。

<?php
require __DIR__ . "/vendor/autoload.php";

use Google\Cloud\Datastore\DatastoreClient;

$datastore = new DatastoreClient([
"projectId" => "54327567209-project"
]);

// Retrieving user entities
function lookup_user(DatastoreClient $datastore, string $id) {
  $key = $datastore->key("user", $id);
  $user = $datastore->lookup($key);
  return $user;
}

// Validation
$authErr = "";
$id = $pword = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // Existence check
  if (!empty($_POST["id"]) && !empty($_POST["password"])) {
    $id = $_POST["id"];
    $pword = $_POST["password"];
    // Attempt to get user from Google Cloud
    $user = lookup_user($datastore, $id); // <---- Error happens here
    // Check if user exists and password matches
    if (!empty($user["name"]) && $user["password"] == $pword) {
      // Removed for debugging
    } else {
      // Id or password authentication fail
      $authErr = "User id or password is invalid";
    }
  } else {
    // Missing id or password
    $authErr = "User id or password is invalid";
  }
}
?>

<!DOCTYPE html>
<html>
  <head>
    <style>
      .error {color: #FF0000;}
    </style>
  </head>
  <body>
    <h2>Account Access</h2>
    <form method="post" action="">
      <div>Username: <input name="id" type="text"></input></div>
      <div>Password: <input name="password" type="password"></input></div>
      <div><input type="submit" value="Login"></div>
      </br>
      <span class="error"><?php echo $authErr; ?></span>
    </form>
  </body>
</html>

页面在尝试查找时变为空白,类似于运行exit()。我看不到任何错误消息。我想知道是否可以打开某种日志记录来帮助解决这个问题。

【问题讨论】:

  • 您在Logs Viewer 中看到了什么吗?您应该按 GAE 应用程序和服务进行过滤
  • 我使用您的代码示例重现了您的环境,它对我有用。我使用了 "google/cloud-datastore": "^1.11" 并在 app.yaml 中将运行时设置为 runtime: php72 。您使用的是 GAE Standard 还是 Flex?

标签: php google-app-engine google-cloud-platform google-cloud-datastore dev-appserver


【解决方案1】:

问题出在lookup_user() 函数上。删除此函数并内联粘贴相同的代码可解决此问题。

【讨论】:

  • 不知道为什么这解决了这个问题。我会遵循 cmets 中给出的建议,如果我找到更多信息,我会编辑答案。
猜你喜欢
  • 2015-11-27
  • 1970-01-01
  • 2019-05-19
  • 2010-12-05
  • 2016-05-22
  • 1970-01-01
  • 2020-12-03
  • 2023-01-24
  • 2021-04-27
相关资源
最近更新 更多