@@ -70,40 +70,7 @@ def prt_bytes(num_bytes, human_flag):
70
70
return '%.1f%s' % (num , suffix )
71
71
72
72
73
- def generate_temp_url (path , seconds , key , method , absolute = False ,
74
- prefix = False , iso8601 = False , ip_range = None ,
75
- digest = 'sha256' ):
76
- """Generates a temporary URL that gives unauthenticated access to the
77
- Swift object.
78
-
79
- :param path: The full path to the Swift object or prefix if
80
- a prefix-based temporary URL should be generated. Example:
81
- /v1/AUTH_account/c/o or /v1/AUTH_account/c/prefix.
82
- :param seconds: time in seconds or ISO 8601 timestamp.
83
- If absolute is False and this is the string representation of an
84
- integer, then this specifies the amount of time in seconds for which
85
- the temporary URL will be valid.
86
- If absolute is True then this specifies an absolute time at which the
87
- temporary URL will expire.
88
- :param key: The secret temporary URL key set on the Swift
89
- cluster. To set a key, run 'swift post -m
90
- "Temp-URL-Key: <substitute tempurl key here>"'
91
- :param method: A HTTP method, typically either GET or PUT, to allow
92
- for this temporary URL.
93
- :param absolute: if True then the seconds parameter is interpreted as a
94
- Unix timestamp, if seconds represents an integer.
95
- :param prefix: if True then a prefix-based temporary URL will be generated.
96
- :param iso8601: if True, a URL containing an ISO 8601 UTC timestamp
97
- instead of a UNIX timestamp will be created.
98
- :param ip_range: if a valid ip range, restricts the temporary URL to the
99
- range of ips.
100
- :param digest: digest algorithm to use. Must be one of ``sha1``,
101
- ``sha256``, or ``sha512``.
102
- :raises ValueError: if timestamp or path is not in valid format,
103
- or if digest is not one of ``sha1``, ``sha256``, or
104
- ``sha512``.
105
- :return: the path portion of a temporary URL
106
- """
73
+ def parse_timestamp (seconds , absolute = False ):
107
74
try :
108
75
try :
109
76
timestamp = float (seconds )
@@ -127,6 +94,20 @@ def generate_temp_url(path, seconds, key, method, absolute=False,
127
94
absolute = True
128
95
break
129
96
97
+ if t is None and not absolute :
98
+ for suffix , multiplier in (
99
+ ('s' , 1 ),
100
+ ('m' , 60 ),
101
+ ('min' , 60 ),
102
+ ('h' , 60 * 60 ),
103
+ ('hr' , 60 * 60 ),
104
+ ('d' , 24 * 60 * 60 ),
105
+ ):
106
+ if seconds .endswith (suffix ):
107
+ timestamp = t = int (
108
+ multiplier * float (seconds [:- len (suffix )]))
109
+ break
110
+
130
111
if t is None :
131
112
raise ValueError ()
132
113
else :
@@ -137,6 +118,46 @@ def generate_temp_url(path, seconds, key, method, absolute=False,
137
118
raise ValueError ()
138
119
except ValueError :
139
120
raise ValueError (TIME_ERRMSG )
121
+ return timestamp , absolute
122
+
123
+
124
+ def generate_temp_url (path , seconds , key , method , absolute = False ,
125
+ prefix = False , iso8601 = False , ip_range = None ,
126
+ digest = 'sha256' ):
127
+ """Generates a temporary URL that gives unauthenticated access to the
128
+ Swift object.
129
+
130
+ :param path: The full path to the Swift object or prefix if
131
+ a prefix-based temporary URL should be generated. Example:
132
+ /v1/AUTH_account/c/o or /v1/AUTH_account/c/prefix.
133
+ :param seconds: time in seconds or ISO 8601 timestamp.
134
+ If absolute is False and this is the string representation of an
135
+ integer, then this specifies the amount of time in seconds for which
136
+ the temporary URL will be valid. This may include a suffix to scale
137
+ the value: 's' for seconds, 'm' (or 'min') for minutes,
138
+ 'h' (or 'hr') for hours, or 'd' for days.
139
+ If absolute is True then this specifies an absolute time at which the
140
+ temporary URL will expire.
141
+ :param key: The secret temporary URL key set on the Swift
142
+ cluster. To set a key, run 'swift post -m
143
+ "Temp-URL-Key: <substitute tempurl key here>"'
144
+ :param method: A HTTP method, typically either GET or PUT, to allow
145
+ for this temporary URL.
146
+ :param absolute: if True then the seconds parameter is interpreted as a
147
+ Unix timestamp, if seconds represents an integer.
148
+ :param prefix: if True then a prefix-based temporary URL will be generated.
149
+ :param iso8601: if True, a URL containing an ISO 8601 UTC timestamp
150
+ instead of a UNIX timestamp will be created.
151
+ :param ip_range: if a valid ip range, restricts the temporary URL to the
152
+ range of ips.
153
+ :param digest: digest algorithm to use. Must be one of ``sha1``,
154
+ ``sha256``, or ``sha512``.
155
+ :raises ValueError: if timestamp or path is not in valid format,
156
+ or if digest is not one of ``sha1``, ``sha256``, or
157
+ ``sha512``.
158
+ :return: the path portion of a temporary URL
159
+ """
160
+ timestamp , absolute = parse_timestamp (seconds , absolute )
140
161
141
162
if isinstance (path , bytes ):
142
163
try :
0 commit comments