Skip to content

Commit

Permalink
Merge pull request #11 from compute-tooling/private
Browse files Browse the repository at this point in the history
Make cloud storage files private
  • Loading branch information
hdoupe authored Jul 30, 2020
2 parents fea2829 + e0e3f20 commit d86dd4f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
17 changes: 9 additions & 8 deletions cs_storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,12 @@ def write(task_id, loc_result, do_upload=True, protocol="gcs"):


def read(rem_result, json_serializable=True, protocol="gcs"):
# compute studio results have public read access.
# fs = gcsfs.GCSFileSystem(token="anon")
s = time.time()
RemoteResult().load(rem_result)
read = {"renderable": [], "downloadable": []}
for category in rem_result:
with fs.open(
f"{protocol}://{BUCKET}/{rem_result[category]['ziplocation']}",
"rb",
**{protocol: {"token": "anon"}},
f"{protocol}://{BUCKET}/{rem_result[category]['ziplocation']}", "rb",
) as f:
res = f.read()

Expand All @@ -259,9 +255,14 @@ def read(rem_result, json_serializable=True, protocol="gcs"):
return read


def read_screenshot(screenshot_id, protocol="gcs"):
if not screenshot_id.endswith(".png"):
screenshot_id += ".png"
with fs.open(f"{protocol}://{BUCKET}/{screenshot_id}", "rb") as f:
return f.read()


def add_screenshot_links(rem_result):
for rem_output in rem_result["renderable"]["outputs"]:
rem_output[
"screenshot"
] = f"https://storage.googleapis.com/{BUCKET}/{rem_output['id']}.png"
rem_output["screenshot"] = f"{rem_output['id']}.png"
return rem_result
2 changes: 0 additions & 2 deletions cs_storage/screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
Template = None
launch = None

import cs_storage


CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))

Expand Down
19 changes: 12 additions & 7 deletions cs_storage/tests/test_cs_storage.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import base64
import io
import json
import uuid
import zipfile
import os

import pytest
import requests
from marshmallow import exceptions

import cs_storage
Expand Down Expand Up @@ -208,16 +205,24 @@ def test_cs_storage_serialization(exp_loc_res):
def test_add_screenshot_links():
rem_res = {"renderable": {"outputs": [{"id": "1234"}, {"id": "4567"}]}}

url = f"https://storage.googleapis.com/{cs_storage.BUCKET}/"
assert cs_storage.add_screenshot_links(rem_res) == {
"renderable": {
"outputs": [
{"id": "1234", "screenshot": url + "1234.png"},
{"id": "4567", "screenshot": url + "4567.png"},
{"id": "1234", "screenshot": "1234.png"},
{"id": "4567", "screenshot": "4567.png"},
]
}
}

current_dir = os.path.abspath(os.path.dirname(__file__))
with open(f"{current_dir}/test-tc-remote.json") as f:
remote_outputs = json.loads(f.read())["outputs"]

cs_storage.add_screenshot_links(remote_outputs)
for output in remote_outputs["renderable"]["outputs"]:
link = output["screenshot"]
assert isinstance(cs_storage.read_screenshot(link), bytes)


def test_errors():
with pytest.raises(exceptions.ValidationError):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
long_description_content_type="text/markdown",
url="https://github.com/compute-tooling/compute-studio-storage",
packages=setuptools.find_packages(),
install_requires=["marshmallow>=3.0.0", "gcsfs"],
install_requires=["marshmallow>=3.0.0", "fsspec", "gcsfs"],
include_package_data=True,
classifiers=[
"Programming Language :: Python :: 3",
Expand Down

0 comments on commit d86dd4f

Please sign in to comment.