在Google AppEngine中读取Java资源文件

时间:2015-07-20 00:25:59

标签: java google-app-engine google-bigquery

我有一个Google AppEngine Java项目设置,调用BigQuery来显示查询作业的结果。我使用的示例代码和说明可以找到here。该应用程序从我的开发计算机运行并检索查询,但是当我上传到AppEngine以在appspot.com上进行测试时,出现client_secrets.json文件(由于JavaScript URL授权,dev和appspot.com的不同文件)无法使用以下片段:

  static GoogleClientSecrets getClientCredential() throws IOException {
if (clientSecrets == null) {

    clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            new InputStreamReader(new FileInputStream(
                new File("WEB-INF/client_secrets.json"))));

  Preconditions.checkArgument(!clientSecrets.getDetails().getClientId().startsWith("Enter ")
      && !clientSecrets.getDetails().getClientSecret().startsWith("Enter "),
      "Enter Client ID and Secret from https://code.google.com/apis/console/?api=bigquery "
      + "into bigquery-appengine-sample/src/main/resources/client_secrets.json");
}
return clientSecrets;

}

这是我的appengine-web.xml资源文件定义:

  <resource-files>
    <include path="**" />
  </resource-files>

client_secrets.json文件在我的war文件中正确打包。我得到的错误是:

> /
Uncaught exception from servlet
java.io.FileNotFoundException: /base/data/home/apps/s~tactile-reason-849/1.385872137632330782/WEB-INF/client_secrets.json (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:171)
    at com.google.api.client.sample.bigquery.appengine.dashboard.ServiceUtils.getClientCredential(ServiceUtils.java:71)
    at com.google.api.client.sample.bigquery.appengine.dashboard.ServiceUtils.newFlow(ServiceUtils.java:103)
    at com.google.api.client.sample.bigquery.appengine.dashboard.MainServlet.initializeFlow(MainServlet.java:125)
    at com.google.api.client.extensions.servlet.auth.oauth2.AbstractAuthorizationCodeServlet.service(AbstractAuthorizationCodeServlet.java:124)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:35)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.java:60)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:257)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
    at com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:146)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:482)
    at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:437)
    at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:444)
    at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:230)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:308)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:300)
    at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:441)
    at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:234)
    at java.lang.Thread.run(Thread.java:745)

我可以尝试使用ServletContext.getResourceAsStream(),但是在示例应用程序中连接到BigQuery的类不是Servlet类,因此这需要对代码进行一些重组才能正确实现,Google's instructions似乎表明我的代码应该工作。感谢任何帮助,谢谢!

2 个答案:

答案 0 :(得分:9)

资源文件不在文件系统上,因此普通的new File("/WEB-INF/client_secrets.json")不起作用。相反,您通常需要将其作为资源加载:

InputStream resourceStream = Thread.currentThread().getContextClassLoader()
    .getResourceAsStream("/WEB-INF/client_secrets.json");

出于某种原因,AppEngine不允许您使用类加载器在WEB-INF下加载资源(您可以使用上述技术在jar文件中加载资源)。​​

您可以使用File API访问App中WEB-INF中的路径,但路径必须是相对的。假设文件client_secrets.json位于WEB-INF文件夹中,这应该有效:

clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
    new InputStreamReader(new FileInputStream(
        new File("WEB-INF/client_secrets.json"))));

要使用File API读取WEB-INF下的资源,您还需要确保您的appengine-web.xml文件包含WEB-INF中的资源

<resource-files>
  <include path="/**.json" />
</resource-files>

请参阅Configuring appengine-web.xmlWhy can't I read from this file

答案 1 :(得分:0)

作为AppEngine错误的解决方法,我创建了一个对象,该对象包含从FirebaseCredentialsHelper类构建的Firebase身份验证数据。返回的对象使用从Firebase控制台生成的String值。

然后,我使用Gson library将FirebaseCredentialsHelper类返回的身份验证对象转换为json,然后转换为字节输入流。

FirebaseApp.initializeApp(FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(Gson().toJson(FirebaseCredentialsHelper.get()).byteInputStream()))
.build()) 

注意:我将Firebase身份验证对象排除在版本控制之外,因为它们包含私人信息。