Expand my Community achievements bar.

SOLVED

Data Upload in Google Drive

Avatar

Level 3

Hi all, 

 

I am searching for ways to upload data from AEM author instance to google drive. Is there any documentation which would be help me in this.

 

 

Thanks in advance.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@janhavi_singh 

Approach I:

Create the scripts/scheduler/workflow for the asset to be pushed from AEM to google drive.

There are google drive APIs available for pushing the data to google drive. 

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;
        }
    }
}

You can use this to upload to google drive and for getting the metadata or the file object from the AEM server and get the input stream from it and convert it to a file object, that you can refer in the above java code

Asset asset = assetResource.adaptTo(Asset.class); 
Rendition original = asset.getOriginal(); 
if (original != null) { 
	InputStream stream = original.getStream(); 
       // add your logic
} 

 

Approach II: Based on the events, create a connection with AEM + Adobe IO and get all the event captured at Adobe IO journal. Then read the Adobe IO journal in your Firefly app that you can create using the Node.JS and from there create the scheduler that will run and get the data from AEM and do the processing over here.

 

Advantage of Approach II : The AEM server will not be overloaded with all the processing operations required for moving the images/asset from AEM to Google Drive.

Disadvantages : Need to maintain a separate codebase for the firefly app and need to be aware of Node.js.

Hope this helps!

Thanks

View solution in original post

2 Replies

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

 

It has examples as well which you can use. 

 

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

Avatar

Correct answer by
Community Advisor

@janhavi_singh 

Approach I:

Create the scripts/scheduler/workflow for the asset to be pushed from AEM to google drive.

There are google drive APIs available for pushing the data to google drive. 

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;
        }
    }
}

You can use this to upload to google drive and for getting the metadata or the file object from the AEM server and get the input stream from it and convert it to a file object, that you can refer in the above java code

Asset asset = assetResource.adaptTo(Asset.class); 
Rendition original = asset.getOriginal(); 
if (original != null) { 
	InputStream stream = original.getStream(); 
       // add your logic
} 

 

Approach II: Based on the events, create a connection with AEM + Adobe IO and get all the event captured at Adobe IO journal. Then read the Adobe IO journal in your Firefly app that you can create using the Node.JS and from there create the scheduler that will run and get the data from AEM and do the processing over here.

 

Advantage of Approach II : The AEM server will not be overloaded with all the processing operations required for moving the images/asset from AEM to Google Drive.

Disadvantages : Need to maintain a separate codebase for the firefly app and need to be aware of Node.js.

Hope this helps!

Thanks