回 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拼接不正确,还是其他原因?