-
Notifications
You must be signed in to change notification settings - Fork 2
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
Feature/upload local recipes to s3 #189
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f14bb6e
upload local recipes to S3
rugeli 59b80b4
update README and add boto3 into requirements
rugeli 565487c
lint
rugeli c97d5a8
rerange import order
rugeli d09b64a
add save_file for S3 uploads and url generation
rugeli 2b22d68
delete redundant error msg
rugeli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import logging | ||
from pathlib import Path | ||
|
||
import boto3 | ||
from botocore.exceptions import ClientError | ||
|
||
|
||
class AWSHandler(object): | ||
""" | ||
Handles all the AWS S3 operations | ||
""" | ||
|
||
def __init__( | ||
self, | ||
bucket_name, | ||
sub_folder_name=None, | ||
region_name=None, | ||
): | ||
self.bucket_name = bucket_name | ||
self.folder_name = sub_folder_name | ||
session = boto3.Session() | ||
self.s3_client = session.client( | ||
"s3", | ||
endpoint_url=f"https://s3.{region_name}.amazonaws.com", | ||
region_name=region_name, | ||
) | ||
|
||
def get_aws_object_key(self, object_name): | ||
if self.folder_name is not None: | ||
object_name = self.folder_name + object_name | ||
else: | ||
object_name = object_name | ||
return object_name | ||
|
||
def upload_file(self, file_path): | ||
"""Upload a file to an S3 bucket | ||
:param file_path: File to upload | ||
:param bucket: Bucket to upload to | ||
:param object_name: S3 object name. If not specified then file_path is used | ||
:return: True if file was uploaded, else False | ||
""" | ||
|
||
file_name = Path(file_path).name | ||
|
||
object_name = self.get_aws_object_key(file_name) | ||
# Upload the file | ||
try: | ||
self.s3_client.upload_file(file_path, self.bucket_name, object_name) | ||
self.s3_client.put_object_acl( | ||
ACL="public-read", Bucket=self.bucket_name, Key=object_name | ||
) | ||
|
||
except ClientError as e: | ||
logging.error(e) | ||
return False | ||
return file_name | ||
|
||
def create_presigned_url(self, object_name, expiration=3600): | ||
"""Generate a presigned URL to share an S3 object | ||
:param object_name: string | ||
:param expiration: Time in seconds for the presigned URL to remain valid | ||
:return: Presigned URL as string. If error, returns None. | ||
""" | ||
object_name = self.get_aws_object_key(object_name) | ||
# Generate a presigned URL for the S3 object | ||
try: | ||
url = self.s3_client.generate_presigned_url( | ||
"get_object", | ||
Params={"Bucket": self.bucket_name, "Key": object_name}, | ||
ExpiresIn=expiration, | ||
) | ||
except ClientError as e: | ||
logging.error(e) | ||
return None | ||
# The response contains the presigned URL | ||
# https://{self.bucket_name}.s3.{region}.amazonaws.com/{object_key} | ||
return url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
# -*- coding: utf-8 -*- | ||
# standardmodule | ||
import os | ||
import webbrowser | ||
from pathlib import Path | ||
|
||
import matplotlib | ||
import numpy as np | ||
import trimesh | ||
from botocore.exceptions import NoCredentialsError | ||
|
||
from simulariumio import ( | ||
TrajectoryConverter, | ||
|
@@ -19,6 +23,7 @@ | |
from simulariumio.constants import DISPLAY_TYPE, VIZ_TYPE | ||
|
||
from cellpack.autopack.upy import hostHelper | ||
from cellpack.autopack.AWSHandler import AWSHandler | ||
import collada | ||
|
||
|
||
|
@@ -1335,6 +1340,7 @@ def writeToFile(self, file_name, bb, recipe_name, version): | |
spatial_units=UnitData("nm"), # nanometers | ||
) | ||
TrajectoryConverter(converted_data).save(file_name, False) | ||
return file_name | ||
|
||
def raycast(self, **kw): | ||
intersect = False | ||
|
@@ -1348,3 +1354,38 @@ def raycast(self, **kw): | |
|
||
def raycast_test(self, obj, start, end, length, **kw): | ||
return | ||
|
||
def post_and_open_file(self, file_name): | ||
simularium_file = Path(f"{file_name}.simularium") | ||
url = None | ||
try: | ||
url = simulariumHelper.store_results_to_s3(simularium_file) | ||
except Exception as e: | ||
aws_readme_url = "https://github.com/mesoscope/cellpack/blob/feature/main/README.md#aws-s3" | ||
if isinstance(e, NoCredentialsError): | ||
print( | ||
f"need to configure your aws account, find instructions here: {aws_readme_url}" | ||
) | ||
else: | ||
print( | ||
f"An error occurred while storing the file {simularium_file} to S3: {e}" | ||
) | ||
if url is not None: | ||
simulariumHelper.open_in_simularium(url) | ||
|
||
@staticmethod | ||
def store_results_to_s3(file_path): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might consider moving more of this into the handler. so in this file all you have to do is call |
||
handler = AWSHandler( | ||
bucket_name="cellpack-results", | ||
sub_folder_name="simularium/", | ||
region_name="us-west-2", | ||
) | ||
file_name = handler.upload_file(file_path) | ||
url = handler.create_presigned_url(file_name) | ||
return url | ||
|
||
@staticmethod | ||
def open_in_simularium(aws_url): | ||
webbrowser.open_new_tab( | ||
f"https://simularium.allencell.org/viewer?trajUrl={aws_url}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ | |
] | ||
|
||
requirements = [ | ||
"boto3>=1.28.3", | ||
"fire>=0.4.0", | ||
"firebase_admin>=6.0.1", | ||
"matplotlib>=3.3.4", | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is helpful, thanks for adding it in!