0

我写一个Java应用程序与在谷歌云存储文件交互。我发现gcloud-java,我正在努力工作。如何验证自己与谷歌云存储的Java应用程序?

看着他们的examples看来我应该能够在用gcloud登录后简单地运行它们,但它似乎并没有工作。我试图运行StorageExample,它说,但尽管登录我无法访问我的项目,我是否指定与否“如果没有提供登录项目将使用”。

$ gcloud auth login 
Your browser has been opened to visit: 

    https://accounts.google.com/o/oauth2/auth?.... 


Saved Application Default Credentials. 

You are now logged in as [...]. 
Your current project is [...]. You can change this setting by running: 
    $ gcloud config set project PROJECT_ID 

$ java -cp GCloudDemo.jar com.google.gcloud.examples.storage.StorageExample list 
Exception in thread "main" java.lang.IllegalArgumentException: A project ID is required for this service but could not be determined from the builder or the environment. Please set a project ID using the builder. 
     at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122) 
     at com.google.gcloud.ServiceOptions.<init>(ServiceOptions.java:317) 
     ... 
     at com.google.gcloud.examples.storage.StorageExample.main(StorageExample.java:566) 

$ java -cp GCloudDemo.jar com.google.gcloud.examples.storage.StorageExample ... list 
Exception in thread "main" com.google.gcloud.storage.StorageException: Login Required 
     at com.google.gcloud.spi.DefaultStorageRpc.translate(DefaultStorageRpc.java:94) 
     at com.google.gcloud.spi.DefaultStorageRpc.list(DefaultStorageRpc.java:148) 
     ... 
     at com.google.gcloud.examples.storage.StorageExample$ListAction.run(StorageExample.java:1) 
     at com.google.gcloud.examples.storage.StorageExample.main(StorageExample.java:579) 
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized 
{ 
    "code" : 401, 
    "errors" : [ { 
    "domain" : "global", 
    "location" : "Authorization", 
    "locationType" : "header", 
    "message" : "Login Required", 
    "reason" : "required" 
    } ], 
    "message" : "Login Required" 
} 
     at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145) 
     ... 
     at com.google.gcloud.spi.DefaultStorageRpc.list(DefaultStorageRpc.java:138) 
     ... 10 more 

显然东西是错误与我的环境,但我有什么做的,解决这个问题?我想改正我的环境,而不是对示例进行更改。

注:

  • 我在Windows 10上运行Cygwin中2.0.4
  • 我不是通过Maven的建设,但我假设这不是问题
+0

作为一个更新,我就可以更容易地把事情用[WSL]工作(https://msdn.microsoft.com/en-us/commandline/wsl)比Cygwin的:) – dimo414

回答

1

原因gcloud auth login不起作用是因为Java在Cygwin中运行不好,特别是在绝对路径方面。

gcloud存储您的设置,并在你的Cygwin的主目录,~/.config/gcloud证书,但Java会通过System.getProperty("user.home")寻找他们在你的Windows 目录。您可以指定应用程序应该通过CLOUDSDK_CONFIG环境变量查找此目录,像这样(请务必打电话java时,只能指定它,将它设置为你的shell会依次突破gcloud):

$ CLOUDSDK_CONFIG=$(cygpath -w ~/.config/gcloud) \ 
    java -cp GCloudDemo.jar com.google.gcloud.examples.storage.StorageExample list 
Exception in thread "main" com.google.gcloud.storage.StorageException: Login Required 
    at .... 

公告那我们不需要指定一个项目,但是由于某种原因我们还没有登录。事实证明,该演示应用程序并没有很好地报告认证错误。在main()方法的开始添加到AuthCredentials.createApplicationDefaults()的显式调用报告了一个更为有用的错误消息:

Exception in thread "main" java.io.IOException: The Application Default Credentials are 
not available. They are available if running in Google Compute Engine. Otherwise, the 
environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a 
file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials 
for more information. 

所以现在我们至少有GOOGLE_APPLICATION_CREDENTIALS环境变量去。但为什么不是gcloud为我们配置的东西?它看起来像这实际上在图书馆bug;它应该现在能够找到我们的登录凭证。

在此期间,我们可以通过显式指定的凭据路径以及变通办法:

$ CLOUDSDK_CONFIG=$(cygpath -w ~/.config/gcloud) \ 
    GOOGLE_APPLICATION_CREDENTIALS=$(cygpath -w ~/.config/gcloud)/application_default_credentials.json \ 
    java -cp GCloudDemo.jar com.google.gcloud.examples.storage.StorageExample list 
Bucket{name=...} 

成功!