diff --git a/girderformindlogger/api/v1/account_profile.py b/girderformindlogger/api/v1/account_profile.py index 118e0688c..29623b0a9 100644 --- a/girderformindlogger/api/v1/account_profile.py +++ b/girderformindlogger/api/v1/account_profile.py @@ -271,11 +271,12 @@ 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({ @@ -283,6 +284,7 @@ def updateAccountDB(self, id, dbURL, s3Bucket, accessKeyId, secretAccessKey): 's3Bucket': s3Bucket, 'accessKeyId': accessKeyId, 'secretAccessKey': secretAccessKey, + 'bucketType': bucketType, }) self._model.save(account, validate=False) return 'Information has been saved successfully.' \ No newline at end of file diff --git a/girderformindlogger/api/v1/response.py b/girderformindlogger/api/v1/response.py index 41f6a966b..83d686f3c 100644 --- a/girderformindlogger/api/v1/response.py +++ b/girderformindlogger/api/v1/response.py @@ -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) @@ -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( @@ -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 @@ -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.')