Ravi
Ravi

Reputation: 342

How to set proxy for storage service in GCP using Java?

I want to download the object from GCP Bucket using JAVA. Here is the code sample which I have written:

    NetHttpTransport.Builder builder = new NetHttpTransport.Builder();
    builder.trustCertificates(GoogleUtils.getCertificateTrustStore());
    builder.setProxy(new Proxy(Proxy.Type.SOCKS, new 
    InetSocketAddress("myproxy", 1234)));
    final HttpTransport httpTransport = builder.build();

    HttpTransportFactory hf = new HttpTransportFactory(){
        @Override
        public HttpTransport create() {
            return httpTransport;
        }
    };

    ServiceAccountCredentials.fromStream(new FileInputStream("path"));
    Storage storage = StorageOptions.newBuilder()
            .setProjectId("projectId")
            .setCredentials(ServiceAccountCredentials.getApplicationDefault(hf))
            .build()
            .getService();

When I run this code, I am getting following exception:

javax.net.ssl.SSLHandshakeException:
sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to
find valid certification path to requested target

Is there any way to set the proxy for Storage Service?

Upvotes: 2

Views: 3062

Answers (2)

Tukesh
Tukesh

Reputation: 95

Setting system proxy may cause issue with other apps running on same JVM. Alternatively, this can be done on the command line when starting your servlet environment:

java -Dhttp.proxyHost=my.proxy.domain.com -Dhttp.proxyPort=3128 -jar sampleApp.jar

Note - Do not forget to use single qoutes around jvm argument when on windows.

I have not tried below but I was exporing this option on how to use an external http client which gives us option to set proxy:

HttpHost proxy = new HttpHost("127.0.0.1", 3128);

        org.apache.http.client.CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                new UsernamePasswordCredentials("user1", "user1"));
        HttpClientBuilder clientBuilder = HttpClientBuilder.create();

        clientBuilder.useSystemProperties();
        clientBuilder.setProxy(proxy);
        clientBuilder.setDefaultCredentialsProvider(credsProvider);
        clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());

        CloseableHttpClient httpClient = clientBuilder.build();

        mHttpTransport = new ApacheHttpTransport(httpClient);

        HttpTransportFactory hf = new HttpTransportFactory() {
            @Override
            public HttpTransport create() {
                return mHttpTransport;
            }
        };

        credential = GoogleCredentials.getApplicationDefault(hf);

        TransportOptions options = HttpTransportOptions.newBuilder().setHttpTransportFactory(hf).build();
        Storage storage_service = StorageOptions.newBuilder().setCredentials(credential)
                .setTransportOptions(options)
                .build()
                .getService();

Upvotes: 1

Federico Panunzio
Federico Panunzio

Reputation: 964

According to what’s stated in this question, Google Cloud Client libraries do not offer a built in proxy support. You will have to configure a system proxy or environment specific proxy. For example, if you're running a JVM you can configure the proxy settings as described here.

Upvotes: 1

Related Questions