0
votes

I currently have a very simple function which creates a google storage bucket leveraging the Java API (in Scala). However, it currently infers gCloud credentials from the environment.

def createBucket(bucketName: String) = {
    val storage = StorageOptions.getDefaultInstance.getService // May need to change. Creds inferred from environment.
    val bucket = storage.create(BucketInfo.of(bucketName))
  }

Is there any way to pass a GoogleCredential object into the constructor? Been stepping through the API @ https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-storage/src/main/java/com/google/cloud/storage but haven't had any luck.

1

1 Answers

1
votes

Found the answer outlined at https://github.com/GoogleCloudPlatform/google-cloud-java#authentication

Storage storage = StorageOptions.newBuilder()
    .setCredentials(new GoogleCredentials(new AccessToken(accessToken, expirationTime)))
    .build()
    .getService();