Skip to content

Commit 2fd0025

Browse files
author
IlyaFaer
committed
merge unit test
2 parents 7c4bd6c + 55a6d42 commit 2fd0025

2 files changed

Lines changed: 169 additions & 215 deletions

File tree

google/cloud/storage/client.py

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -848,12 +848,12 @@ def get_hmac_key_metadata(
848848

849849
def generate_signed_post_policy_v4(
850850
self,
851-
credentials,
852851
bucket_name,
853852
blob_name,
854853
conditions,
855854
expiration,
856855
fields=None,
856+
credentials=None,
857857
virtual_hosted_style=False,
858858
bucket_bound_hostname=None,
859859
scheme=None,
@@ -869,12 +869,7 @@ def generate_signed_post_policy_v4(
869869
``credentials`` has a ``service_account_email`` property which
870870
identifies the credentials.
871871
872-
Generated policy object allows user to upload
873-
objects with a POST request.
874-
875-
:type credentials: :class:`google.auth.credentials.Signing`
876-
:param credentials: Credentials object with an associated private key to
877-
sign text.
872+
Generated policy object allows user to upload objects with a POST request.
878873
879874
:type bucket_name: str
880875
:param bucket_name: Bucket name.
@@ -892,6 +887,10 @@ def generate_signed_post_policy_v4(
892887
:type fields: dict
893888
:param fields: (Optional) Additional elements to include into request.
894889
890+
:type credentials: :class:`google.auth.credentials.Signing`
891+
:param credentials: (Optional) Credentials object with an associated private
892+
key to sign text.
893+
895894
:type virtual_hosted_style: bool
896895
:param virtual_hosted_style: (Optional) If True, construct the URL relative to the bucket
897896
virtual hostname, e.g., '<bucket-name>.storage.googleapis.com'.
@@ -915,7 +914,7 @@ def generate_signed_post_policy_v4(
915914
:param access_token: (Optional) Access token for a service account.
916915
917916
:rtype: dict
918-
:returns: Signed POST policy object.
917+
:returns: Signed POST policy.
919918
920919
Example:
921920
Generate signed POST policy and upload a file.
@@ -937,26 +936,18 @@ def generate_signed_post_policy_v4(
937936
files = {"file": ("bucket-name", f)}
938937
requests.post(policy["url"], data=policy["fields"], files=files)
939938
"""
939+
credentials = self._credentials if credentials is None else credentials
940940
ensure_signed_credentials(credentials)
941941

942-
now = _NOW()
943-
if expiration is None:
944-
expiration = now + datetime.timedelta(hours=1)
945-
946-
expiration_seconds = get_expiration_seconds_v4(expiration)
947-
policy_expires = now + datetime.timedelta(seconds=expiration_seconds)
948-
949942
timestamp, datestamp = get_v4_now_dtstamps()
950-
credential_scope = "{}/auto/storage/goog4_request".format(datestamp)
951943

944+
x_goog_credential = "{email}/{datestamp}/auto/storage/goog4_request".format(
945+
email=credentials.signer_email, datestamp=datestamp
946+
)
952947
required_conditions = [
953948
{"key": blob_name},
954949
{"x-goog-date": timestamp},
955-
{
956-
"x-goog-credential": "{email}/{scope}".format(
957-
email=credentials.signer_email, scope=credential_scope
958-
)
959-
},
950+
{"x-goog-credential": x_goog_credential},
960951
{"x-goog-algorithm": "GOOG4-RSA-SHA256"},
961952
]
962953

@@ -966,17 +957,20 @@ def generate_signed_post_policy_v4(
966957
for key, value in fields.items():
967958
if not key.startswith("x-ignore-"):
968959
policy_fields[key] = value
969-
if isinstance(value, list):
970-
conditions.append([key] + value)
971-
else:
972-
conditions.append({key: value})
960+
conditions.append({key: value})
973961

974962
conditions += required_conditions
975963

964+
now = _NOW()
965+
if expiration is None:
966+
expiration = now + datetime.timedelta(hours=1)
967+
968+
policy_expires = now + datetime.timedelta(
969+
seconds=get_expiration_seconds_v4(expiration)
970+
)
976971
policy = json.dumps(
977972
{"conditions": conditions, "expiration": policy_expires.isoformat() + "Z"},
978973
separators=(",", ":"),
979-
ensure_ascii=False,
980974
)
981975
str_to_sign = base64.b64encode(policy.encode("utf-8"))
982976

@@ -992,14 +986,13 @@ def generate_signed_post_policy_v4(
992986
{
993987
"key": blob_name,
994988
"x-goog-algorithm": "GOOG4-RSA-SHA256",
995-
"x-goog-credential": "{email}/{scope}".format(
996-
email=credentials.signer_email, scope=credential_scope
997-
),
989+
"x-goog-credential": x_goog_credential,
998990
"x-goog-date": timestamp,
999991
"x-goog-signature": signature,
1000992
"policy": str_to_sign,
1001993
}
1002994
)
995+
1003996
if virtual_hosted_style:
1004997
url = "https://{}.storage.googleapis.com/".format(bucket_name)
1005998

0 commit comments

Comments
 (0)