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

Support accessing public url #57

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
19 changes: 14 additions & 5 deletions fs_gcsfs/_gcsfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ def _get_blob(self, key: str) -> Optional[Blob]:
"""Returns blob if exists or None otherwise"""
return self.bucket.get_blob(key)

def _get_blob_from_path(self, path: str) -> Optional[Blob]:
"""Returns blob if exists or None otherwise"""
return self._get_blob(self._path_to_key(path))

def getinfo(self, path: str, namespaces: Optional[List[str]] = None, check_parent_dir: bool = True) -> Info:
if check_parent_dir:
self.check()
Expand All @@ -143,10 +147,9 @@ def getinfo(self, path: str, namespaces: Optional[List[str]] = None, check_paren
if _path == "/":
return self._dir_info("")

key = self._path_to_key(_path)
dir_key = self._path_to_dir_key(_path)

blob = self._get_blob(key)
blob = self._get_blob_from_path(_path)
if blob:
# Check if there exists a blob at the provided path, return the corresponding object Info
return self._info_from_blob(blob, namespaces)
Expand Down Expand Up @@ -176,7 +179,10 @@ def _info_from_blob(blob: Blob, namespaces: Optional[List[str]] = None) -> Info:
"type": int(ResourceType.file)
}
# TODO more namespaces: basic, urls, gcs, ...

if "urls" in namespaces:
info["urls"] = {
"http": blob.public_url
}
return Info(info)

def _dir_info(self, name: str) -> Info:
Expand Down Expand Up @@ -442,11 +448,14 @@ def exists(self, path: str) -> bool:
return self.isdir(path)

def geturl(self, path: str, purpose: str = "download"):
# TODO This does currently not have the same functionality as S3FS.geturl() (returning a public URL) which may be confusing to users
_path = self.validatepath(path)
_key = self._path_to_key(_path)
if purpose == "download":
return "gs://" + self.DELIMITER.join([self._bucket_name, _key])
blob = self._get_blob_from_path(_path)
if blob.public_url:
return blob.public_url
# TODO This does currently not have the ability to return a signed URL.
return None
else:
raise errors.NoURL(path, purpose)

Expand Down