labpack/storage/aws/s3.py
def list_versions(self, bucket_name, prefix='', delimiter='', max_results=1000, starting_key='', starting_version=''):

        '''
            a method for retrieving a list of the versions of records in a bucket

        :param bucket_name: string with name of bucket
        :param prefix: [optional] string with value limiting results to key prefix
        :param delimiter: [optional] string with value limiting results to key delimiter
        :param max_results: [optional] integer with max results to return
        :param starting_key: [optional] string with key value to continue search with
        :param starting_version: [optional] string with version id to continue search with
        :return: list of results with key, size and date, dictionary with key and version id
        '''

        title = '%s.list_versions' % self.__class__.__name__
        
        from datetime import datetime
        from dateutil.tz import tzutc
         
    # validate inputs
        input_fields = {
            'bucket_name': bucket_name,
            'prefix': prefix,
            'delimiter': delimiter,
            'max_results': max_results,
            'starting_key': starting_key,
            'starting_version': starting_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)
    
    # validate mutual requirements
        if starting_version or starting_key:
            if not starting_version or not starting_key:
                raise ValueError('%s inputs starting_key and starting_version each require the other.' % 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
        kw_args = {
            'Bucket': bucket_name
        }
        if starting_key:
            kw_args['KeyMarker'] = starting_key
        if starting_version:
            kw_args['VersionIdMarker'] = starting_version
        if prefix:
            kw_args['Prefix'] = prefix
        if delimiter:
            kw_args['Delimiter'] = delimiter
        if max_results:
            kw_args['MaxKeys'] = max_results

    # send request for objects
        record_list = []
        next_key = {}
        try:
            response = self.connection.list_object_versions(**kw_args)
        except:
            raise AWSConnectionError(title)

    # add version keys and ids to object list
        if 'Versions' in response:
            for record in response['Versions']:
                details = {
                    'key': '',
                    'version_id': ''
                }
                details = self.iam.ingest(record, details)
                epoch_zero = datetime.fromtimestamp(0).replace(tzinfo=tzutc())
                details['last_modified'] = (details['last_modified'] - epoch_zero).total_seconds()
                details['current_version'] = details['is_latest']
                del details['is_latest']
                record_list.append(details)

    # add delete markers to object list
        if 'DeleteMarkers' in response:
            for record in response['DeleteMarkers']:
                details = {
                    'key': '',
                    'version_id': ''
                }
                details = self.iam.ingest(record, details)
                epoch_zero = datetime.fromtimestamp(0).replace(tzinfo=tzutc())
                details['last_modified'] = (details['last_modified'] - epoch_zero).total_seconds()
                details['current_version'] = details['is_latest']
                del details['is_latest']
                if not 'size' in details.keys():
                    details['size'] = 0
                record_list.append(details)

    # define next key value
        if response['IsTruncated']:
            next_key = {
                'key': response['NextKeyMarker'],
                'version_id': response['NextVersionIdMarker']
            }

        return record_list, next_key

            a method for retrieving a list of the versions of records in a bucket

        :param bucket_name: string with name of bucket
        :param prefix: [optional] string with value limiting results to key prefix
        :param delimiter: [optional] string with value limiting results to key delimiter
        :param max_results: [optional] integer with max results to return
        :param starting_key: [optional] string with key value to continue search with
        :param starting_version: [optional] string with version id to continue search with
        :return: list of results with key, size and date, dictionary with key and version id
        
a method for retrieving a list of the versions of records in a bucket
a method for retrieving a list of the versions of records in a bucket
def list_versions(self, bucket_name, prefix='', delimiter='', max_results=1000, starting_key='', starting_version=''):
        title = '%s.list_versions' % self.__class__.__name__
        from datetime import datetime
        from dateutil.tz import tzutc
        input_fields = {
            'bucket_name': bucket_name,
            'prefix': prefix,
            'delimiter': delimiter,
            'max_results': max_results,
            'starting_key': starting_key,
            'starting_version': starting_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 starting_version or starting_key:
            if not starting_version or not starting_key:
                raise ValueError('%s inputs starting_key and starting_version each require the other.' % 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))
        kw_args = {
            'Bucket': bucket_name
        }
        if starting_key:
            kw_args['KeyMarker'] = starting_key
        if starting_version:
            kw_args['VersionIdMarker'] = starting_version
        if prefix:
            kw_args['Prefix'] = prefix
        if delimiter:
            kw_args['Delimiter'] = delimiter
        if max_results:
            kw_args['MaxKeys'] = max_results
        record_list = []
        next_key = {}
        try:
            response = self.connection.list_object_versions(**kw_args)
        except:
            raise AWSConnectionError(title)
        if 'Versions' in response:
            for record in response['Versions']:
                details = {
                    'key': '',
                    'version_id': ''
                }
                details = self.iam.ingest(record, details)
                epoch_zero = datetime.fromtimestamp(0).replace(tzinfo=tzutc())
                details['last_modified'] = (details['last_modified'] - epoch_zero).total_seconds()
                details['current_version'] = details['is_latest']
                del details['is_latest']
                record_list.append(details)
        if 'DeleteMarkers' in response:
            for record in response['DeleteMarkers']:
                details = {
                    'key': '',
                    'version_id': ''
                }
                details = self.iam.ingest(record, details)
                epoch_zero = datetime.fromtimestamp(0).replace(tzinfo=tzutc())
                details['last_modified'] = (details['last_modified'] - epoch_zero).total_seconds()
                details['current_version'] = details['is_latest']
                del details['is_latest']
                if not 'size' in details.keys():
                    details['size'] = 0
                record_list.append(details)
        if response['IsTruncated']:
            next_key = {
                'key': response['NextKeyMarker'],
                'version_id': response['NextVersionIdMarker']
            }
        return record_list, next_key
def list_versions ( self , bucket_name , prefix = '' , delimiter = '' , max_results = 1000 , starting_key = '' , starting_version = '' ) : title = '%s.list_versions' % self . __class__ . __name__ from datetime import datetime from dateutil . tz import tzutc input_fields = { 'bucket_name' : bucket_name , 'prefix' : prefix , 'delimiter' : delimiter , 'max_results' : max_results , 'starting_key' : starting_key , 'starting_version' : starting_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 starting_version or starting_key : if not starting_version or not starting_key : raise ValueError ( '%s inputs starting_key and starting_version each require the other.' % 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 ) ) kw_args = { 'Bucket' : bucket_name } if starting_key : kw_args [ 'KeyMarker' ] = starting_key if starting_version : kw_args [ 'VersionIdMarker' ] = starting_version if prefix : kw_args [ 'Prefix' ] = prefix if delimiter : kw_args [ 'Delimiter' ] = delimiter if max_results : kw_args [ 'MaxKeys' ] = max_results record_list = [ ] next_key = { } try : response = self . connection . list_object_versions ( ** kw_args ) except : raise AWSConnectionError ( title ) if 'Versions' in response : for record in response [ 'Versions' ] : details = { 'key' : '' , 'version_id' : '' } details = self . iam . ingest ( record , details ) epoch_zero = datetime . fromtimestamp ( 0 ) . replace ( tzinfo = tzutc ( ) ) details [ 'last_modified' ] = ( details [ 'last_modified' ] - epoch_zero ) . total_seconds ( ) details [ 'current_version' ] = details [ 'is_latest' ] del details [ 'is_latest' ] record_list . append ( details ) if 'DeleteMarkers' in response : for record in response [ 'DeleteMarkers' ] : details = { 'key' : '' , 'version_id' : '' } details = self . iam . ingest ( record , details ) epoch_zero = datetime . fromtimestamp ( 0 ) . replace ( tzinfo = tzutc ( ) ) details [ 'last_modified' ] = ( details [ 'last_modified' ] - epoch_zero ) . total_seconds ( ) details [ 'current_version' ] = details [ 'is_latest' ] del details [ 'is_latest' ] if not 'size' in details . keys ( ) : details [ 'size' ] = 0 record_list . append ( details ) if response [ 'IsTruncated' ] : next_key = { 'key' : response [ 'NextKeyMarker' ] , 'version_id' : response [ 'NextVersionIdMarker' ] } return record_list , next_key
a method for retrieving a list of the versions of records in a bucket
a method for retrieving a list of the versions of records in a bucket
'''
            a method for retrieving a list of the versions of records in a bucket

        :param bucket_name: string with name of bucket
        :param prefix: [optional] string with value limiting results to key prefix
        :param delimiter: [optional] string with value limiting results to key delimiter
        :param max_results: [optional] integer with max results to return
        :param starting_key: [optional] string with key value to continue search with
        :param starting_version: [optional] string with version id to continue search with
        :return: list of results with key, size and date, dictionary with key and version id
        ''' # validate inputs # validate mutual requirements # verify existence of bucket # create key word argument dictionary # send request for objects # add version keys and ids to object list # add delete markers to object list # define next key value
list of results with key, size and date, dictionary with key and version id
list of results with key size and date dictionary with key and version id
self
bucket_name
string with name of bucket
string with name of bucket
prefix
[optional] string with value limiting results to key prefix
[ optional ] string with value limiting results to key prefix
delimiter
[optional] string with value limiting results to key delimiter
[ optional ] string with value limiting results to key delimiter
max_results
[optional] integer with max results to return
[ optional ] integer with max results to return
starting_key
[optional] string with key value to continue search with
[ optional ] string with key value to continue search with
starting_version
[optional] string with version id to continue search with
[ optional ] string with version id to continue search with