【问题标题】:AEM Asset API: Create intermediate directories as requiredAEM Asset API:根据需要创建中间目录
【发布时间】:2019-08-21 14:16:07
【问题描述】:

我使用的是 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 错误只是一个错误?

【问题讨论】:

    标签: java aem assets


    【解决方案1】:

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

    以下示例显示在资产层次结构中创建 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:整个层次结构的文件夹

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

    【讨论】:

    • 感谢您的回复。你的解决方案对我不起作用。对于第一个示例,我得到相同的 409,并带有此错误消息 {"class":["core/response"],"properties":{"path":"/api/assets/we-retail /","parentLocation":"/api/assets/we-retail.json","re​​ferer":"","changes":[],"location":"/api/assets/we- Retail/*.json","status.message":"父级不存在","status.code":409}}*.
    • 对于第二个例子,我得到一个 500 和这条消息 "{"class":"assetFolder","properties":{"title":"Images"}}" {" class":["core/response"],"properties":{"path":"/api/assets/we-retail/newfolder/images","parentLocation":"/api/assets/we-retail/newfolder .json","re​​ferer":"","changes":[],"location":"/api/assets/we-retail/newfolder/images.json","status.code":500}}我>
    • 您的 AEM 环境中似乎没有 we-retail 文件夹。如果您尝试在根级别创建文件夹,请删除路径中的 we-retail 并尝试。 curl -u admin:admin localhost:4502/api/assets* -F"name=newfolder" -F"title=New Folder"
    • curl -u admin:admin localhost:4502/api/assets* -F"name=newfolder" -F"title=New Folder" curl -u admin:admin localhost:4502/api/assets/newfolder/images -H"Content-Type: application /json" -d "{"class":"assetFolder","properties":{"title":"Images"}}"
    • 理想情况下,资产 API 正在寻找有关当前文件夹的特定信息,并且没有任何父文件夹的信息,这就是它无法创建父路径的原因。但在最后一个场景中,我们强制它将它们创建为 sling:Folder,因此它对所有中间文件夹使用相同的主类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 2015-04-11
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多