开发者社区> 问答> 正文

此帖子作废 OSS上传回调

最近在做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:07:34 2817 0
1 条回答
写回答
取消 提交回答
  • 这句话是你程序里写的吗,还有其他报错信息吗
    2017-08-19 12:24:41
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

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