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

Added basic support for temporary urls #13

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions swiftspec/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,34 @@ def get_tokens_from_env(self):
token = os.environ.get("OS_AUTH_TOKEN")
url = os.environ.get("OS_STORAGE_URL")
if token and url:
return [{"token": token, "url": url}]
return [{"X-Auth-Token": token, "url": url}]
d70-t marked this conversation as resolved.
Show resolved Hide resolved

sig = os.environ.get("TEMP_URL_SIG")
expire = os.environ.get("TEMP_URL_EXPIRES")
prf = os.environ.get("TEMP_URL_PREFIX")

if url and sig and expire:
return [{"url":url,"temp_url_sig": sig, "temp_url_expires": expire, "temp_url_prefix":prf}]
d70-t marked this conversation as resolved.
Show resolved Hide resolved
else:
return []

def headers_for_url(self, url):
headers = {}
for auth in self.auth:
if url.startswith(auth["url"]):
headers["X-Auth-Token"] = auth["token"]
if url.startswith(auth["url"]) and "X-Auth-Token" in auth:
headers["X-Auth-Token"]=auth["X-Auth-Token"]
d70-t marked this conversation as resolved.
Show resolved Hide resolved
break
return headers

def params_for_url(self, url):
params = {}
for auth in self.auth:
if url.startswith(auth["url"]) and "temp_url_sig" in auth:
d70-t marked this conversation as resolved.
Show resolved Hide resolved
params=auth.copy()
del params["url"]
d70-t marked this conversation as resolved.
Show resolved Hide resolved
break
return params

@classmethod
def _strip_protocol(cls, path):
"""For SWIFT, we always want to keep the full URL"""
Expand All @@ -167,6 +183,8 @@ async def _ls(self, path, detail=True, **kwargs):
"format": "json",
}
url = f"https://{ref.host}/v1/{ref.account}"
params.update(self.params_for_url(url))

async with session.get(
url, params=params, headers=self.headers_for_url(url)
) as res:
Expand All @@ -193,6 +211,8 @@ async def _ls(self, path, detail=True, **kwargs):
"prefix": prefix,
}
url = f"https://{ref.host}/v1/{ref.account}/{ref.container}"
params.update(self.params_for_url(url))

async with session.get(
url, params=params, headers=self.headers_for_url(url)
) as res:
Expand Down Expand Up @@ -222,6 +242,7 @@ def _raise_not_found_for_status(self, response, ref):
async def _cat_file(self, path, start=None, end=None, **kwargs):
ref = SWIFTRef(path)
headers = self.headers_for_url(ref.http_url)
params = self.params_for_url(ref.http_url)
if start is not None:
assert start >= 0
if end is not None:
Expand All @@ -235,7 +256,7 @@ async def _cat_file(self, path, start=None, end=None, **kwargs):
headers["Range"] = f"bytes=0-{end}"

session = await self.set_session()
async with session.get(ref.http_url, headers=headers) as res:
async with session.get(ref.http_url, params=params, headers=headers) as res:
self._raise_not_found_for_status(res, ref)
return await res.read()

Expand All @@ -251,13 +272,14 @@ async def _pipe_file(self, path, data, chunksize=50 * 2**20, **kwargs):

url = ref.http_url
headers = self.headers_for_url(url)
params = self.params_for_url(url)
headers["Content-Length"] = str(size)
if self.verify_uploads:
# in swift, ETag is alwas the MD5sum and will be used by the server to verify the upload
headers["ETag"] = md5(data).hexdigest()

session = await self.set_session()
async with session.put(url, data=data, headers=headers) as res:
async with session.put(url, data=data, params=params, headers=headers) as res:
res.raise_for_status()

async def _rm_file(self, path, missing_is_ok=False, **kwargs):
Expand Down Expand Up @@ -325,8 +347,9 @@ async def _info(self, path, **kwargs):
"size": None,
}
headers = self.headers_for_url(ref.http_url)
params = self.params_for_url(ref.http_url)
session = await self.set_session()
async with session.head(ref.http_url, headers=headers) as res:
async with session.head(ref.http_url, params=params, headers=headers) as res:
if res.status != 200:
raise FileNotFoundError(f"file '{ref.swift_url}' not found")
info = {
Expand Down