我正在尝试实施一种方法来检查Google云中是否存在特定存储桶。例如,该函数采用存储桶名称gs://my-bucket/
和凭据JSON的字符串化版本。我将凭证字符串转换为输入流,以便将其传递给setCredentials
方法(不确定是否有办法通过直接传递字符串来执行此操作 - 在API中未找到一个)。但是,执行此操作后,我收到与无效前提条件相关的错误。我基本上直接从谷歌云Java API存储示例中删除了这个方法(虽然在Scala中),所以我不确定是什么原因造成的。
代码:
def bucketExists(bucketName: String, credentials: String): Boolean = {
val credentialStream = new ByteArrayInputStream(credentials.getBytes(StandardCharsets.UTF_8))
val storage = StorageOptions.newBuilder
.setCredentials(
ServiceAccountCredentials.fromStream(credentialStream))
.build
.getService
storage.get(bucketName, Storage.BucketGetOption.fields()).exists()
}
错误讯息:
java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;)V
at com.google.cloud.storage.StorageImpl.optionMap(StorageImpl.java:945)
at com.google.cloud.storage.StorageImpl.optionMap(StorageImpl.java:937)
at com.google.cloud.storage.StorageImpl.optionMap(StorageImpl.java:967)
at com.google.cloud.storage.StorageImpl.list(StorageImpl.java:250)
at models.services.CloudServiceImpl.bucketExists(CloudServiceImpl.scala:726)
at models.services.CloudServiceImpl.genomicsETL(CloudServiceImpl.scala:591)
at controllers.AuthController$$anonfun$ingestDataSource$1$$anonfun$apply$44.apply(AuthController.scala:339)
at controllers.AuthController$$anonfun$ingestDataSource$1$$anonfun$apply$44.apply(AuthController.scala:327)
at scala.util.Success$$anonfun$map$1.apply(Try.scala:237)
at scala.util.Try$.apply(Try.scala:192)
编辑:
将storage.get(bucketName, Storage.BucketGetOption.fields()).exists()
交换为storage.get(bucketName).exists()
并获得我认为更好的例外:
java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor;
at com.google.api.gax.retrying.BasicRetryingFuture.<init>(BasicRetryingFuture.java:77)
at com.google.api.gax.retrying.DirectRetryingExecutor.createFuture(DirectRetryingExecutor.java:75)
at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:53)
at com.google.cloud.storage.StorageImpl.get(StorageImpl.java:165)
at models.services.CloudServiceImpl.bucketExists(CloudServiceImpl.scala:725)
我相信这表明有多个谷歌番石榴版本导致冲突,并且可以通过着色来解决。可能需要验证番石榴版本的来源(最好的猜测是播放和谷歌云控制台)。