Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Google Drive API Integration with AEM

Avatar

Level 3

I am trying to integrate google drive api with AEM. I am not able to find any specific documentation for the same.

At first I tried to create a workflow but that was for data upload. Now I want to integrate google drive api with AEM. 

 

So is there any documentation which can help me with this?

 

Thanks in advance

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

You can create Java classes in your project referring sample code Google has provided

https://developers.google.com/drive/api/quickstart/java

https://developers.google.com/drive/api/guides/create-file

 

Based on your use case you can create workflow,servlet based on above samples. You may need to add Google API jar files as part of your project by embedding the jars in your bundle. Document for same 
https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/embed-third-party-dependen...

 

View solution in original post

3 Replies

Avatar

Level 1

What exactly your requirement is with Google drive ? you can refer this https://o7planning.org/11889/manipulating-files-and-folders-on-google-drive-using-java

which explains how to communicate to the drive with api from java application. You can do same th a servlet or with your requirement from AEM. 

Avatar

Correct answer by
Community Advisor

You can create Java classes in your project referring sample code Google has provided

https://developers.google.com/drive/api/quickstart/java

https://developers.google.com/drive/api/guides/create-file

 

Based on your use case you can create workflow,servlet based on above samples. You may need to add Google API jar files as part of your project by embedding the jars in your bundle. Document for same 
https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/embed-third-party-dependen...

 

Avatar

Level 7

The Google Drive API allows you to upload file data when you create or update a File. 

There are three types of uploads you can perform:

  • Simple upload (uploadType=media). Use this upload type to quickly transfer a small media file (5 MB or less) without supplying metadata. 

  • Multipart upload (uploadType=multipart). Use this upload type to quickly transfer a small file (5 MB or less) and metadata that describes the file, in a single request. 

  • Resumable upload (uploadType=resumable). Use this upload type for large files (greater than 5 MB) and when there's a high chance of network interruption, such as when creating a file from a mobile app. Resumable uploads are also a good choice for most applications because they also work for small files at a minimal cost of one additional HTTP request per upload.

The Google API client libraries implement at least one of the types of uploads. Refer to the client library documentation for additional details on how to use each of the types. 

https://developers.google.com/drive/api/guides/manage-uploads

 

import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;

import java.io.IOException;
import java.util.Arrays;

/* Class to demonstrate use of Drive insert file API */
public class UploadBasic {

   
/**
     * Upload new file.
     * @return Inserted file metadata if successful, {@code null} otherwise.
     * @throws IOException if service account credentials file not found.
     */

   
public static String uploadBasic() throws IOException{
       
// Load pre-authorized user credentials from the environment.
       
// TODO(developer) - See https://developers.google.com/identity for
       
// guides on implementing OAuth2 for your application.
       
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault().createScoped(Arrays.asList(DriveScopes.DRIVE_FILE));
       
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
                credentials
);

       
// Build a new authorized API client service.
       
Drive service = new Drive.Builder(new NetHttpTransport(),
               
GsonFactory.getDefaultInstance(),
                requestInitializer
)
               
.setApplicationName("Drive samples")
               
.build();
       
// Upload file photo.jpg on drive.
       
File fileMetadata = new File();
        fileMetadata
.setName("photo.jpg");
       
// File's content.
        java
.io.File filePath = new java.io.File("files/photo.jpg");
       
// Specify media type and file-path for file.
       
FileContent mediaContent = new FileContent("image/jpeg", filePath);
       
try {
           
File file = service.files().create(fileMetadata, mediaContent)
                   
.setFields("id")
                   
.execute();
           
System.out.println("File ID: " + file.getId());
           
return file.getId();
       
}catch (GoogleJsonResponseException e) {
           
// TODO(developer) - handle error appropriately
           
System.err.println("Unable to upload file: " + e.getDetails());
           
throw e;
       
}
   
}
}

 

It has examples as well which you can use. 

 

This is a straight forward rest API integration which you can easily implement it