Skip to content

Commit b59ff17

Browse files
boyxupernitisht
authored andcommitted
add easy-access methods to PostPolicy (#596)
- Keep post policy untouched while applying to Minio.presigned_post_policy - Add PostPolicy.append_policy for easy-access
1 parent 978fb46 commit b59ff17

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

minio/api.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,13 +1337,11 @@ def presigned_post_policy(self, post_policy):
13371337
credential_string = generate_credential_string(self._access_key,
13381338
date, region)
13391339

1340-
post_policy.policies.append(('eq', '$x-amz-date', iso8601_date))
1341-
post_policy.policies.append(
1342-
('eq', '$x-amz-algorithm', _SIGN_V4_ALGORITHM))
1343-
post_policy.policies.append(
1344-
('eq', '$x-amz-credential', credential_string))
1345-
1346-
post_policy_base64 = post_policy.base64()
1340+
post_policy_base64 = post_policy.base64(extras=[
1341+
('eq', '$x-amz-date', iso8601_date),
1342+
('eq', '$x-amz-algorithm', _SIGN_V4_ALGORITHM),
1343+
('eq', '$x-amz-credential', credential_string),
1344+
])
13471345
signature = post_presign_signature(date, region,
13481346
self._secret_key,
13491347
post_policy_base64)

minio/post_policy.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,15 @@ def set_content_length_range(self, min_length, max_length):
120120

121121
self._content_length_range = (min_length, max_length)
122122

123-
def _marshal_json(self):
123+
def append_policy(self, condition, target, value):
124+
self.policies.append([condition, target, value])
125+
126+
def _marshal_json(self, extras=()):
124127
"""
125128
Marshal various policies into json str/bytes.
126129
"""
127-
policies = self.policies
130+
policies = self.policies[:]
131+
policies.extend(extras)
128132
if self._content_length_range:
129133
policies.append(['content-length-range'] +
130134
list(self._content_length_range))
@@ -139,11 +143,11 @@ def _marshal_json(self):
139143

140144
return json.dumps(policy_stmt)
141145

142-
def base64(self):
146+
def base64(self, extras=()):
143147
"""
144148
Encode json into base64.
145149
"""
146-
s = self._marshal_json()
150+
s = self._marshal_json(extras=extras)
147151
s_bytes = s if isinstance(s, bytes) else s.encode('utf-8')
148152
b64enc = base64.b64encode(s_bytes)
149153
return b64enc.decode('utf-8') if isinstance(b64enc, bytes) else b64enc

0 commit comments

Comments
 (0)