DatastoreOptions.Builder options = new DatastoreOptions.Builder(); Credential credential = GoogleCredential.getApplicationDefault(); if (serviceAccountEmail != null && privateKeyFile != null) { options.credential(credential).projectId(datasetId).build());
public HttpDatastoreRpc(DatastoreOptions options) { HttpTransportOptions httpTransportOptions = (HttpTransportOptions) options.getTransportOptions(); HttpTransport transport = httpTransportOptions.getHttpTransportFactory().create(); com.google.datastore.v1.client.DatastoreOptions.Builder clientBuilder = new com.google.datastore.v1.client.DatastoreOptions.Builder() .projectId(options.getProjectId()) .initializer(getHttpRequestInitializer(options, httpTransportOptions)) .transport(transport); String normalizedHost = options.getHost() != null ? options.getHost().toLowerCase() : ""; if (isLocalHost(normalizedHost)) { clientBuilder = clientBuilder.localHost(removeScheme(normalizedHost)); } else if (!removeScheme(com.google.datastore.v1.client.DatastoreFactory.DEFAULT_HOST) .equals(removeScheme(normalizedHost)) && !normalizedHost.isEmpty()) { String fullUrl = normalizedHost; if (fullUrl.charAt(fullUrl.length() - 1) != '/') { fullUrl = fullUrl + '/'; } fullUrl = fullUrl + com.google.datastore.v1.client.DatastoreFactory.VERSION + "/projects/" + options.getProjectId(); clientBuilder = clientBuilder.projectId(null).projectEndpoint(fullUrl); } client = com.google.datastore.v1.client.DatastoreFactory.get().create(clientBuilder.build()); }
public HttpDatastoreRpc(DatastoreOptions options) { HttpTransportOptions httpTransportOptions = (HttpTransportOptions) options.getTransportOptions(); HttpTransport transport = httpTransportOptions.getHttpTransportFactory().create(); com.google.datastore.v1.client.DatastoreOptions.Builder clientBuilder = new com.google.datastore.v1.client.DatastoreOptions.Builder() .projectId(options.getProjectId()) .initializer(getHttpRequestInitializer(options, httpTransportOptions)) .transport(transport); String normalizedHost = options.getHost() != null ? options.getHost().toLowerCase() : ""; if (isLocalHost(normalizedHost)) { clientBuilder = clientBuilder.localHost(removeScheme(normalizedHost)); } else if (!removeScheme(com.google.datastore.v1.client.DatastoreFactory.DEFAULT_HOST) .equals(removeScheme(normalizedHost)) && !normalizedHost.isEmpty()) { String fullUrl = normalizedHost; if (fullUrl.charAt(fullUrl.length() - 1) != '/') { fullUrl = fullUrl + '/'; } fullUrl = fullUrl + com.google.datastore.v1.client.DatastoreFactory.VERSION + "/projects/" + options.getProjectId(); clientBuilder = clientBuilder.projectId(null).projectEndpoint(fullUrl); } client = com.google.datastore.v1.client.DatastoreFactory.get().create(clientBuilder.build()); }
/** * Constructs a {@link Datastore} from environment variables and/or the Compute Engine metadata * server. * * <p>The project ID is determined from, in order of preference: * <ul> * <li>DATASTORE_PROJECT_ID environment variable * <li>Compute Engine * </ul> * * <p>Credentials are taken from, in order of preference: * <ol> * <li>No credentials (if the DATASTORE_EMULATOR_HOST environment variable is set) * <li>Service Account specified by the DATASTORE_SERVICE_ACCOUNT and DATASTORE_PRIVATE_KEY_FILE * environment variables * <li>Google Application Default as described at * {@link "https://developers.google.com/identity/protocols/application-default-credentials"} * </ol> */ public static DatastoreOptions.Builder getOptionsFromEnv() throws GeneralSecurityException, IOException { DatastoreOptions.Builder options = new DatastoreOptions.Builder(); setProjectEndpointFromEnv(options); options.credential(getCredentialFromEnv()); return options; }
/** * Constructs a {@link Datastore} from environment variables and/or the Compute Engine metadata * server. * * <p>The project ID is determined from, in order of preference: * <ul> * <li>DATASTORE_PROJECT_ID environment variable * <li>Compute Engine * </ul> * * <p>Credentials are taken from, in order of preference: * <ol> * <li>No credentials (if the DATASTORE_EMULATOR_HOST environment variable is set) * <li>Service Account specified by the DATASTORE_SERVICE_ACCOUNT and DATASTORE_PRIVATE_KEY_FILE * environment variables * <li>Google Application Default as described at * {@link "https://developers.google.com/identity/protocols/application-default-credentials"} * </ol> */ public static DatastoreOptions.Builder getOptionsFromEnv() throws GeneralSecurityException, IOException { DatastoreOptions.Builder options = new DatastoreOptions.Builder(); setProjectEndpointFromEnv(options); options.credential(getCredentialFromEnv()); return options; }
private static void setProjectEndpointFromEnv(DatastoreOptions.Builder options) { // DATASTORE_HOST is deprecated. if (System.getenv("DATASTORE_HOST") != null) { logger.warning(String.format( "Ignoring value of environment variable DATASTORE_HOST. " + "To point datastore to a host running locally, use " + "the environment variable %s.", LOCAL_HOST_ENV_VAR)); } String projectId = getProjectIdFromEnv(); if (System.getenv(URL_OVERRIDE_ENV_VAR) != null) { options.projectEndpoint(String.format("%s/projects/%s", System.getenv(URL_OVERRIDE_ENV_VAR), projectId)); return; } if (System.getenv(LOCAL_HOST_ENV_VAR) != null) { options.projectId(projectId); options.localHost(System.getenv(LOCAL_HOST_ENV_VAR)); return; } options.projectId(projectId); return; }
private static void setProjectEndpointFromEnv(DatastoreOptions.Builder options) { // DATASTORE_HOST is deprecated. if (System.getenv("DATASTORE_HOST") != null) { logger.warning(String.format( "Ignoring value of environment variable DATASTORE_HOST. " + "To point datastore to a host running locally, use " + "the environment variable %s.", LOCAL_HOST_ENV_VAR)); } String projectId = getProjectIdFromEnv(); if (System.getenv(URL_OVERRIDE_ENV_VAR) != null) { options.projectEndpoint(String.format("%s/projects/%s", System.getenv(URL_OVERRIDE_ENV_VAR), projectId)); return; } if (System.getenv(LOCAL_HOST_ENV_VAR) != null) { options.projectId(projectId); options.localHost(System.getenv(LOCAL_HOST_ENV_VAR)); return; } options.projectId(projectId); return; }
/** * Builds a Cloud Datastore client for the given pipeline options, project and an optional * locahost. */ public Datastore getDatastore( PipelineOptions pipelineOptions, String projectId, @Nullable String localhost) { Credentials credential = pipelineOptions.as(GcpOptions.class).getGcpCredential(); HttpRequestInitializer initializer; if (credential != null) { initializer = new ChainingHttpRequestInitializer( new HttpCredentialsAdapter(credential), new RetryHttpRequestInitializer()); } else { initializer = new RetryHttpRequestInitializer(); } DatastoreOptions.Builder builder = new DatastoreOptions.Builder().projectId(projectId).initializer(initializer); if (localhost != null) { builder.localHost(localhost); } else { builder.host("batch-datastore.googleapis.com"); } return DatastoreFactory.get().create(builder.build()); }
/** Build a new datastore client. */ static Datastore getDatastore(PipelineOptions pipelineOptions, String projectId) { Credentials credential = pipelineOptions.as(GcpOptions.class).getGcpCredential(); HttpRequestInitializer initializer; if (credential != null) { initializer = new ChainingHttpRequestInitializer( new HttpCredentialsAdapter(credential), new RetryHttpRequestInitializer()); } else { initializer = new RetryHttpRequestInitializer(); } DatastoreOptions.Builder builder = new DatastoreOptions.Builder().projectId(projectId).initializer(initializer); return DatastoreFactory.get().create(builder.build()); }
/** * @see #getOptionsFromEnv() */ public static Datastore getDatastoreFromEnv() throws GeneralSecurityException, IOException { return DatastoreFactory.get().create(getOptionsFromEnv().build()); }
/** * @see #getOptionsFromEnv() */ public static Datastore getDatastoreFromEnv() throws GeneralSecurityException, IOException { return DatastoreFactory.get().create(getOptionsFromEnv().build()); }