get last uploaded directory backup
    :param aws_access_key_id:
    :param aws_secret_access_key:
    :param bucket_name:
    :param s3_folder:
    :param zip_backups_dir:
    :param project:
    :return:
3526
def s3_get_dir_backup(aws_access_key_id, aws_secret_access_key, bucket_name, s3_folder, zip_backups_dir, project):
    

    matches = []
    pat = dir_zip_pat % project
    print('looking for pat "%s" in bucket %s' % (pat, bucket_name))
    bucket = s3_bucket(aws_access_key_id, aws_secret_access_key, bucket_name)
    bucketlist = bucket.list()
    for f in bucketlist:
        
        if re.search(pat + '$', f.name):
            print('%s matches' % f.name)
            bk_date = dt.strptime(f.name.replace(s3_folder + '/', '')[0:19], TIMESTAMP_FORMAT)
            matches.append({
                'key': f,
                'file': f.name,
                'date': bk_date
            })
    if matches:
        last_bk = sorted(matches, key=operator.itemgetter('date'))[0]
        dest = os.path.join(zip_backups_dir, last_bk['file'].replace(s3_folder + '/', ''))
        if not os.path.exists(dest):
            last_bk['key'].get_contents_to_filename(dest)
            print('Downloaded %s to %s' % (f.name, dest))
        else:
            print('Last backup %s already exists' % dest)
    else:
        print('no matches')
491519
def get_last_block(working_dir):
    

    # make this usable even if we haven't explicitly configured virtualchain 
    impl = sys.modules[__name__]
    return BlockstackDB.get_lastblock(impl, working_dir)
1180430
def get_last_fingerprint(fullpath):
    
    record = model.FileFingerprint.get(file_path=fullpath)
    if record:
        return record.fingerprint
    return None
82165
def get_bucket(s3_bucket=None, validate=False):
    """"""
    global S3Connection

    if S3Connection != None:
        settings = oz.settings
        s3_bucket = s3_bucket or settings["s3_bucket"]
        opts = {}
        if settings["s3_host"]:
            opts["host"] = settings["s3_host"]
        if settings["aws_access_key"] and settings["aws_secret_key"]:
            opts["aws_access_key_id"] = settings["aws_access_key"]
            opts["aws_secret_access_key"] = settings["aws_secret_key"]
        return S3Connection(**opts).get_bucket(s3_bucket, validate=validate)
    else:
        raise Exception("S3 not supported in this environment as boto is not installed")
43803
def _get_buckets_cache_filename(bucket, prefix):
    '''
    
    '''

    cache_dir = _get_cache_dir()
    if not os.path.exists(cache_dir):
        os.makedirs(cache_dir)

    return os.path.join(cache_dir, '{0}-{1}-files.cache'.format(bucket, prefix))
352628
def get(cls, bucket, key, upload_id, with_completed=False):
        """"""
        q = cls.query.filter_by(
            upload_id=upload_id,
            bucket_id=as_bucket_id(bucket),
            key=key,
        )
        if not with_completed:
            q = q.filter(cls.completed.is_(False))

        return q.one_or_none()
123758
def _get_file_from_s3(creds, metadata, saltenv, bucket, path,
                      cached_file_path):
    '''
    
    '''

    # check the local cache...
    if os.path.isfile(cached_file_path):
        file_meta = _find_file_meta(metadata, bucket, saltenv, path)
        file_md5 = "".join(list(filter(str.isalnum, file_meta['ETag']))) \
            if file_meta else None

        cached_md5 = salt.utils.hashutils.get_hash(cached_file_path, 'md5')

        # hashes match we have a cache hit
        log.debug('Cached file: path=%s, md5=%s, etag=%s',
                  cached_file_path, cached_md5, file_md5)
        if cached_md5 == file_md5:
            return

    # ... or get the file from S3
    __utils__['s3.query'](
        key=creds.key,
        keyid=creds.keyid,
        kms_keyid=creds.kms_keyid,
        bucket=bucket,
        service_url=creds.service_url,
        path=_quote(path),
        local_file=cached_file_path,
        verify_ssl=creds.verify_ssl,
        location=creds.location,
        path_style=creds.path_style,
        https_enable=creds.https_enable
    )
229892
def archive(self, repo_slug=None, format='zip', prefix=''):
        
        prefix = '%s'.lstrip('/') % prefix
        self._get_files_in_dir(repo_slug=repo_slug, dir='/')
        if self.bitbucket.repo_tree:
            with NamedTemporaryFile(delete=False) as archive:
                with ZipFile(archive, 'w') as zip_archive:
                    for name, file in self.bitbucket.repo_tree.items():
                        with NamedTemporaryFile(delete=False) as temp_file:
                            temp_file.write(file.encode('utf-8'))
                        zip_archive.write(temp_file.name, prefix + name)
            return (True, archive.name)
        return (False, 'Could not archive your project.')
