Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add easy-access methods to PostPolicy #596

Merged
merged 2 commits into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 5 additions & 7 deletions minio/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,13 +1337,11 @@ def presigned_post_policy(self, post_policy):
credential_string = generate_credential_string(self._access_key,
date, region)

post_policy.policies.append(('eq', '$x-amz-date', iso8601_date))
post_policy.policies.append(
('eq', '$x-amz-algorithm', _SIGN_V4_ALGORITHM))
post_policy.policies.append(
('eq', '$x-amz-credential', credential_string))

post_policy_base64 = post_policy.base64()
post_policy_base64 = post_policy.base64(extras=[
('eq', '$x-amz-date', iso8601_date),
('eq', '$x-amz-algorithm', _SIGN_V4_ALGORITHM),
('eq', '$x-amz-credential', credential_string),
])
signature = post_presign_signature(date, region,
self._secret_key,
post_policy_base64)
Expand Down
12 changes: 8 additions & 4 deletions minio/post_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,15 @@ def set_content_length_range(self, min_length, max_length):

self._content_length_range = (min_length, max_length)

def _marshal_json(self):
def append_policy(self, condition, target, value):
self.policies.append([condition, target, value])

def _marshal_json(self, extras=()):
"""
Marshal various policies into json str/bytes.
"""
policies = self.policies
policies = self.policies[:]
policies.extend(extras)
if self._content_length_range:
policies.append(['content-length-range'] +
list(self._content_length_range))
Expand All @@ -139,11 +143,11 @@ def _marshal_json(self):

return json.dumps(policy_stmt)

def base64(self):
def base64(self, extras=()):
"""
Encode json into base64.
"""
s = self._marshal_json()
s = self._marshal_json(extras=extras)
s_bytes = s if isinstance(s, bytes) else s.encode('utf-8')
b64enc = base64.b64encode(s_bytes)
return b64enc.decode('utf-8') if isinstance(b64enc, bytes) else b64enc
Expand Down