labpack/storage/aws/s3.py
def read_headers(self, bucket_name, record_key, record_version='', version_check=False):

        '''
            a method for retrieving the headers of a record from s3

        :param bucket_name: string with name of bucket
        :param record_key: string with key value of record
        :param record_version: [optional] string with aws id of version of record
        :param version_check: [optional] boolean to enable current version check
        :return: dictionary with headers of record
        '''
        
        title = '%s.read_headers' % self.__class__.__name__
        
        from datetime import datetime
        from dateutil.tz import tzutc
        
    # validate inputs
        input_fields = {
            'bucket_name': bucket_name,
            'record_key': record_key,
            'record_version': record_version
        }
        for key, value in input_fields.items():
            if value:
                object_title = '%s(%s=%s)' % (title, key, str(value))
                self.fields.validate(value, '.%s' % key, object_title)

    # verify existence of bucket
        if not bucket_name in self.bucket_list:
            if not bucket_name in self.list_buckets():
                raise ValueError('S3 bucket "%s" does not exist in aws region %s.' % (bucket_name, self.iam.region_name))
                
    # create key word argument dictionary
        headers_kwargs = {
            'Bucket': bucket_name,
            'Key': record_key
        }
        if record_version:
            headers_kwargs['VersionId'] = record_version

    # create metadata default
        metadata_details = {}
        
    # send request for record header
        try:
            record = self.connection.head_object(**headers_kwargs)
        except Exception as err:
            try:
                import requests
                requests.get('https://www.google.com')
                return metadata_details
            except:
                raise AWSConnectionError(title, captured_error=err)

    # create metadata from response
        metadata_details = {
            'key': record_key,
            'version_id': '',
            'current_version': True,
            'content_type': '',
            'content_encoding': '',
            'metadata': {}
        }
        metadata_details = self.iam.ingest(record, metadata_details)
        epoch_zero = datetime.fromtimestamp(0).replace(tzinfo=tzutc())
        metadata_details['last_modified'] = (metadata_details['last_modified'] - epoch_zero).total_seconds()
        if 'response_metadata' in metadata_details.keys():
            del metadata_details['response_metadata']

    # determine current version from version id
        if record_version and version_check:
            version_kwargs = {
                'Bucket': bucket_name,
                'Prefix': record_key
            }
            try:
                version_check = self.connection.list_object_versions(**version_kwargs)
                for version in version_check['Versions']:
                    if version['VersionId'] == metadata_details['version_id']:
                        metadata_details['current_version'] = version['IsLatest']
                        break
            except:
                raise AWSConnectionError(title)

        return metadata_details

            a method for retrieving the headers of a record from s3

        :param bucket_name: string with name of bucket
        :param record_key: string with key value of record
        :param record_version: [optional] string with aws id of version of record
        :param version_check: [optional] boolean to enable current version check
        :return: dictionary with headers of record
        
a method for retrieving the headers of a record from s3
a method for retrieving the headers of a record from s3
def read_headers(self, bucket_name, record_key, record_version='', version_check=False):
        title = '%s.read_headers' % self.__class__.__name__
        from datetime import datetime
        from dateutil.tz import tzutc
        input_fields = {
            'bucket_name': bucket_name,
            'record_key': record_key,
            'record_version': record_version
        }
        for key, value in input_fields.items():
            if value:
                object_title = '%s(%s=%s)' % (title, key, str(value))
                self.fields.validate(value, '.%s' % key, object_title)
        if not bucket_name in self.bucket_list:
            if not bucket_name in self.list_buckets():
                raise ValueError('S3 bucket "%s" does not exist in aws region %s.' % (bucket_name, self.iam.region_name))
        headers_kwargs = {
            'Bucket': bucket_name,
            'Key': record_key
        }
        if record_version:
            headers_kwargs['VersionId'] = record_version
        metadata_details = {}
        try:
            record = self.connection.head_object(**headers_kwargs)
        except Exception as err:
            try:
                import requests
                requests.get('https://www.google.com')
                return metadata_details
            except:
                raise AWSConnectionError(title, captured_error=err)
        metadata_details = {
            'key': record_key,
            'version_id': '',
            'current_version': True,
            'content_type': '',
            'content_encoding': '',
            'metadata': {}
        }
        metadata_details = self.iam.ingest(record, metadata_details)
        epoch_zero = datetime.fromtimestamp(0).replace(tzinfo=tzutc())
        metadata_details['last_modified'] = (metadata_details['last_modified'] - epoch_zero).total_seconds()
        if 'response_metadata' in metadata_details.keys():
            del metadata_details['response_metadata']
        if record_version and version_check:
            version_kwargs = {
                'Bucket': bucket_name,
                'Prefix': record_key
            }
            try:
                version_check = self.connection.list_object_versions(**version_kwargs)
                for version in version_check['Versions']:
                    if version['VersionId'] == metadata_details['version_id']:
                        metadata_details['current_version'] = version['IsLatest']
                        break
            except:
                raise AWSConnectionError(title)
        return metadata_details
