[GH-ISSUE #116] Using Jclouds to access GCS docker need Credentials #24

Closed
opened 2026-03-03 12:07:29 +03:00 by kerem · 3 comments
Owner

Originally created by @rexydwybrundy on GitHub (Nov 1, 2019).
Original GitHub issue: https://github.com/fsouza/fake-gcs-server/issues/116

I using jclouds to try accessing blobstorage in GCS. It seems I need to set up some credentials and I didn't found it here. Do you have some examples? Thanks

Originally created by @rexydwybrundy on GitHub (Nov 1, 2019). Original GitHub issue: https://github.com/fsouza/fake-gcs-server/issues/116 I using jclouds to try accessing blobstorage in GCS. It seems I need to set up some credentials and I didn't found it here. Do you have some examples? Thanks
kerem 2026-03-03 12:07:29 +03:00
  • closed this issue
  • added the
    stale
    label
Author
Owner

@fsouza commented on GitHub (Nov 7, 2019):

Hey @rexydwybrundy, thanks for opening this issue. I'm not sure what jclouds is 🙈 Can you share a snippet of code that is supposed to work with a regular GCS server? Perhaps I can look into adapting it somehow.

<!-- gh-comment-id:550596534 --> @fsouza commented on GitHub (Nov 7, 2019): Hey @rexydwybrundy, thanks for opening this issue. I'm not sure what jclouds is 🙈 Can you share a snippet of code that is supposed to work with a regular GCS server? Perhaps I can look into adapting it somehow.
Author
Owner

@rexydwybrundy commented on GitHub (Nov 7, 2019):

@fsouza so jcloud is like an open source multi-cloud toolkit for the Java platform that gives you the freedom to create applications that are portable across clouds while giving you full-control to use cloud-specific features. So I want to able to upload my file to the storage of any cloud provider with just specify the provider. But when I use this docker, is there any default credential that I can use or this is only the storage?

import com.google.common.base.Charsets;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
import org.jclouds.ContextBuilder;
import org.jclouds.apis.ApiMetadata;
import org.jclouds.apis.Apis;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location;
import org.jclouds.googlecloud.GoogleCredentialsFromJson;
import org.jclouds.googlecloudstorage.GoogleCloudStorageApi;
import org.jclouds.googlecloudstorage.GoogleCloudStorageApiMetadata;
import org.jclouds.openstack.swift.v1.SwiftApiMetadata;
import org.jclouds.providers.ProviderMetadata;
import org.jclouds.providers.Providers;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

import static java.nio.charset.StandardCharsets.UTF_8;

public class JcloudsTestGCS {

    public static final Map<String, ApiMetadata> allApis = Maps.uniqueIndex(Apis.viewableAs(BlobStoreContext.class),
            Apis.idFunction());

    public static final Map<String, ProviderMetadata> appProviders = Maps.uniqueIndex(Providers.viewableAs(BlobStoreContext.class),
            Providers.idFunction());

    public static final Set<String> allKeys = ImmutableSet.copyOf(Iterables.concat(appProviders.keySet(), allApis.keySet()));

    public static void main(String[] args) throws IOException {

        String provider = "google-cloud-storage";
        String identity = "myProjectEmail";
        String credential = "credential.json"; // This credential should be my credential storage account. Jclouds didn't allow me to use without credential
        String containerName = "testBucket";


        // Args
        boolean providerIsGCS = provider.equalsIgnoreCase("google-cloud-storage");
        // For GCE, the credential parameter is the path to the private key file
        if (providerIsGCS) {
            credential = getCredentialFromJsonKeyFile(credential);
        }

        String fileContents = Files.toString(new File("credential.json"), UTF_8);
        GoogleCredentialsFromJson credentials = new GoogleCredentialsFromJson(fileContents);

        BlobStoreContext context = ContextBuilder.newBuilder(provider)
                .credentials(identity, credential)
                .buildView(BlobStoreContext.class)
                ;
        System.out.println("1");
        BlobStore blobStore = null;
        try {
            ApiMetadata apiMetadata = context.unwrap().getProviderMetadata().getApiMetadata();
            blobStore = context.getBlobStore();
            // Create Container
            Location location = null;
            if (apiMetadata instanceof SwiftApiMetadata) {
                System.out.println("SwiftApiMetadata Detected");
                location = Iterables.getFirst(blobStore.listAssignableLocations(), null);
            }
            System.out.println("1");
            blobStore.createContainerInLocation(location, containerName);
            System.out.println("1");
            String blobName = "test";
            ByteSource payload = ByteSource.wrap("testdata".getBytes(Charsets.UTF_8));

            // List Container Metadata
            for (StorageMetadata resourceMd : blobStore.list()) {
                if (containerName.equals(resourceMd.getName())) {
                    System.out.println(resourceMd);
                }
            }
            System.out.println("1");
            // Add Blob
            Blob blob = blobStore.blobBuilder(blobName)
                    .payload(payload)
                    .contentLength(payload.size())
                    .build();
            blobStore.putBlob(containerName, blob);

            // Use Provider API
            Object object = null;
            if (apiMetadata instanceof GoogleCloudStorageApiMetadata) {
                GoogleCloudStorageApi api = context.unwrapApi(GoogleCloudStorageApi.class);
                object = api.getObjectApi().getObject(containerName, blobName);
            }
            if (object != null) {
                System.out.println(object);
            }

        } catch (Exception e) {
            System.out.println("Error " + e);
        } finally {
            context.close();
        }
    }

    private static String getCredentialFromJsonKeyFile(String filename) {
        try {
            String fileContents = Files.toString(new File(filename), UTF_8);
            Supplier<Credentials> credentialSupplier = new GoogleCredentialsFromJson(fileContents);
            String credential = credentialSupplier.get().credential;
            return credential;
        } catch (IOException e) {
            System.err.println("Exception reading private key from '%s': " + filename);
            e.printStackTrace();
            System.exit(1);
            return null;
        }
    }
}
<!-- gh-comment-id:550613948 --> @rexydwybrundy commented on GitHub (Nov 7, 2019): @fsouza so jcloud is like an open source multi-cloud toolkit for the Java platform that gives you the freedom to create applications that are portable across clouds while giving you full-control to use cloud-specific features. So I want to able to upload my file to the storage of any cloud provider with just specify the provider. But when I use this docker, is there any default credential that I can use or this is only the storage? ```java import com.google.common.base.Charsets; import com.google.common.base.Supplier; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.collect.Maps; import com.google.common.io.ByteSource; import com.google.common.io.Files; import org.jclouds.ContextBuilder; import org.jclouds.apis.ApiMetadata; import org.jclouds.apis.Apis; import org.jclouds.blobstore.BlobStore; import org.jclouds.blobstore.BlobStoreContext; import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.StorageMetadata; import org.jclouds.domain.Credentials; import org.jclouds.domain.Location; import org.jclouds.googlecloud.GoogleCredentialsFromJson; import org.jclouds.googlecloudstorage.GoogleCloudStorageApi; import org.jclouds.googlecloudstorage.GoogleCloudStorageApiMetadata; import org.jclouds.openstack.swift.v1.SwiftApiMetadata; import org.jclouds.providers.ProviderMetadata; import org.jclouds.providers.Providers; import java.io.File; import java.io.IOException; import java.util.Map; import java.util.Set; import static java.nio.charset.StandardCharsets.UTF_8; public class JcloudsTestGCS { public static final Map<String, ApiMetadata> allApis = Maps.uniqueIndex(Apis.viewableAs(BlobStoreContext.class), Apis.idFunction()); public static final Map<String, ProviderMetadata> appProviders = Maps.uniqueIndex(Providers.viewableAs(BlobStoreContext.class), Providers.idFunction()); public static final Set<String> allKeys = ImmutableSet.copyOf(Iterables.concat(appProviders.keySet(), allApis.keySet())); public static void main(String[] args) throws IOException { String provider = "google-cloud-storage"; String identity = "myProjectEmail"; String credential = "credential.json"; // This credential should be my credential storage account. Jclouds didn't allow me to use without credential String containerName = "testBucket"; // Args boolean providerIsGCS = provider.equalsIgnoreCase("google-cloud-storage"); // For GCE, the credential parameter is the path to the private key file if (providerIsGCS) { credential = getCredentialFromJsonKeyFile(credential); } String fileContents = Files.toString(new File("credential.json"), UTF_8); GoogleCredentialsFromJson credentials = new GoogleCredentialsFromJson(fileContents); BlobStoreContext context = ContextBuilder.newBuilder(provider) .credentials(identity, credential) .buildView(BlobStoreContext.class) ; System.out.println("1"); BlobStore blobStore = null; try { ApiMetadata apiMetadata = context.unwrap().getProviderMetadata().getApiMetadata(); blobStore = context.getBlobStore(); // Create Container Location location = null; if (apiMetadata instanceof SwiftApiMetadata) { System.out.println("SwiftApiMetadata Detected"); location = Iterables.getFirst(blobStore.listAssignableLocations(), null); } System.out.println("1"); blobStore.createContainerInLocation(location, containerName); System.out.println("1"); String blobName = "test"; ByteSource payload = ByteSource.wrap("testdata".getBytes(Charsets.UTF_8)); // List Container Metadata for (StorageMetadata resourceMd : blobStore.list()) { if (containerName.equals(resourceMd.getName())) { System.out.println(resourceMd); } } System.out.println("1"); // Add Blob Blob blob = blobStore.blobBuilder(blobName) .payload(payload) .contentLength(payload.size()) .build(); blobStore.putBlob(containerName, blob); // Use Provider API Object object = null; if (apiMetadata instanceof GoogleCloudStorageApiMetadata) { GoogleCloudStorageApi api = context.unwrapApi(GoogleCloudStorageApi.class); object = api.getObjectApi().getObject(containerName, blobName); } if (object != null) { System.out.println(object); } } catch (Exception e) { System.out.println("Error " + e); } finally { context.close(); } } private static String getCredentialFromJsonKeyFile(String filename) { try { String fileContents = Files.toString(new File(filename), UTF_8); Supplier<Credentials> credentialSupplier = new GoogleCredentialsFromJson(fileContents); String credential = credentialSupplier.get().credential; return credential; } catch (IOException e) { System.err.println("Exception reading private key from '%s': " + filename); e.printStackTrace(); System.exit(1); return null; } } } ```
Author
Owner

@fsouza commented on GitHub (Feb 15, 2021):

Closing as stale. Feel free to reopen if needed.

<!-- gh-comment-id:779481816 --> @fsouza commented on GitHub (Feb 15, 2021): Closing as stale. Feel free to reopen if needed.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/fake-gcs-server#24
No description provided.