Perimosh
Perimosh

Reputation: 2794

AEM Asset API: Create intermediate directories as required

I'm using AEM 6.3. I'm using the Assets API to upload assets using curl commands. This is how I'm doing it:

curl -i 'http://localhost:4502/api/assets/newFolder' -H "Content-Type: application/json" -d '{"class":"assetFolder","properties":{"title":"New folder"}}' -u admin:admin

My use case requieres to create intermediate directories if they don't exist. Something like this:

curl -i 'http://localhost:4502/api/assets/intermediate1/intermediate2/newFolder' -H "Content-Type: application/json" -d '{"class":"assetFolder","properties":{"title":"New folder"}}' -u admin:admin

In the example above, neither intermediate1 nor intermediate2 exist. The curl doesn't work, AEM returns:

HTTP/1.1 500 Server Error
Date: Wed, 21 Aug 2019 14:13:35 GMT
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Set-Cookie: cq-authoring-mode=CLASSIC;Path=/;Expires=Wed, 28-Aug-2019 14:13:35 GMT
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json; charset=UTF-8
Transfer-Encoding: chunked

{"class":["core/response"],"properties":{"path":"/api/assets/intermediate1/intermediate2/newFolder","parentLocation":"/api/assets/intermediate1/intermediate2.json","referer":"","changes":[],"location":"/api/assets/intermediate1/intermediate2/newFolder.json","status.code":500}}

The 500 error is weird. From logs I can see:

21.08.2019 11:13:35.512 *ERROR* [0:0:0:0:0:0:0:1 [1566396815508] POST /api/assets/intermediate1/intermediate2/newFolder HTTP/1.1] com.adobe.granite.rest.impl.servlet.PostRequest Exception during request processing.
java.lang.NullPointerException: null
    at com.adobe.granite.rest.assets.impl.AssetResourceProvider.getDataPropertyKey(AssetResourceProvider.java:412)

Maybe the AEM Assets API doesn't support the creation of intermediate folders if they don't exist? And maybe the 500 error is just a bug?

Upvotes: 1

Views: 2493

Answers (1)

Sudheer Donaboina
Sudheer Donaboina

Reputation: 416

There are 2 options to use the Assets API. you can try any one of the below:

Below examples displays creating 2 new folders in the assets hierarchy.

Option1

curl -u admin:admin  http://localhost:4502/api/assets/we-retail/* -F"name=newfolder" -F"title=New Folder"

Result:

{"class":["core/response"],"properties":{"path":"/api/assets/we-retail/*","parentLocation":"/api/assets/we-retail.json","referer":"","isCreate":true,"changes":[{"argument":"/api/assets/we-retail/newfolder","type":"created"}],"location":"/api/assets/we-retail/newfolder.json","status.message":"Created","title":"Content created /api/assets/we-retail/*","status.code":201}}

Option 2

curl -u admin:admin  http://localhost:4502/api/assets/we-retail/newfolder/images -H"Content-Type: application/json" -d "{"class":"assetFolder","properties":{"title":"Images"}}"

Result

{"class":["core/response"],"properties":{"path":"/api/assets/we-retail/newfolder/images","parentLocation":"/api/assets/we-retail/newfolder.json","referer":"","isCreate":true,"changes":[{"argument":"/api/assets/we-retail/newfolder/images","type":"created"}],"location":"/api/assets/we-retail/newfolder/images.json","status.message":"Created","title":"Content created /api/assets/we-retail/newfolder/images","status.code":201}}


Single Quotes are not recognized by default in command prompt. Changing it to double quotes should work


Other way to create the folders forcing primarytype as sling:folder for the entire hierarchy

curl -u admin:admin -Fjcr:primaryType=sling:Folder localhost:4502/content/dam/mFolder/aFolder

Upvotes: 1

Related Questions