import java.text.SimpleDateFormat;
为什么 我把dir 值 1/ 改为 user-dir 就可以上传了
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.SimpleTimeZone;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
public class PostPolicy {
static final String endpoint = "http://******.oss-cn-hangzhou.aliyuncs.com";
static final String accessId = "pWnzccu8tGQtC1dU";
static final String accessKey = "J7HfpzLCqEWMAQHVhsbqXYanUxVzGG";
static final String bucketName = "post-test";
public static Map<String, String> getPostPolicy(String user_id){
OSSClient client = new OSSClient(endpoint, accessId, accessKey);
try {
Date expiration = new Date(new Date().getTime() + 2900 * 1000);
PolicyConditions policyConds = new PolicyConditions();
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, "user-dir");
String postPolicy = client.generatePostPolicy(expiration, policyConds);
byte[] binaryData = postPolicy.getBytes("utf-8");
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
String postSignature = client.calculatePostSignature(postPolicy);
Map<String, String> respMap = new LinkedHashMap<String, String>();
respMap.put("accessid", accessId);
respMap.put("host", endpoint);
respMap.put("policy", encodedPolicy);
respMap.put("signature", postSignature);
respMap.put("expire", formatISO8601Date(expiration));
respMap.put("dir", "1/");
System.out.println(respMap.size());
return respMap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static String formatISO8601Date(Date date) {
final String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.CHINA);
dateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
return dateFormat.format(date);
}
}
而 1/ 或者 1 就会报错 错误如下
Error xml:<?xml version="1.0" encoding="UTF-8"?>
请大侠帮帮试试 顺便解答一下
<Error>
<Code>AccessDenied</Code>
<Message>Invalid according to Policy: Policy Condition failed: ["starts-with", "$key", "user-dir"]</Message>
<RequestId>566B88CAB295345D15A80F00</RequestId>
<HostId>*******.oss-cn-hangzhou.aliyuncs.com</HostId>
</Error>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。