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

[FEAT] Arbitrary Server to GCP bucket added #1244

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions girderformindlogger/api/v1/account_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,18 +271,20 @@ def getUsers(self, appletId, role='user', MRN='', sort=None, pagination=None):
Description('Update profile personal db uri')
.param('id', 'account id', required=True)
.param('dbURL', 'db uri for store the user responses', default=False, required=True)
.param('s3Bucket', 'S3 bucket name for uploading media responses', default=None, required=False)
.param('accessKeyId', 'S3 access key id', default=None, required=False)
.param('secretAccessKey', 'S3 secret access key', default=None, required=False)
.param('s3Bucket', 'Bucket name for uploading media responses', default=None, required=False)
.param('accessKeyId', 'Access key id', default=None, required=False)
.param('secretAccessKey', 'Secret access key', default=None, required=False)
.param('bucketType', 'Amazon S3, GCP or Azure', default=None, required=False)
)
def updateAccountDB(self, id, dbURL, s3Bucket, accessKeyId, secretAccessKey):
def updateAccountDB(self, id, dbURL, s3Bucket, accessKeyId, secretAccessKey, bucketType):
account = self._model.findOne({"accountId": ObjectId(id)})
self._model.validateDBURL(dbURL)
account.update({
'db': dbURL,
's3Bucket': s3Bucket,
'accessKeyId': accessKeyId,
'secretAccessKey': secretAccessKey,
'bucketType': bucketType,
})
self._model.save(account, validate=False)
return 'Information has been saved successfully.'
88 changes: 81 additions & 7 deletions girderformindlogger/api/v1/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def __init__(self):
self.route('GET', ('last7Days', ':applet'), self.getLast7Days)
self.route('GET', ('tokens', ':applet'), self.getResponseTokens)
self.route('POST', (':applet', ':activity'), self.createResponseItem)
self.route('POST', (':applet', 'downloadGCPData'), self.downloadGCPData)
self.route('POST', (':applet', 'updateResponseToken'), self.updateResponseToken)
self.route('PUT', (':applet',), self.updateReponseHistory)
self.route('GET', (':applet', 'reviews'), self.getReviewerResponses)
Expand Down Expand Up @@ -811,12 +812,22 @@ def createResponseItem(
self._model.reconnectToDb(db_uri=owner_account.get('db', None))

if owner_account and owner_account.get('s3Bucket', None) and owner_account.get('accessKeyId', None):
self.s3_client = boto3.client(
's3',
region_name=DEFAULT_REGION,
aws_access_key_id=owner_account.get('accessKeyId', None),
aws_secret_access_key=owner_account.get('secretAccessKey', None)
)
bucketType = owner_account.get('bucketType', None)
if bucketType and 'GCP' in bucketType or 'gcp' in bucketType:
self.s3_client = boto3.client(
's3',
region_name=DEFAULT_REGION,
endpoint_url="https://storage.googleapis.com",
aws_access_key_id=owner_account.get('accessKeyId', None),
aws_secret_access_key=owner_account.get('secretAccessKey', None)
)
else:
self.s3_client = boto3.client(
's3',
region_name=DEFAULT_REGION,
aws_access_key_id=owner_account.get('accessKeyId', None),
aws_secret_access_key=owner_account.get('secretAccessKey', None)
)

try:
newItem = self._model.createResponseItem(
Expand Down Expand Up @@ -875,7 +886,10 @@ def createResponseItem(
value['size']=metadata['responses'][key]['size']
value['type']=metadata['responses'][key]['type']
if owner_account and owner_account.get('s3Bucket', None):
value['uri']="s3://{}/{}".format(owner_account.get('s3Bucket', None),_file_obj_key)
if bucketType and 'GCP' in bucketType or 'gcp' in bucketType:
value['uri']="gs://{}/{}".format(owner_account.get('s3Bucket', None),_file_obj_key)
else:
value['uri']="s3://{}/{}".format(owner_account.get('s3Bucket', None),_file_obj_key)
else:
value['uri']="s3://{}/{}".format(os.environ['S3_MEDIA_BUCKET'],_file_obj_key)
# now, replace the metadata key with a link to this upload
Expand Down Expand Up @@ -1019,6 +1033,66 @@ def createResponseItem(
print(traceback.print_tb(sys.exc_info()[2]))
return(str(traceback.print_tb(sys.exc_info()[2])))

@access.public
@autoDescribeRoute(
Description('Download file from GCP.')
.notes(
'This endpoint is used to download GCP private bucket data.'
)
#.responseClass('Item')
.modelParam(
'applet',
model=AppletModel,
level=AccessType.READ,
destName='applet',
description='The ID of the Applet this response is to.'
)
.param('bucket', 'The name of the bucket.', required=True, default=None)
.param(
'key',
'The file path',
required=True, default=False)
.errorResponse()
.errorResponse('Write access was denied on the parent folder.', 403)
)
def downloadGCPData(
self,
applet,
bucket,
key
):
from girderformindlogger.models.profile import Profile
from girderformindlogger.models.account_profile import AccountProfile
try:
owner_account = AccountProfile().findOne({
'applets.owner': applet.get('_id')
})

if not owner_account or not owner_account.get('s3Bucket', None):
raise ValidationException(
"Couldn't find owner account for this response"
)

if owner_account and owner_account.get('s3Bucket', None) and owner_account.get('accessKeyId', None):
self.s3_client = boto3.client(
's3',
region_name=DEFAULT_REGION,
endpoint_url="https://storage.googleapis.com",
aws_access_key_id=owner_account.get('accessKeyId', None),
aws_secret_access_key=owner_account.get('secretAccessKey', None)
)
bytes_buffer = io.BytesIO()
self.s3_client.download_fileobj(Bucket=bucket, Key=key, Fileobj=bytes_buffer)
byte_value = bytes_buffer.getvalue()
return(byte_value)

return('')
except:
import sys, traceback
print(sys.exc_info())
print(traceback.print_tb(sys.exc_info()[2]))
return(str(traceback.print_tb(sys.exc_info()[2])))

@access.user(scope=TokenScope.DATA_WRITE)
@autoDescribeRoute(
Description('update user response items.')
Expand Down