From a5cc93c2c8b1a5c0e48ee81a476d381b24404621 Mon Sep 17 00:00:00 2001 From: ncgl-syngenta Date: Thu, 3 Feb 2022 09:01:51 -0600 Subject: [PATCH] generate public url without signature (#100) * generate public url without signature * imports * update rules --- .pylintrc | 2 +- syngenta_digital_dta/s3/adapter.py | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/.pylintrc b/.pylintrc index fba4d49..229ceee 100644 --- a/.pylintrc +++ b/.pylintrc @@ -5,7 +5,7 @@ persistent=no unsafe-load-any-extension=no [MESSAGES CONTROL] -disable=import-star-module-level,old-octal-literal,oct-method,print-statement,unpacking-in-except,parameter-unpacking,backtick,old-raise-syntax,old-ne-operator,long-suffix,dict-view-method,dict-iter-method,metaclass-assignment,next-method-called,raising-string,indexing-exception,raw_input-builtin,long-builtin,file-builtin,execfile-builtin,coerce-builtin,cmp-builtin,buffer-builtin,basestring-builtin,apply-builtin,filter-builtin-not-iterating,using-cmp-argument,useless-suppression,range-builtin-not-iterating,suppressed-message,missing-docstring,no-absolute-import,old-division,cmp-method,reload-builtin,zip-builtin-not-iterating,intern-builtin,unichr-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,input-builtin,round-builtin,hex-method,nonzero-method,map-builtin-not-iterating,relative-import,invalid-name,bad-continuation,no-member,locally-disabled,fixme,import-error,too-many-locals,no-name-in-module,too-many-instance-attributes,no-self-use,logging-fstring-interpolation,W0703,W0702,R0904 +disable=W0212,C0116,missing-function-docstring,import-star-module-level,old-octal-literal,oct-method,print-statement,unpacking-in-except,parameter-unpacking,backtick,old-raise-syntax,old-ne-operator,long-suffix,dict-view-method,dict-iter-method,metaclass-assignment,next-method-called,raising-string,indexing-exception,raw_input-builtin,long-builtin,file-builtin,execfile-builtin,coerce-builtin,cmp-builtin,buffer-builtin,basestring-builtin,apply-builtin,filter-builtin-not-iterating,using-cmp-argument,useless-suppression,range-builtin-not-iterating,suppressed-message,missing-docstring,no-absolute-import,old-division,cmp-method,reload-builtin,zip-builtin-not-iterating,intern-builtin,unichr-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,input-builtin,round-builtin,hex-method,nonzero-method,map-builtin-not-iterating,relative-import,invalid-name,bad-continuation,no-member,locally-disabled,fixme,import-error,too-many-locals,no-name-in-module,too-many-instance-attributes,no-self-use,logging-fstring-interpolation,W0703,W0702,R0904 enable=W0603,global-statement [REPORTS] diff --git a/syngenta_digital_dta/s3/adapter.py b/syngenta_digital_dta/s3/adapter.py index 80a2c22..af41c84 100644 --- a/syngenta_digital_dta/s3/adapter.py +++ b/syngenta_digital_dta/s3/adapter.py @@ -1,9 +1,11 @@ -from io import BytesIO import os +from io import BytesIO import boto3 +import botocore import jsonpickle from botocore.exceptions import ClientError + from syngenta_digital_dta.common.base_adapter import BaseAdapter @@ -18,14 +20,20 @@ def __init__(self, **kwargs): self.aws_access_key_id = kwargs.get('aws_access_key_id') self.aws_secret_access_key = kwargs.get('aws_secret_access_key') self.region = kwargs.get('region') - self.client = boto3.client( + self.client = self.__make_client() + self.resource = self.__make_resource() + + def __make_client(self): + return boto3.client( 's3', endpoint_url=self.endpoint, aws_access_key_id=self.aws_access_key_id, aws_secret_access_key=self.aws_secret_access_key, region_name=self.region ) - self.resource = boto3.resource( + + def __make_resource(self): + return boto3.resource( 's3', endpoint_url=self.endpoint, aws_access_key_id=self.aws_access_key_id, @@ -102,6 +110,14 @@ def multipart_upload(self, **kwargs): super().publish('create', self.__generate_publish_data(**kwargs)) return complete_response + def create_public_url(self, **kwargs): + # we create a new client here to not modify other method calls + client = self.__make_client() + client._client_config.signature_version = botocore.UNSIGNED + return client.generate_presigned_url( + 'get_object', ExpiresIn=0, Params={'Bucket': self.bucket, 'Key': kwargs['s3_path']} + ) + def create_presigned_read_url(self, **kwargs): results = self.client.generate_presigned_url( 'get_object',