def read_headers ( self , bucket_name , record_key , record_version = '' , version_check = False ) : title = '%s.read_headers' % self . __class__ . __name__ from datetime import datetime from dateutil . tz import tzutc input_fields = { 'bucket_name' : bucket_name , 'record_key' : record_key , 'record_version' : record_version } for key , value in input_fields . items ( ) : if value : object_title = '%s(%s=%s)' % ( title , key , str ( value ) ) self . fields . validate ( value , '.%s' % key , object_title ) if not bucket_name in self . bucket_list : if not bucket_name in self . list_buckets ( ) : raise ValueError ( 'S3 bucket "%s" does not exist in aws region %s.' % ( bucket_name , self . iam . region_name ) ) headers_kwargs = { 'Bucket' : bucket_name , 'Key' : record_key } if record_version : headers_kwargs [ 'VersionId' ] = record_version metadata_details = { } try : record = self . connection . head_object ( ** headers_kwargs ) except Exception as err : try : import requests requests . get ( 'https://www.google.com' ) return metadata_details except : raise AWSConnectionError ( title , captured_error = err ) metadata_details = { 'key' : record_key , 'version_id' : '' , 'current_version' : True , 'content_type' : '' , 'content_encoding' : '' , 'metadata' : { } } metadata_details = self . iam . ingest ( record , metadata_details ) epoch_zero = datetime . fromtimestamp ( 0 ) . replace ( tzinfo = tzutc ( ) ) metadata_details [ 'last_modified' ] = ( metadata_details [ 'last_modified' ] - epoch_zero ) . total_seconds ( ) if 'response_metadata' in metadata_details . keys ( ) : del metadata_details [ 'response_metadata' ] if record_version and version_check : version_kwargs = { 'Bucket' : bucket_name , 'Prefix' : record_key } try : version_check = self . connection . list_object_versions ( ** version_kwargs ) for version in version_check [ 'Versions' ] : if version [ 'VersionId' ] == metadata_details [ 'version_id' ] : metadata_details [ 'current_version' ] = version [ 'IsLatest' ] break except : raise AWSConnectionError ( title ) return metadata_details
a method for retrieving the headers of a record from s3
a method for retrieving the headers of a record from s3
'''
            a method for retrieving the headers of a record from s3

        :param bucket_name: string with name of bucket
        :param record_key: string with key value of record
        :param record_version: [optional] string with aws id of version of record
        :param version_check: [optional] boolean to enable current version check
        :return: dictionary with headers of record
        ''' # validate inputs # verify existence of bucket # create key word argument dictionary # create metadata default # send request for record header # create metadata from response # determine current version from version id
dictionary with headers of record
dictionary with headers of record
self
bucket_name
string with name of bucket
string with name of bucket
record_key
string with key value of record
string with key value of record
record_version
[optional] string with aws id of version of record
[ optional ] string with aws id of version of record
version_check
[optional] boolean to enable current version check
[ optional ] boolean to enable current version check