Skip to content

Instantly share code, notes, and snippets.

@joshgachnang
Last active August 29, 2015 13:56
Show Gist options
  • Save joshgachnang/9237670 to your computer and use it in GitHub Desktop.
Save joshgachnang/9237670 to your computer and use it in GitHub Desktop.
Swift temp url
def _get_swift_temp_urls(self, url):
try:
key = SWIFT_TEMP_URL_KEY.encode('ascii', 'ignore')
except UnicodeDecodeError:
raise ValueError('SWIFT_TEMP_URL_KEY must be an ascii key.')
try:
temp_url_duration = int(SWIFT_TEMP_URL_DURATION)
except ValueError:
raise ValueError('SWIFT_TEMP_URL_DURATION must be an integer.')
method = SWIFT_TEMP_URL_METHOD
if method not in ['GET', 'PUT']:
raise ValueError('SWIFT_TEMP_URL_METHOD must be either GET or PUT')
# Parse out filename from glance url
try:
object_name = url['file'].split('/')[3]
except KeyError as e:
raise self.ImageProviderException(
'Image URL {} improperly formatted'.format(str(e))
)
template = '/v1/AUTH_{tenant}/{container}/{object_name}'
url = template.format(tenant=KEYSTONE_TENANT_ID,
container=GLANCE_SWIFT_CONTAINER,
object_name=object_name).lstrip('/')
expiration = int(time.time() + temp_url_duration)
hmac_body = '\n'.join([method, str(expiration), url])
sig = hmac.new(key, hmac_body, hashlib.sha1).hexdigest()
host = SWIFT_URL.rstrip('/')
return '{host}/{url}?temp_url_sig={sig}&temp_url_expires={exp}'
.format(host=host, url=url, sig=sig, exp=expiration)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment