简体   繁体   中英

Project Parameter Not Found when Creating Google Cloud Bucket (Java API)

I am attempting to create a google cloud bucket using the below function:

  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())
  }

The credential stream I am reading in is a standard google cloud credentials JSON and contains the project ID. However, I am receiving an error java.lang.NullPointerException: Required parameter project must be specified as shown below:

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)

Any idea why this might be? I was attempting to follow the API guide outlined here: https://cloud.google.com/storage/docs/creating-buckets#storage-create-bucket-java

So, it turns out that creating a storage object from a credentials JSON does not assign it to the project referenced by the JSON. It must be manually set:

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

You can also update google-auth-library-oauth2-http and google-cloud-storage libs to latest versions. projectId can be used from Service Account key if it's not set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM