2

I am Trying to upload JSON data to gcs. As I did not use google cloud previously I started with uploading random String to gcs but I got stuck at the beginning itself while creating a Storage service object

Maven dependency

 <dependency>
   <groupId>com.google.cloud</groupId>
   <artifactId>google-cloud-storage</artifactId>
   <version>1.70.0</version>
 </dependency>


import com.google.cloud.storage.*;
Storage storage = StorageOptions.getDefaultInstance().getService();
    BlobId blobId = BlobId.of("bucket_name", "test_upload/test.txt");
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
    Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
    System.out.println(blob);

Compile Time Error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project gcshelper: Compilation failure: Compilation failure: 
ERROR] /Users/v3/gcshelper/src/main/java/com/tv/gcs/GcsTest.java:[16,41] cannot access com.google.cloud.ServiceOptions [ERROR] class file for com.google.cloud.ServiceOptions not found [ERROR] /Users/v3/gcshelper/src/main/java/com/tv/gcs/GcsTest.java:[19,28] cannot access com.google.cloud.Service [ERROR] class file for com.google.cloud.Service not found [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging.
kiran kumar
  • 81
  • 2
  • 9
  • If you could post a test sample where it can be downloaded (a zip, tar or github), I for one would be happy to try on my system. – Kolban Apr 24 '19 at 16:00

4 Answers4

4
<dependency>
 <groupId>com.google.cloud</groupId>
 <artifactId>google-cloud</artifactId>
 <version>0.47.0-alpha</version>
</dependency>

solved my issue

kiran kumar
  • 81
  • 2
  • 9
1

If someone is using module-info.java then you need to have these two dependencies:

    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-core</artifactId>
        <version>2.6.1</version>
    </dependency>

    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-firestore</artifactId>
        <version>3.0.21</version>
    </dependency>

and in your module-info.java do something like:

open module ModuleName {
    ...
    requires google.cloud.core;
    requires google.cloud.firestore;
    requires com.google.auth;
    requires com.google.auth.oauth2;
    ...
}

if you are using intellij, then it will ask you to do google.cloud.firestore and com.google.auth.oauth2 but this won't work as these depend on google.cloud.core and com.google.auth.

Al-Anazi
  • 364
  • 1
  • 8
0

Did you try to include this dependency in your pom.xml ?

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-core</artifactId>
    <version>1.70.0</version>
</dependency> 
hkanjih
  • 1,271
  • 1
  • 11
  • 29
  • I am getting below error after adding the above dependency: Exception in thread "main" java.lang.NoClassDefFoundError: com/google/auth/Credentials at com.tv.gcs.GcsTest.main(GcsTest.java:16) Caused by: java.lang.ClassNotFoundException: com.google.auth.Credentials at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more – kiran kumar Apr 24 '19 at 18:26
  • Looks like you may need to add ... https://github.com/googleapis/google-auth-library-java – Kolban Apr 24 '19 at 18:37
  • adding the dependency mentioned by @Kolban is not working, same issue exists – kiran kumar Apr 24 '19 at 18:45
  • If you wish, upload a ZIP, tar or point to a Github of a sample trivial failing project with instructions on how you build and I for one will be willing to have a test. – Kolban Apr 24 '19 at 19:04
  • if I go on adding the dependency the dependencies are increasing. – kiran kumar Apr 24 '19 at 19:04
  • @Kolban there is nothing extra code, it's just as is I uploaded the code content. If you want to recreate the same on your machine create a class file and paste above code should be fine – kiran kumar Apr 24 '19 at 19:06
  • 1
    Howdy ... I understand. When ever I am looking for help on StackOverflow, I try and make it ridiculously easy for others to assist me. If I have a problem with simple code or execution, I try and build the most minimal sample that illustrates the problem and then give that to the readers to run. I don't want to make folks who may be able to assist do any more work than necessary ... such as creating wrapper classes or POM files. I also include full instructions to recreate. It is also common that my own error shines through in the full sample code. – Kolban Apr 24 '19 at 19:14
  • https://drive.google.com/file/d/1EMpS6D3iYWJjqnneO6GqceNor2m41ZpF/view?usp=sharing – kiran kumar Apr 24 '19 at 19:55
  • I downloaded and ran your ZIP using "mvn compile" and I got 100% success for a clean build. Now we get to execution. Exactly, how are you running your application? My guess is that when you run it, you have not supplied the classes to the Class Path that are part of the Google Cloud SDK. – Kolban Apr 24 '19 at 22:05
0

my understanding is that you can’t upload files to the gcp container using java, and the stack trace is showing that maven failing at compilation time.

Well, You can try 2 things:

1.- Make sure that you Authentication settings are good, You can follow the steps in the link[1]. 2.- Configure you Maven installation according the gcp instructions[2].

[1]https://cloud.google.com/docs/authentication/production#auth-cloud-implicit-java [2]https://cloud.google.com/appengine/docs/standard/java/tools/maven#setting_up_maven