Skip to content

Commit

Permalink
Merge pull request #98 from CellProfiler/issues/cp-3507
Browse files Browse the repository at this point in the history
Download images from s3
  • Loading branch information
mcquin authored Mar 5, 2018
2 parents 07c6c3c + f31a5e6 commit 73fd06c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
46 changes: 31 additions & 15 deletions bioformats/formatreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import numpy as np
import os
import sys
import re

if sys.version_info.major == 3:
from urllib.request import urlopen, urlparse, url2pathname
Expand All @@ -49,6 +50,7 @@
import bioformats
from . import metadatatools as metadatatools
import javabridge as javabridge
import boto3

OMERO_READER_IMPORTED = False
try:
Expand Down Expand Up @@ -607,21 +609,7 @@ def __init__(self, path=None, url=None, perform_init=True):
#
# Other URLS, copy them to a tempfile location
#
ext = url[url.rfind("."):]
src = urlopen(url)
dest_fd, self.path = tempfile.mkstemp(suffix=ext)
try:
dest = os.fdopen(dest_fd, 'wb')
shutil.copyfileobj(src, dest)
except:
src.close()
dest.close()
os.remove(self.path)
self.using_temp_file = True
src.close()
dest.close()
urlpath = urlparse(url)[2]
filename = unquote(urlpath.split("/")[-1])
filename = self.download(url)
else:
if sys.platform.startswith("win"):
self.path = self.path.replace("/", os.path.sep)
Expand Down Expand Up @@ -685,6 +673,34 @@ def __init__(self, path=None, url=None, perform_init=True):
if perform_init:
self.init_reader()

def download(self, url):
scheme = urlparse(url)[0]
ext = url[url.rfind("."):]
urlpath = urlparse(url)[2]
filename = unquote(urlpath.split("/")[-1])

self.using_temp_file = True

if scheme == 's3':
client = boto3.client('s3')
bucket_name, key = re.compile('s3://([\w\d\-\.]+)/(.*)').search(url).groups()
url = client.generate_presigned_url(
'get_object',
Params={'Bucket': bucket_name, 'Key': key.replace("+", " ")}
)

src = urlopen(url)
dest_fd, self.path = tempfile.mkstemp(suffix=ext)
try:
with os.fdopen(dest_fd, 'wb') as dest:
shutil.copyfileobj(src, dest)
except:
os.remove(self.path)
finally:
src.close()

return filename

def __enter__(self):
return self

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
]
},
install_requires=[
"boto3",
"future",
"javabridge>=1.0"
],
Expand Down

0 comments on commit 73fd06c

Please sign in to comment.