java - AEM Assets API : Create intermediate directories as required

标签 java aem assets

我使用的是 AEM 6.3。我正在使用 Assets API 使用 curl 命令上传资源。我就是这样做的:

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

我的用例需要创建中间目录(如果它们不存在)。像这样的事情:

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

在上面的例子中,intermediate1和intermediate2都不存在。 curl 不起作用,AEM 返回:

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

500 错误很奇怪。从日志中我可以看到:

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)

如果中间文件夹不存在,AEM Assets API 是否可能不支持创建它们?也许 500 错误只是一个错误?

最佳答案

有 2 个选项可以使用 Assets API。您可以尝试以下任一方法:

下面的示例显示在 Assets 层次结构中创建 2 个新文件夹。

选项1

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

结果:

{"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}}

选项 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"}}"

结果

{"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}}


命令提示符中默认不识别单引号。将其更改为双引号应该可以工作


创建文件夹的其他方法,强制primarytype作为整个层次结构的sling:folder

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

关于java - AEM Assets API : Create intermediate directories as required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57593733/

相关文章:

adobe - Adobe Experience Manager (AEM) 6.2/6.3 使用什么版本的 Apache Sling?

authentication - CQ 如何验证每个请求?

php - Symfony 3 - {{asset ('assets/css/main.css' )}} 不工作

css - 管理所有 Assets 缓存的最佳实践(图像、CSS、JS 等)

java - Spring Boot 应用程序因未知原因崩溃

java - Guava 和 Weblogic :ClassNotFoundException

java - Adobe Experience Manager 路径的完整 URL

java - 使用 Apache Commons 数学所需的指导

java - 无法将消息从 Json 字符串转换为对象。类转换异常

android - 无法访问 Assets 子文件夹中的文件