Expand my Community achievements bar.

Asset upload code for AEMaaCS

Avatar

Level 5

Hi, we have asset upload servlet currently running on on-Prem version. Now plan to migrate it to AEMaaCS. Few APIs are deprecated and needs to use Cloud APIs for asset upload, delete activities. Anyone can you share asset upload functionality sample which would be compatible when deployed onto AEMaaCS with cloud api.

Really appreciate, if anyone  can share their pointers around it.

9 Replies

Avatar

Community Advisor

Hello @rsl_lucky ,

Please follow this article on how to upload assets to AEMaaCS. 

https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/assets/admin/develo...

 

 

Here is an overview of the process:

In Experience Manager as a Cloud Service, you can directly upload the assets to the cloud storage using HTTP API. The steps to upload a binary file are below. Execute these steps in an external application and not within the Experience Manager JVM.

  1. Submit an HTTP request. It informs Experience Manager deployment of your intent to upload a new binary.
  2. PUT the contents of the binary to one or more URIs provided by the initiation request.
  3. Submit an HTTP request to inform the server that the contents of the binary were successfully uploaded.

 

Here is a sample code snippets that explain "how to",

https://github.com/adobe/aem-upload

 

Hope that helps!

 

regards,

Preetpal Singh

Avatar

Level 5

Hi @Preetpal_Bindra  thanks for above information, went through those articles but unclear on approach were to kick start.

Currently aem 6.5 on-Prem version holds a servlet built with AssetManager api and it does the job of upload & delete of assets from repository. Now plan to move for AEMaaCS so in that case this servlet doesn't perform it's job as expected.

Needs to upgrade existing servlets to support AEMaaCS what are the steps need to be taken care here. Really appreciate if any inputs here that would help.

Avatar

Community Advisor

Aem asset create api is deprecated on aem cloud and it’s because of the reason that assets now don’t resides  on aem repository instead the binary go to s3 bucket so your existing servlet won’t work for aem cloud.

it seems for your use case you can either use asset upload api as explained on https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/assets/admin/develo... or open source upload library mentioned on the above article 

Avatar

Level 5

Thanks @DPrakashRaj  for the above details.

Can we have any sample steps for the approach to follow in my use case has currently servlet takes care of assets upload job for me. Please let me know the step by step process for the same.

Avatar

Level 5

Thanks @DPrakashRaj for the above info.

Came across below sample which does the 3-step process of assets upload but do not see any inputs params like fil I/p & others set while binary upload PUT, asset completion POST requests are made.

https://experiencing150.rssing.com/chan-25971229/article426.html

 

Could you let us know if this can be followed and what extra inclusion is required.

 

Avatar

Administrator

@rsl_lucky Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni

Avatar

Level 5

Awaiting users confirmation on further resolution required on other different discussion threads as part of the solution, it can be resolved. 

Avatar

Level 2

Hi @rsl_lucky .
I happened to touch this topic recently and here is the same code. The code below uploads and image under /content/dam.

String aemHost = "<aem-host>";

        long length = new File("test.png").length();

        InputStream is = new FileInputStream("test.png");

        Map<String, String> parameters = Map.of("fileSize", String.valueOf(length),
                "fileName", "test.png");

        String form = parameters.entrySet()
                .stream()
                .map(e -> e.getKey() + "=" + URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8))
                .collect(Collectors.joining("&"));

        System.out.println("Init upload");

        // initate upload and get meta info
        HttpClient httpClient = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder(URI.create(aemHost + "/content/dam.initiateUpload.json"))
                .POST(HttpRequest.BodyPublishers.ofString(form))
                .header("Authorization", "Bearer <access-token>")
                .header("Content-Type", "application/x-www-form-urlencoded")
                .build();

        HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
        String body = response.body();
        Gson gson = new Gson();
        UploadResponse uploadResponse = gson.fromJson(body, UploadResponse.class);
        System.out.println("Received Upload Info.");
        System.out.println();


        String uploadURI = uploadResponse.getFiles().get(0).uploadURIs.get(0);

        System.out.println("Uploading chunk...");

        // upload chunk to Blob storage
        HttpRequest uploadRequest = HttpRequest.newBuilder(URI.create(uploadURI))
                .header("Content-Type", "application/x-www-form-urlencoded")
                .PUT(HttpRequest.BodyPublishers.ofByteArray(is.readAllBytes()))
                .build();

        HttpResponse<String> upload = httpClient.send(uploadRequest, HttpResponse.BodyHandlers.ofString());
        if (upload.statusCode() != 201) {
            System.out.println("Upload chunk failed: " + upload.statusCode());
            System.out.println(upload.body());
            return;
        }
        System.out.println("Chunk has been uploaded successfully.");
        System.out.println();

        Map<String, String> params = Map.of("mimeType", "image/png",
                "uploadToken", uploadResponse.files.get(0).uploadToken,
                "fileName", "test.png");

        String uploadCompleteForm = params.entrySet()
                .stream()
                .map(e -> e.getKey() + "=" + e.getValue())
                .collect(Collectors.joining("&"));

        System.out.println("Completing upload...");

        // Complete upload - Commit the uploaded blocks
        HttpRequest uploadCompleteRequest = HttpRequest.newBuilder(URI.create(aemHost + uploadResponse.completeURI))
                .POST(HttpRequest.BodyPublishers.ofString(uploadCompleteForm))
                .header("Content-Type", "application/x-www-form-urlencoded")
                .header("Authorization", "Bearer <access-token>")
                .build();

        HttpResponse<String> uploadCompleteResponse = httpClient.send(uploadCompleteRequest, HttpResponse.BodyHandlers.ofString());
        System.out.println("Upload complete with status:  " + uploadCompleteResponse.statusCode());


@Data
    public class UploadResponse {

        @SerializedName("folderPath")
        @Expose
        public String folderPath;
        @SerializedName("files")
        @Expose
        public List<FileInfo> files;
        @SerializedName("completeURI")
        @Expose
        public String completeURI;

    }

    @Data
    public class FileInfo {

        @SerializedName("fileName")
        @Expose
        public String fileName;
        @SerializedName("uploadURIs")
        @Expose
        public List<String> uploadURIs;
        @SerializedName("uploadToken")
        @Expose
        public String uploadToken;
        @SerializedName("mimeType")
        @Expose
        public String mimeType;
        @SerializedName("minPartSize")
        @Expose
        public Integer minPartSize;
        @SerializedName("maxPartSize")
        @Expose
        public Integer maxPartSize;

    }