Asset upload code for AEMaaCS | Community
Skip to main content
rsl_lucky
September 14, 2023

Asset upload code for AEMaaCS

  • September 14, 2023
  • 4 replies
  • 3003 views

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.

4 replies

Preetpal_Bindra
Community Advisor
Community Advisor
September 14, 2023

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/developer-reference-material-apis.html?lang=en#asset-upload

 

 

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

rsl_lucky
rsl_luckyAuthor
September 15, 2023

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.

DPrakashRaj
Community Advisor
Community Advisor
September 17, 2023

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/developer-reference-material-apis.html?lang=en or open source upload library mentioned on the above article 

rsl_lucky
rsl_luckyAuthor
September 20, 2023

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.

DPrakashRaj
Community Advisor
Community Advisor
September 20, 2023
kautuk_sahni
Community Manager
Community Manager
September 18, 2023

@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
rsl_lucky
rsl_luckyAuthor
October 17, 2023

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

December 18, 2024

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