1331580
def new_backup(self, src):
        

        backup_id_file = p.join(self.backup_dir, '.bk_idx')
        backup_num = file_or_default(backup_id_file, 1, int)
        backup_name = str(backup_num) + "_" + os.path.basename(src)
        backup_num += 1

        file_put_contents(backup_id_file, str(backup_num))
        return p.join(self.backup_dir, backup_name)
103664
def last_modified():
    
    files = model.FileFingerprint.select().order_by(
        orm.desc(model.FileFingerprint.file_mtime))
    for file in files:
        return file.file_mtime, file.file_path
    return None, None
675039
def get_credentials(self):
        
        creds_mgr = CredentialsManager(self)
        creds = creds_mgr.get_credentials_from_upload_api()
        return {
            'aws_access_key_id': creds['access_key'],
            'aws_secret_access_key': creds['secret_key'],
            'aws_session_token': creds['token'],
            'expiry_time': creds['expiry_time']
        }
951563
def get_s3_uri(m, file_names, config):
    
    bucket_name = m.group(1)
    path = m.group(2)
    bucket = get_s3_bucket(bucket_name, config)
    result = 0
    if (config.start_date or config.date_range) and ("{year" in path or "{month" in path or "{day" in path):
        for tmp_date in date_generator(config.end_date, config.date_range or ((config.end_date - config.start_date).days + 1)):
            # +1 because we want to include end_date
            for key in bucket.list(prefix=path.format(year=tmp_date.year, month=tmp_date.month, day=tmp_date.day)):
                file_names.append("s3://{}/{}".format(bucket_name, key.name))
                result += key.size
    else:
        for key in bucket.list(prefix=path):
            file_names.append("s3://{}/{}".format(bucket_name, key.name))
            result += key.size
    return result
1017352
def _get_last_dirs(path, num=1):
    
    head, tail = os.path.split(path)
    last_path = str(tail)
    for ii in range(num):
        head, tail = os.path.split(head)
        last_path = os.path.join(tail, last_path)

    last_path = "..." + last_path
    return last_path
465093
def copy_file(aws_access_key_id, aws_secret_access_key, bucket_name, file, s3_folder):
    

    #  Connect to the bucket

    bucket = s3_bucket(aws_access_key_id, aws_secret_access_key, bucket_name)
    key = boto.s3.key.Key(bucket)

    if s3_folder:
        target_name = '%s/%s' % (s3_folder, os.path.basename(file))
    else:
        target_name = os.path.basename(file)

    key.key = target_name
    print('Uploading %s to %s' % (file, target_name))
    key.set_contents_from_filename(file)
    print('Upload %s FINISHED: %s' % (file, dt.now()))
247809
def copy(self, folder):
        
        if self.object_id is None:
            raise RuntimeError('Attempting to move an unsaved Message')

        url = self.build_url(
            self._endpoints.get('copy_message').format(id=self.object_id))

        if isinstance(folder, str):
            folder_id = folder
        else:
            folder_id = getattr(folder, 'folder_id', None)

        if not folder_id:
            raise RuntimeError('Must Provide a valid folder_id')

        data = {self._cc('destinationId'): folder_id}

        response = self.con.post(url, data=data)
        if not response:
            return None

        message = response.json()

        # Everything received from cloud must be passed as self._cloud_data_key
        return self.__class__(parent=self, **{self._cloud_data_key: message})
70427
def zip_and_upload(app_dir, bucket, key, session=None):
    """"""
    if session:
        s3_client = session.client('s3')
    else:
        s3_client = boto3.client('s3')
    transfer = S3Transfer(s3_client)

    filedes, temp_file = tempfile.mkstemp()
    os.close(filedes)
    LOGGER.info("staticsite: archiving app at %s to s3://%s/%s",
                app_dir, bucket, key)
    with zipfile.ZipFile(temp_file, 'w', zipfile.ZIP_DEFLATED) as filehandle:
        with change_dir(app_dir):
            for dirname, _subdirs, files in os.walk('./'):
                if dirname != './':
                    filehandle.write(dirname)
                for filename in files:
                    filehandle.write(os.path.join(dirname, filename))
    transfer.upload_file(temp_file, bucket, key)
    os.remove(temp_file)
376432
def get(self, project_name, updatetime=None, md5sum=None):
        ''''''
        if time.time() - self.last_check_projects > self.CHECK_PROJECTS_INTERVAL:
            self._check_projects()
        if self._need_update(project_name, updatetime, md5sum):
            self._update_project(project_name)
        return self.projects.get(project_name, None)