Today, I investigated a problem and made some detours. I hope to provide references and suggestions for future arrivals.

Question:

XX feedback, callback using OSS PostObject does not occur callback. But with putobject the same callback happens. The customer suspects that there is something wrong with our PostObject.

Screen:

The OSS callback process is client >OSS > Application Server > Client



Create an ECS to use as the test application server

Create an asp.net ashx, code implementation, used to get OSS callback parameters.

Wrote two uploaded demos

  1. Putobject (examples are on the SDK help.aliyun.com/document_de…)
PutObjectRequest putObjectRequest=new PutObjectRequest(bucketName, key, new ByteArrayInputStream(content.getBytes()), meta); Callback callback = new Callback(); Callbackurl (" http://receiver IP/ revice.ashx "); callback.setCallbackBody("{\\\ "mimeType\\\":${mimeType},\\\ "size\\\":${size}} "); putObjectRequest.setCallback(callback); ossClient.putObject(putObjectRequest)Copy the code
  1. Postobject (Post Demo, see github.com/aliyun/aliy…)

Key code:

File file = new File(localFile); String filename = file.getName(); String contentType = new MimetypesFileTypeMap().getContentType(file); if (contentType == null || contentType.equals(" ")) { contentType = "application/octet-stream "; } StringBuffer strBuf = new StringBuffer(); strBuf.append("\r\n ").append("-- ").append(boundary).append("\r\n "); strBuf.append("Content-Disposition: form-data; name=\ "file\"; "+ "filename=\ "" + filename + "\"\r\n "); strBuf.append("Content-Type: " + contentType + "\r\n\r\n "); out.write(strBuf.toString().getBytes()); //callback part StringBuffer strBuf1 = new StringBuffer(); String callback = "{\" callbackUrl \ ": \" the http:// application receiving end IP/Revice ashx \ "and \" callbackBody \ ": \" {\ \ \ "bucket \ \ \ "=${bucket},\\\"size\\\ "=${size}}\"} "; byte[] textByte = callback.getBytes("UTF-8 "); strBuf1.append("\r\n ").append("-- ").append(boundary).append("\r\n "); String callbackstr=new String(Base64.encodeBase64(textByte)); strBuf1.append("Content-Disposition: form-data; name=\ "callback\"\r\n\r\n " + callbackstr + "\r\n\r\n "); out.write(strBuf1.toString().getBytes()); // Callback DataInputStream in = new DataInputStream(new FileInputStream(file)); int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = in.read(bufferOut)) ! = -1) { out.write(bufferOut, 0, bytes); } in.close(); byte[] endData = ("\r\n-- " + boundary + "--\r\n ").getBytes(); out.write(endData); out.flush(); out.close();Copy the code

Testing:

In Put mode, the application server has a callback request from OSS.

In Post mode, the application server does not receive any callback request



If the json format is incorrect or the callback fails, it should return the corresponding MSG. Not likely. If it was an online bug, the order would have been full.

Check the code and communicate with the OSS backend students. Check that the argument for callback is under file when sending the request.



This will be part of the file. Adjust your position



The business server also caught the request as expected

To sum up is careful, or careful. Don’t be impatient and debug a lot. You’ll get what you want in the end.

Attached is the complete PostObject Callback Java version code