Jenny
Jenny

Reputation: 405

getObject operation returning metadata instead of object content from GCP in camel-spring boot application

I have a spring boot application using camel to connect to google object storage to get an object (text or photo). This is the code I'm running:

package footballRestAPIs;


import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.google.storage.GoogleCloudStorageConstants;
import org.springframework.stereotype.Component;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;

import core.ErrorProcessor;


@Component("ListObjFromGCP")
public class ListObjFromGCP extends RouteBuilder{
    
    
     @Override
        public void configure() throws Exception {
            
         
            onException(Exception.class).handled(true)
            .process(new ErrorProcessor());
            

            rest("/").produces("application.json") 
            .get("selectPhoto")
            
            .to("direct:selectPhoto");

        from("direct:selectPhoto")
        .process(new Processor() {
            @Override
            public void process(Exchange xchg) throws Exception {
                xchg.getIn().setHeader(GoogleCloudStorageConstants.OBJECT_NAME, "kitten.png");
                
            }
        })
        
        .to("google-storage://sagessapp_test?operation=getObject")
.log("${body}");

        }

}

In the aplication.properties file I have the following:

spring.cloud.gcp.credentials.location=classpath:/gcp-credentials.json
camel.component.google-storage.service-account-key=classpath:/gcp-credentials.json

and this is what .log("${body}") shows:

Blob{bucket=sagessapp_test, name=kitten.png, generation=1637310466399682, size=0, content-type=application/octet-stream, metadata=null}

Inside the blob I can see the metadata, I did not find any documentation on how to return the content of the object. Any suggestions? Thank you in advance!

Upvotes: 2

Views: 289

Answers (2)

Kurt
Kurt

Reputation: 1

After looking through the source code of the Camel Google Storage component, I learned you have to use it as a consumer to retrieve the actual file content.

Doing so as part of a route, you use pollEnrich, so

.pollEnrich("google-storage://sagessapp_test?includeBody=true&objectName=kitten.png")

Upvotes: 0

Farid Shumbar
Farid Shumbar

Reputation: 1420

Object metadata is returned by default.

Try using the alt=media parameter to return the object data:

By default, this responds with an object resource in the response body. If you provide the URL parameter alt=media, then it will respond with the object data in the response body.

Upvotes: 0

Related Questions