阿里云对象存储, 服务端签名直传并设置上传回调自定义参数

在服务端设置回调时,签名直传服务响应客户端发送给应用服务器的GET消息,代码片段如下:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

	String accessId = ""; // 请填写您的AccessKeyId。
	String accessKey = ""; // 请填写您的AccessKeySecret。
	String endpoint = "oss-cn-hangzhou.aliyuncs.com"; // 请填写您的 endpoint。
	String bucket = "bucket-name"; // 请填写您的 bucketname 。
	String host = "http://" + bucket + "." + endpoint; // host的格式为 bucketname.endpoint
	// callbackUrl为 上传回调服务器的URL,请将下面的IP和Port配置为您自己的真实信息。
	String callbackUrl = "http://88.88.88.88:8888";
	String dir = "user-dir-prefix/"; // 用户上传文件时指定的前缀。

	OSSClient client = new OSSClient(endpoint, accessId, accessKey);
	try {
		long expireTime = 30;
		long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
		Date expiration = new Date(expireEndTime);
		PolicyConditions policyConds = new PolicyConditions();
		policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
		policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);

		String postPolicy = client.generatePostPolicy(expiration, policyConds);
		byte[] binaryData = postPolicy.getBytes("utf-8");
		String encodedPolicy = BinaryUtil.toBase64String(binaryData);
		String postSignature = client.calculatePostSignature(postPolicy);

		Map respMap = new LinkedHashMap();
		respMap.put("accessid", accessId);
		respMap.put("policy", encodedPolicy);
		respMap.put("signature", postSignature);
		respMap.put("dir", dir);
		respMap.put("host", host);
		respMap.put("expire", String.valueOf(expireEndTime / 1000));
		// respMap.put("expire", formatISO8601Date(expiration));

		JSONObject jasonCallback = new JSONObject();
		jasonCallback.put("callbackUrl", callbackUrl);
		jasonCallback.put("callbackBody",
				"filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}");
		jasonCallback.put("callbackBodyType", "application/x-www-form-urlencoded");
		String base64CallbackBody = BinaryUtil.toBase64String(jasonCallback.toString().getBytes());
		respMap.put("callback", base64CallbackBody);

		JSONObject ja1 = JSONObject.fromObject(respMap);
		// System.out.println(ja1.toString());
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET, POST");
		response(request, response, ja1.toString());

	} catch (Exception e) {
		// Assert.fail(e.getMessage());
		System.out.println(e.getMessage());
	}
} 

其中的 callbackBody:filename=KaTeX parse error: Expected 'EOF', got '&' at position 9: {object}&̲size={size}&mimeType=KaTeX parse error: Expected 'EOF', got '&' at position 11: {mimeType}&̲height={imageInfo.height}&width=${imageInfo.width}&val1=“张三”&var2=“李四”,
将获取签名时携带需要的参数,写死在callbackBody中。

上传回调服务响应OSS发送给应用服务器的POST消息,得到上文callbackBody的参数

filename=“aa.txt”&size=140&mimeType=xxx&height=140&width=100&val1=“张三”&var2=“李四”,进行自己的业务处理。

你可能感兴趣的:(阿里云对象存储, 服务端签名直传并设置上传回调自定义参数)