我正在尝试使用以下功能创建Google云端桶:
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())
}
我正在阅读的凭据流是标准的Google云凭据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 :(得分:2)
因此,事实证明,从凭证JSON创建存储对象不会将其分配给JSON引用的项目。必须手动设置:
val storage = StorageOptions.newBuilder
.setCredentials(ServiceAccountCredentials.fromStream(credentialStream))
.setProjectId(projectId)
.build
.getService
答案 1 :(得分:0)
您还可以将 google-auth-library-oauth2-http 和 google-cloud-storage 库更新为最新版本。 如果未设置 projectId ,则可以通过“服务帐户”密钥使用。