【问题标题】:Apigee: can't upload binary data for assetApigee:无法上传资产的二进制数据
【发布时间】:2014-01-08 07:37:19
【问题描述】:

我知道这是一项测试版功能,但我无法使用它。我可以创建资产,但是当我尝试上传数据时,我没有得到任何有用的东西。这就是我正在做的事情(javascript):

this.uploadImageData = function(asset, imageData) {
var options = {
method : 'POST',
endpoint : 'assets/' + asset.uuid + '/data',
data : imageData // <-- read in from a file using FileReader.readAsBinaryString()
}

console.log('preparing to upload image data');
console.log(options);
var self = this;
this.client.request(options, function(error, response) {
self.uploadingImageData(error, response);
});
}

这是我得到的回复:

Error (500)(web_application): undefined apigee.js:2975
Object {error: "web_application", timestamp: 1387435347814, duration: 0, exception: "javax.ws.rs.WebApplicationException"} apigee.js:2975

我真的可以借助某人的帮助来弄清楚发生了什么。

【问题讨论】:

    标签: javascript apigee


    【解决方案1】:

    2014 年及以后的更新:

    自 2014 年起,Usergrid / Apigee BaaS 资产机制已大大简化。您现在可以通过一次调用将二进制资产上传到任何实体。示例:

    curl -X POST -i -F name='clouds' -F file=@happy_clouds.jpg 'https://api.usergrid.com/your-org/your-app/pictures/'
    

    文档:http://apigee.com/docs/api-baas/content/assets


    遗留资产上传(此处为历史参考):

    我将为您提供完成此操作所需的 API POST 数据;希望您可以翻译此内容,以便通过一点努力对您的 javascript 进行故障排除。还要确保您使用的是最新版本的 SDK。

    首先,您必须创建资产占位符:

    POST "https://api.usergrid.com/{org}/{app}/assets" 
    
    {
        "name": "MyPicture.jpg",
        "owner": "{uuid_of_user_to_own}", 
        "path":"assets/{something_unique-will_throw_error_if_not}", 
        "content-type":"image/jpeg"
    }
    
    • 确保为此发送适当的授权标头
    • 确保在来自entities[0].uuid 的响应中捕获uuid

    接下来,您需要实际上传二进制数据:

    POST "https://api.usergrid.com/{org}/{app}/assets/{uuid_from_first_response}/data"
    

    有效载荷:[MyPicture.jpg] as binary data/file

    • 确保为此发送适当的授权标头
    • 确保将标题 Content-Type 设置为 application/octet-stream

    等价于:

    curl -X POST \
    -i "https://api.usergrid.com/{org}/{app}/assets/{uuid_from_first_response}/data" \
    --data-binary "@MyPicture.jpg" \
    -H "Content-Type: application/octet-stream" \
    -H "Authorization: Bearer {access_token}"
    

    如果您的请求成功(这在很大程度上取决于您是否发布了正确的二进制数据流!)您应该能够通过以下方式访问它:

    GET "https://api.usergrid.com/{org}/{app}/assets/{uuid_from_first_response}/data"
    

    要进行故障排除,请使用上述,或使用POSTMAN 之类的工具,该工具允许您在GUI 中设置标题,选择要上传的图像文件,并从头到尾运行整个过程.

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-23
      • 2014-10-31
      • 1970-01-01
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 2022-08-11
      • 1970-01-01
      相关资源
      最近更新 更多