最近在做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拼接不正确,还是其他原因?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。