AEM Cloud Service - Sample Java Standalone Program to Upload Assets

Goal

AEM Cloud Version : 2021.6.5382.20210602T190018Z-210527 (June 2, 2021)

Uploading assets to AEM Cloud Service is a 3 step process (initiateUpload.json, Upload to blob storage, completeUpload.json). Tools like https://github.com/adobe/aem-upload can be used to upload files to AEM using a NodeJS client. 

Drew Robinson explains the steps in detail here

Demo | Github


Command

Add aem-sdk-api-2021.5.5343.20210524T070738Z-210527.jar to your class path and run the program apps.UploadToAEMCS

java -classpath "C:\dev\projects\aemcs\out\production\test;C:\Users\nalabotu\.m2\repository\com\adobe\aem\aem-sdk-api\2021.5.5343.20210524T070738Z-210527\aem-sdk-api-2021.5.5343.20210524T070738Z-210527.jar" apps.UploadToAEMCS


Program

1) Get the Bearer Token from AEM Developer Console



2) Set the AEM CS host name, AEM folder path, Bearer token and input file path in the following java stand alone program to perform the 3 steps (initiateUpload.json, Upload to Blob Storage, completeUpload.json). For uploading a big file the binary has to be split into multiple chunks and uploaded using various PUT urls provided by the initiateUpload.json response


package apps;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;

import org.apache.http.client.fluent.Form;
import org.apache.http.client.fluent.Request;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;

public class UploadToAEMCS {
    public static String AEM_CS_HOST = "https://author-p10961-e90064.adobeaemcloud.com";
    public static String AEM_FOLDER = "/content/dam/experience-aem";
    public static String INPUT_FILE = "C:/Users/nalabotu/Pictures/adobe.jpg";
    public static String BEARER_TOKEN = "eyJhbGci................";

    public static String INITIATE_UPLOAD_REQ = AEM_CS_HOST + AEM_FOLDER + ".initiateUpload.json";
    public static String FILE_NAME = "fileName";
    public static String UPLOAD_TOKEN = "uploadToken";
    public static String MIME_TYPE = "mimeType";
    public static String FILE_SIZE = "fileSize";

    public static void main(String[] args) throws Exception {
        String initiateUploadResponse = makeInitiateUploadRequest();

        JSONObject uploadResponse = new JSONObject(initiateUploadResponse);
        JSONArray filesJSON = uploadResponse.getJSONArray("files");
        JSONObject fileJSON = (JSONObject) filesJSON.get(0);

        FileInputStream fileIn = new FileInputStream(INPUT_FILE);
        byte[] fileBytes = IOUtils.toByteArray(fileIn);

        String binaryPUTUrl = fileJSON.getJSONArray("uploadURIs").getString(0);
        HttpResponse putResponse = Request.Put(binaryPUTUrl)
                .bodyByteArray(fileBytes).execute().returnResponse();

        int statusCode = putResponse.getStatusLine().getStatusCode();

        if( (statusCode < 200) || (statusCode > 210)){
            throw new Exception("Error uploading the binary");
        }

        String completeResponse = makeCompleteUploadRequest(uploadResponse);

        System.out.println("Uploaded : " + (AEM_FOLDER + completeResponse) );
    }

    private static String makeInitiateUploadRequest()throws Exception{
        File inputFile = new File(INPUT_FILE);

        Form form = Form.form();
        form.add(FILE_NAME, inputFile.getName());
        form.add(FILE_SIZE, String.valueOf(inputFile.length()));

        String initiateUploadResponse = Request.Post(INITIATE_UPLOAD_REQ)
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Authorization", "Bearer " + BEARER_TOKEN)
                .bodyForm(form.build()).execute().returnContent().asString();

        System.out.println(initiateUploadResponse);

        return initiateUploadResponse;
    }

    private static String makeCompleteUploadRequest(JSONObject uploadResponse) throws  Exception{
        JSONArray filesJSON = uploadResponse.getJSONArray("files");
        JSONObject fileJSON = (JSONObject) filesJSON.get(0);

        String uploadToken = fileJSON.getString("uploadToken");
        String mimeType = fileJSON.getString("mimeType");

        String completeURI = AEM_CS_HOST + uploadResponse.getString("completeURI");
        File inputFile = new File(INPUT_FILE);

        Form form = Form.form();
        form.add(UPLOAD_TOKEN, uploadToken);
        form.add(FILE_NAME, inputFile.getName());
        form.add(MIME_TYPE, mimeType);

        String completeResponse = Request.Post(completeURI)
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Authorization", "Bearer " + BEARER_TOKEN)
                .bodyForm(form.build()).execute().returnContent().asString();

        return completeResponse;
    }
}



2 comments:

  1. Wow is the only word I can use to describe the service of this platform. how to check airtel data balance via sms

    ReplyDelete
  2. Thanks for sharing this informing article. Looking forward to read more of your article. Also, check naub post-utme past questions & solutions

    ReplyDelete