使用以下代码,我可以签署到Amazon S3的上传URL。我想补充的是能够限制上传大小。
AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider(configFileName, profileName));
GeneratePresignedUrlRequest generateUploadPresignedUrlRequest = new GeneratePresignedUrlRequest(BUCKETNAME, key);
generateUploadPresignedUrlRequest.setMethod(HttpMethod.PUT);
generateUploadPresignedUrlRequest.setExpiration(getFutureDate( 2*DAYS ));
URL uploadURL = s3client.generatePresignedUrl(generateUploadPresignedUrlRequest);
我知道我需要的是添加一个设置“content-length-range”的策略。示例:[“content-length-range”,0,10485760]
但我用Java编写的所有这些尝试都失败了.403 Forbidden。
编辑:我省略了非工作代码并在generatePresignedUrl之前添加了这个代码,这是我的“修复”。
Date futureDate = getFutureDate( 2*DAYS );
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String date = df.format(futureDate);
String policy = "{ \"expiration\": \""+date+"\","+
"\"conditions\": [" +
"{\"bucket\": \""+BUCKETNAME+"\" }," +
"['content-length-range', 0, 25000]," +
"]" +
"}";
generateUploadPresignedUrlRequest.addRequestParameter("Policy", policy);
答案 0 :(得分:0)
LOL - 发布后5分钟我发现了我的错误......
答案 - 注意"政策"而不是"政策" (大写P) generateUploadPresignedUrlRequest.addRequestParameter(" Policy",policyStr);
答案 1 :(得分:0)
我尝试了这段代码,aws s3失败并返回:
<?xml version="1.0" encoding="UTF-8"?> <Error><Code>MalformedPolicy</Code><Message>Policies must be valid JSON and the first byte must be '{'</Message><RequestId>D6617E1A36C438A4</RequestId><HostId>nsBW8QdBaRWXVWRv+FYQc9mWPab2ZQ4N85I5CWh7He2IxaX+OkZL+u01ZA2Cgihb0Oaa1o1/7A0=</HostId></Error>
我已经尝试过对Base64编码策略(如here所示),它给出了相同的结果。
似乎该策略字段不适用于预签名的网址。