Use multiline strings in `PostPolicyGenerator`
This commit is contained in:
parent
e6eb702a88
commit
e3ee5c1f2e
|
@ -20,33 +20,35 @@ public class PostPolicyGenerator {
|
||||||
private final String bucket;
|
private final String bucket;
|
||||||
private final String awsAccessId;
|
private final String awsAccessId;
|
||||||
|
|
||||||
public PostPolicyGenerator(String region, String bucket, String awsAccessId) {
|
public PostPolicyGenerator(final String region, final String bucket, final String awsAccessId) {
|
||||||
this.region = region;
|
this.region = region;
|
||||||
this.bucket = bucket;
|
this.bucket = bucket;
|
||||||
this.awsAccessId = awsAccessId;
|
this.awsAccessId = awsAccessId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pair<String, String> createFor(ZonedDateTime now, String object, int maxSizeInBytes) {
|
public Pair<String, String> createFor(final ZonedDateTime now, final String object, final int maxSizeInBytes) {
|
||||||
String expiration = now.plusMinutes(30).format(DateTimeFormatter.ISO_INSTANT);
|
final String expiration = now.plusMinutes(30).format(DateTimeFormatter.ISO_INSTANT);
|
||||||
String credentialDate = now.format(CREDENTIAL_DATE);
|
final String credentialDate = now.format(CREDENTIAL_DATE);
|
||||||
String requestDate = now.format(AWS_DATE_TIME);
|
final String requestDate = now.format(AWS_DATE_TIME);
|
||||||
String credential = String.format("%s/%s/%s/s3/aws4_request", awsAccessId, credentialDate, region);
|
final String credential = String.format("%s/%s/%s/s3/aws4_request", awsAccessId, credentialDate, region);
|
||||||
|
|
||||||
String policy = String.format("{ \"expiration\": \"%s\",\n" +
|
final String policy = String.format("""
|
||||||
" \"conditions\": [\n" +
|
{
|
||||||
" {\"bucket\": \"%s\"},\n" +
|
"expiration": "%s",
|
||||||
" {\"key\": \"%s\"},\n" +
|
"conditions": [
|
||||||
" {\"acl\": \"private\"},\n" +
|
{"bucket": "%s"},
|
||||||
" [\"starts-with\", \"$Content-Type\", \"\"],\n" +
|
{"key": "%s"},
|
||||||
" [\"content-length-range\", 1, " + maxSizeInBytes + "],\n" +
|
{"acl": "private"},
|
||||||
"\n" +
|
["starts-with", "$Content-Type", ""],
|
||||||
" {\"x-amz-credential\": \"%s\"},\n" +
|
["content-length-range", 1, %d],
|
||||||
" {\"x-amz-algorithm\": \"AWS4-HMAC-SHA256\"},\n" +
|
|
||||||
" {\"x-amz-date\": \"%s\" }\n" +
|
{"x-amz-credential": "%s"},
|
||||||
" ]\n" +
|
{"x-amz-algorithm": "AWS4-HMAC-SHA256"},
|
||||||
"}", expiration, bucket, object, credential, requestDate);
|
{"x-amz-date": "%s" }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
""", expiration, bucket, object, maxSizeInBytes, credential, requestDate);
|
||||||
|
|
||||||
return new Pair<>(credential, Base64.getEncoder().encodeToString(policy.getBytes(StandardCharsets.UTF_8)));
|
return new Pair<>(credential, Base64.getEncoder().encodeToString(policy.getBytes(StandardCharsets.UTF_8)));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue