2017-09-19 65 views
0

我在嘗試使用下面的函數來創建一個谷歌的雲鬥:項目參數創建谷歌雲桶(Java API)時,未找到

def createBucket(bucketName: String, credentials: String): Bucket = { 

    val credentialStream = new ByteArrayInputStream(
     credentials.getBytes(StandardCharsets.UTF_8.name())) 
    val storage = StorageOptions.newBuilder 
     .setCredentials(ServiceAccountCredentials.fromStream(credentialStream)) 
     .build() 
     .getService() 
    storage.create(BucketInfo.newBuilder(bucketName).build()) 
    } 

我讀的憑證流是一個標準的谷歌雲憑據JSON幷包含項目ID。不過,我收到了一條錯誤java.lang.NullPointerException: Required parameter project must be specified如下圖所示:

play.api.UnexpectedException: Unexpected exception[StorageException: java.lang.NullPointerException: Required parameter project must be specified.] 
     at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:276) 
     at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:206) 
     at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160) 
     at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188) 
     at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:98) 
     at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100) 
     at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99) 
     at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:346) 
     at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:345) 
     at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) 
Caused by: com.google.cloud.storage.StorageException: java.lang.NullPointerException: Required parameter project must be specified. 
     at com.google.cloud.storage.StorageException.translateAndThrow(StorageException.java:71) 
     at com.google.cloud.storage.StorageImpl.create(StorageImpl.java:114) 
     at models.services.CloudServiceImpl.createBucket(CloudServiceImpl.scala:728) 

任何想法,這可能是爲什麼?我試圖按照這裏列出的API指南:https://cloud.google.com/storage/docs/creating-buckets#storage-create-bucket-java

回答

0

所以,事實證明,從證書JSON創建存儲對象並不會將其分配給JSON引用的項目。必須手動設置:

val storage = StorageOptions.newBuilder 
    .setCredentials(ServiceAccountCredentials.fromStream(credentialStream)) 
    .setProjectId(projectId) 
    .build 
    .getService