差别
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 | 前一修订版 | ||
start:100serverintegration:40fileoperation [2017/09/22 07:12] |
start:100serverintegration:40fileoperation [2018/12/12 07:15] jk 已恢复为旧版 (2018/11/06 11:34) |
||
---|---|---|---|
行 1: | 行 1: | ||
+ | ====== 文件上传下载 ====== | ||
+ | ---- | ||
+ | |||
+ | 环信使用 REST 的方式来实现语音和图片等文件的上传下载。同时,为了保证聊天文件的安全,我们的 API 保证了以下几点: | ||
+ | |||
+ | * 只有 APP 的登录用户才能够上传文件。 | ||
+ | * 在上传文件的时候可以选择是否限制访问权限。 | ||
+ | * 如果选择限制的话,会在上传请求完成后返回一个 secret,只有知道这个 secret,并且是 APP 的注册用户,才能够下载文件。 | ||
+ | * 如果选择不限制的话,则只要是 APP 的注册用户就能够下载。 | ||
+ | |||
+ | ===== 上传语音/图片文件 ===== | ||
+ | |||
+ | 注意:上传文件大小不能超过 10M,超过会上传失败。 | ||
+ | |||
+ | <WRAP round tip> | ||
+ | 注意:在调用程序中,如果返回429或503错误,说明接口被限流了,请稍微暂停一下并重试。详见[[start:450errorcode:45restastrict|接口限流说明]]。 | ||
+ | </WRAP> | ||
+ | |||
+ | 所需要的 HTTP Header: * Authorization – 获取到的token * restrict-access – 是否限制访问权限。注意:这个 API 并没有考虑这个属性的值,而是有这个属性即可。最后,需要使用 HTTP multipart/form-data 形式。 | ||
+ | |||
+ | * Path: /{org_name}/{app_name}/chatfiles | ||
+ | * HTTP Method: POST | ||
+ | * Request Headers: {“restrict-access”:true,”Authorization”:”Bearer ${token}”} | ||
+ | * URL Params: 无 | ||
+ | * Request Body: 文件表单项 | ||
+ | * Response Body: 详情参见示例返回值,返回的 JSON 数据中会包含除上述属性之外的一些其他信息,均可以忽略。 | ||
+ | * 可能的错误码:401(未授权[无token、token错误、token过期])、5xx。详见:[[start:450errorcode:10restapierrorcode|服务器端 REST API 常见错误码]] | ||
+ | |||
+ | curl 示例: | ||
+ | |||
+ | <code php> | ||
+ | curl --verbose --header "Authorization: Bearer YWMtz1hFWOZpEeOPpcmw1FB0RwAAAUZnAv0D7y9-i4c9_c4rcx1qJDduwylRe7Y" --header "restrict-access:true" --form file=@/Users/stliu/a.jpg https://a1.easemob.com/easemob-demo/chatdemoui/chatfiles | ||
+ | </code> | ||
+ | |||
+ | 或者查看我们的[[https://github.com/easemob/emchat-server-examples|示例代码]]。 | ||
+ | |||
+ | Response 示例: | ||
+ | |||
+ | <code json> | ||
+ | { | ||
+ | "action" : "post",// POST请求 | ||
+ | "application" : "ecc6b6c0-e668-11e3-9d00-896af7b8411e", | ||
+ | "path" : "/chatfiles", | ||
+ | "uri" : "https://a1.easemob.com/easemob-demo/chatdemoui/chatfiles", //上传路径 | ||
+ | "entities" : [ { | ||
+ | "uuid" : "0c0f5f3a-e66b-11e3-8863-f1c202c2b3ae",//文件唯一ID,指定是哪个文件,发送消息时需要用到。 | ||
+ | "type" : "chatfile", | ||
+ | "share-secret" : "DRGM8OZrEeO1vafuJSo2IjHBeKlIhDp0GCnFu54xOF3M6KLr" //上传成功后返回,发送消息时需要用到。 | ||
+ | }], | ||
+ | "timestamp" : 1401283318916, | ||
+ | "duration" : 1841,//上传时间 | ||
+ | "organization" : "easemob-demo", | ||
+ | "applicationName" : "chatdemoui" | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | ===== 下载语音/图片文件 ===== | ||
+ | |||
+ | 这里需要注意的就是,需要在 HTTP header 中带上上面返回的 share-secret 和当前登录用户的 token 才能够下载,同时注意 header 中执行的 accept 的值需要设置成 application/octet-stream。 | ||
+ | |||
+ | <WRAP round tip> | ||
+ | 注意:在调用程序中,如果返回429或503错误,说明接口被限流了,请稍微暂停一下并重试。详见[[start:450errorcode:45restastrict|接口限流说明]]。 | ||
+ | </WRAP> | ||
+ | |||
+ | curl 示例: | ||
+ | |||
+ | <code php> | ||
+ | curl -O -H "share-secret: DRGM8OZrEeO1vafuJSo2IjHBeKlIhDp0GCnFu54xOF3M6KLr" --header "Authorization: Bearer YWMtz1hFWOZpEeOPpcmw1FB0RwAAAUZnAv0D7y9-i4c9_c4rcx1qJDduwylRe7Y" -H "Accept: application/octet-stream" https://a1.easemob.com/easemob-demo/chatdemoui/chatfiles/0c0f5f3a-e66b-11e3-8863-f1c202c2b3ae | ||
+ | </code> | ||
+ | |||
+ | ===== 下载缩略图 ===== | ||
+ | |||
+ | 环信支持在服务器端自动的创建图片的缩略图,可以先下载缩略图,当用户有需求的时候,再下载大图。 | ||
+ | |||
+ | 这里和下载大图唯一不同的就是 header 中多了一个 “thumbnail: true”,当服务器看到过来的请求的 header 中包括这个的时候,就会返回缩略图,否则返回原始大图。 | ||
+ | |||
+ | <WRAP round tip> | ||
+ | 注意:在调用程序中,如果返回429或503错误,说明接口被限流了,请稍微暂停一下并重试。详见[[start:450errorcode:45restastrict|接口限流说明]]。 | ||
+ | </WRAP> | ||
+ | |||
+ | curl 示例: | ||
+ | |||
+ | <code php> | ||
+ | curl -O -H "thumbnail: true" -H "share-secret: DRGM8OZrEeO1vafuJSo2IjHBeKlIhDp0GCnFu54xOF3M6KLr" -H "Authorization: Bearer YWMtz1hFWOZpEeOPpcmw1FB0RwAAAUZnAv0D7y9-i4c9_c4rcx1qJDduwylRe7Y" -H "Accept: application/octet-stream" https://a1.easemob.com/easemob-demo/chatdemoui/chatfiles/0c0f5f3a-e66b-11e3-8863-f1c202c2b3ae | ||
+ | </code> | ||
+ | |||
+ | ---- | ||
+ | <WRAP group> | ||
+ | <WRAP half column> | ||
+ | 上一页:[[start:100serverintegration:30chatlog|聊天记录]] | ||
+ | </WRAP> | ||
+ | |||
+ | <WRAP half column> | ||
+ | 下一页:[[start:100serverintegration:50messages|发送消息]] | ||
+ | </WRAP> | ||
+ | </WRAP> |