Skip to content

Commit

Permalink
#26 - add GCP ADC GCS stub example
Browse files Browse the repository at this point in the history
  • Loading branch information
obriensystems committed Oct 16, 2023
1 parent a2d45f7 commit 554164f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
18 changes: 18 additions & 0 deletions magellan-nbi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,25 @@
<org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
</properties>


<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.24.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- https://cloud.google.com/docs/authentication/client-libraries#java -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
</dependency>
<!-- mapstruct -->
<dependency>
<groupId>org.mapstruct</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.google.api.gax.paging.Page;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.io.IOException;

@Service
public class ApplicationService implements ApplicationServiceLocal {

Expand All @@ -13,9 +19,34 @@ public String health() {

@Override
public String gcp() {
try {
authenticateImplicitWithAdc("gen-ai-old");
} catch (IOException io) {
System.out.println(io.getMessage());
}
return "gcp";
}

// https://cloud.google.com/docs/authentication/client-libraries#java
private void authenticateImplicitWithAdc(String project) throws IOException {

// *NOTE*: Replace the client created below with the client required for your application.
// Note that the credentials are not specified when constructing the client.
// Hence, the client library will look for credentials using ADC.
//
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
Storage storage = StorageOptions.newBuilder().setProjectId(project).build().getService();

System.out.println("Buckets:");
Page<Bucket> buckets = storage.list();
for (Bucket bucket : buckets.iterateAll()) {
System.out.println(bucket.toString());
}
System.out.println("Listed all storage buckets.");
}


@Override
public String forward() {
// TODO Auto-generated method stub
Expand Down

0 comments on commit 554164f

Please sign in to comment.