尝试使用服务帐户凭据将内容发送到Google Cloud时出错
我们为项目目的创建了一个存储桶。
我创建了一个Java服务,尝试将JSON内容发送到存储桶。
我正在使用内置库将内容发送到存储桶。
我已附加了文档中提到的JARS和一个简单的4行代码,以将文件内容发送到存储桶。
根据文档的建议,我几乎遵循了下面的GITHUB链接。
但是我不确定我是否需要最新版本的jar,而文档中只提到了少数几个jar,例如(google-auth-library-oauth2-http,google-cloud-core,google-cloud -storage和google-auth-library-credentials),我不得不添加很多其他的jar来解决上述jar的依赖性错误。
https://cloud.google.com/docs/authentication/production#auth-cloud-explicit-java
Here is my code
import com.google.auth.Credentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
Credentials credentials = GoogleCredentials.fromStream(new FileInputStream(keyPath));
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
BlobId blobId = BlobId.of("tms-data", "test");
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(("UTF-8")));
I have imported the following jars
List of Jars used
commons-logging-1.2.jar
httpcore-4.4.6.jar
google-api-client-jackson2-1.25.0.jar
google-http-client-1.25.0.jar
jackson-core-2.9.6.jar
httpclient-4.5.5.jar
jsr305-3.0.2.jar
google-oauth-client-1.25.0.jar
gson-2.1.jar
threetenbp-1.3.3.jar
gax-grpc-1.34.0.jar
gax-1.34.0.jar
google-auth-library-credentials-0.9.0.jar
google-auth-library-oauth2-http-0.11.0.jar
google-cloud-storage-1.51.0.jar
google-http-client-jackson2-1.26.0.jar
guava-23.0.jar
api-common-1.2.0.jar
google-cloud-core-grpc-1.52.0.jar
google-cloud-core-1.52.0.jar
google-cloud-core-http-1.52.0.jar
google-api-client-1.26.0.jar
xpp3-1.1.4c.jar
protobuf-java-2.4.1.jar
But i am still getting the following error
java.lang.ClassNotFoundException: com.google.api.client.extensions.appengine.http.UrlFetchTransport
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.google.cloud.http.HttpTransportOptions.<init>(HttpTransportOptions.java:131)
at com.google.cloud.http.HttpTransportOptions.<init>(HttpTransportOptions.java:45)
at com.google.cloud.http.HttpTransportOptions$Builder.build(HttpTransportOptions.java:90)
at com.google.cloud.storage.StorageOptions.getDefaultHttpTransportOptions(StorageOptions.java:106)
at com.google.cloud.storage.StorageOptions$StorageDefaults.getDefaultTransportOptions(StorageOptions.java:101)
at com.google.cloud.ServiceOptions.<init>(ServiceOptions.java:288)
at com.google.cloud.storage.StorageOptions.<init>(StorageOptions.java:83)
at com.google.cloud.storage.StorageOptions.<init>(StorageOptions.java:31)
at com.google.cloud.storage.StorageOptions$Builder.build(StorageOptions.java:78)
at WtrTMSTripAdvisor.v1.priv.service.sendJSONToGCP(service.java:431)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.wm.app.b2b.server.JavaService.baseInvoke(JavaService.java:407)
at com.wm.app.b2b.server.invoke.InvokeManager.process(InvokeManager.java:650)
at com.wm.app.b2b.server.util.tspace.ReservationProcessor.process(ReservationProcessor.java:39)
at com.wm.app.b2b.server.invoke.StatisticsProcessor.process(StatisticsProcessor.java:49)
at com.wm.app.b2b.server.invoke.ServiceCompletionImpl.process(ServiceCompletionImpl.java:243)
at com.wm.app.b2b.server.invoke.ValidateProcessor.process(ValidateProcessor.java:49)
at com.wm.app.b2b.server.invoke.PipelineProcessor.process...<truncated>