开发者社区> 问答> 正文

OSS callback功能常见问题分析

展开
收起
秋二 2016-06-17 21:22:54 8197 0
3 条回答
写回答
取消 提交回答
  • ReOSS callback功能常见问题分析
    最近在写阿里云的上传回调功能,我的开发环境是Win10,开发语言是Java,代码基本照抄了CallbackSample.java里的代码,回调应用服务器使用的是官网提供的AppCallbackServer,启动9000端口,使用其他软件映射到外网域名(如www.abc.com)。使用手机访问该外网域名,可以访问到,证明不存在防火墙阻止的问题。使用telnet localhost 9000,端口也是通的,证明回调应用服务器正常工作。


    一、写main方法的测试用例,报400错,返回CallbackFailed

    main|com.aliyun.oss|WARN|com.aliyun.oss.common.utils.LogUtils.logException(LogUtils.java:62)|[Server]Unable to execute HTTP request: Error status : 400.
    [ErrorCode]: CallbackFailed
    [RequestId]: 59AFB837D9145FCA59DEDFD1
    [HostId]: xxx.oss-cn-hangzhou.aliyuncs.com##
    16:55:41.712 [main] ERROR cn.com.qiboot.NewConsumerProject.util.aliOssUtil.AliOssUtil - requestId is: 59AFB837D9145FCA59DEDFD1
    16:55:41.715 [main] ERROR cn.com.qiboot.NewConsumerProject.util.aliOssUtil.AliOssUtil - error code is: CallbackFailed
    16:55:41.715 [main] ERROR cn.com.qiboot.NewConsumerProject.util.aliOssUtil.AliOssUtil - Error Message: CallbackFailed
    16:55:41.715 [main] ERROR cn.com.qiboot.NewConsumerProject.util.aliOssUtil.AliOssUtil - Host ID:           xxx.oss-cn-hangzhou.aliyuncs.com


    二、使用wireShark,输入tcp.port == 9000 ||udp.port == 9000,没有任何结果。理论上来说,我已经监听该端口,wireShark应该是会有记录的吧


    看OSS的文档,400应该不会报CallbackFailed,不知道是什么情况,求大神指教

    2017-09-06 17:32:32
    赞同 展开评论 打赏
  • 回 1楼xgp的帖子
    楼主求帮助啊!能不能帮忙看看这个问题:
    最近在做OSS文件上传回调,直接用的.Net SDK中的上传回调代码
    这是我的代码:

    public void PutObjectCallback(string bucketName)
            {
                var client = new OssClient(endpoint, accessKeyId, accessKeySecret, securityToken);
                string key = "my_key;  
                string callbackUrl = "myUrl";
                string callbackBody = "bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&mimeType=${mimeType}&" +
                                    "patient_name=${x:var1}&patient_id=${x:var2}";
                try
                {
                    string responseContent = "";
                    var metadata = BuildCallbackMetadata(callbackUrl, callbackBody);
                    using (var fs = File.Open(fileToUpload, FileMode.Open))
                    {
                        var putObjectRequest = new PutObjectRequest(bucketName, key, fs, metadata);
                        var result = client.PutObject(putObjectRequest);
                        responseContent = GetCallbackResponse(result); //返回值一直是verify not ok
                    }
                    LightMessage MsgInfo = new LightMessage() { Light = LightType.Red, Message = "文件上传成功" + responseContent, Time = DateTime.Now };
                    StoreSCPMainForm.thisForm.Addmsg(MsgInfo);
                }
                catch (OssException ex)
                {
                    LightMessage MsgInfo = new LightMessage() { Light = LightType.Red, Message = "文件上传回调异常" + ex.ToString(), Time = DateTime.Now };
                    StoreSCPMainForm.thisForm.Addmsg(MsgInfo);
                    logMsg.WriteLog(new LogItem(LogType.Warnning, ex.ToString()));
                }
                catch (Exception ex)
                {
                    LightMessage MsgInfo = new LightMessage() { Light = LightType.Red, Message = "文件上传回调初始化失败" + ex.ToString(), Time = DateTime.Now };
                    StoreSCPMainForm.thisForm.Addmsg(MsgInfo);
                    logMsg.WriteLog(new LogItem(LogType.Warnning, ex.ToString()));
                }
            }
            private string GetCallbackResponse(PutObjectResult putObjectResult)
            {
                string callbackResponse = null;
                using (var stream = putObjectResult.ResponseStream)
                {
                    var buffer = new byte[4 * 1024];
                    var bytesRead = stream.Read(buffer, 0, buffer.Length);
                    callbackResponse = Encoding.Default.GetString(buffer, 0, bytesRead);
                }
                return callbackResponse;
            }
            private ObjectMetadata BuildCallbackMetadata(string callbackUrl, string callbackBody)
            {
                string callbackHeaderBuilder = new CallbackHeaderBuilder(callbackUrl, callbackBody).Build();
                string CallbackVariableHeaderBuilder = new CallbackVariableHeaderBuilder().
                    AddCallbackVariable("x:var1", "x:tangliang").AddCallbackVariable("x:patient_id", "x:000000").Build();
                var metadata = new ObjectMetadata();
                metadata.AddHeader(HttpHeaders.Callback, callbackHeaderBuilder);
                metadata.AddHeader(HttpHeaders.CallbackVar, CallbackVariableHeaderBuilder);
                return metadata;
            }
    单独上传是成功的,但是上传回调总是报错:{"Status":"verify not ok"},还请指教!是我的callbackBody拼接不正确,还是其他原因?
    2017-08-16 11:29:34
    赞同 展开评论 打赏
  • 阿里云对象存储(OSS)开发工程师
    回 楼主(xgp) 的帖子
    如果遇到callback的其他问题,可以在这个帖子中回复,或者到提供的链接下评论
    2016-06-20 09:35:49
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
OSS运维进阶实战手册 立即下载
《OSS运维基础实战手册》 立即下载
OSS运维基础实战手册 立即下载