def get_archive(src_url, key, preferred_region):
    '\n    A generic getter for retrieving an s3 location of an archive where the archive is based off a\n    src_url.\n\n    sub-dir: hg.mozilla.org supports archives of sub directories within a repository. This\n    flexibility allows for creating archives of only a portion of what would normally be an entire\n    repo archive.\n\n    logic flow:\n     If their is already a key within s3, a re-direct link is given for the\n    s3 location. If the key does not exist, download the archive from src url, upload it to s3\n    for each region supported and return all uploaded s3 url locations.\n\n     When the key does not exist, the remaining work will be assigned to a celery background task\n    with a url location returned immediately for obtaining task state updates.\n    '
    buckets = current_app.config['ARCHIVER_S3_BUCKETS']
    random_region = buckets.keys()[randint(0, (len(buckets.keys()) - 1))]
    region = (preferred_region if (preferred_region and (preferred_region in buckets)) else random_region)
    bucket = buckets[region]
    s3 = current_app.aws.connect_to('s3', region)
    session = current_app.db.session('relengapi')
    if (not s3.get_bucket(bucket).get_key(key)):
        task_id = key.replace('/', '_')
        tracker = tables.ArchiverTask.query.filter((tables.ArchiverTask.task_id == task_id)).first()
        if (tracker and (tracker.state in FINISHED_STATES)):
            log.info('Task tracker: {} exists but finished with state: {}'.format(task_id, tracker.state))
            delete_tracker(tracker)
            tracker = None
        if (not tracker):
            log.info('Creating new celery task and task tracker for: {}'.format(task_id))
            task = create_and_upload_archive.apply_async(args=[src_url, key], task_id=task_id)
            if (task and task.id):
                pending_expires_at = (now() + datetime.timedelta(seconds=PENDING_EXPIRES_IN))
                session.add(tables.ArchiverTask(task_id=task.id, s3_key=key, created_at=now(), pending_expires_at=pending_expires_at, src_url=src_url, state='PENDING'))
                session.commit()
            else:
                return ({}, 500)
        return ({}, 202, {'Location': url_for('archiver.task_status', task_id=task.id)})
    log.info('generating GET URL to {}, expires in {}s'.format(key, GET_EXPIRES_IN))
    signed_url = s3.generate_url(method='GET', expires_in=GET_EXPIRES_IN, bucket=bucket, key=key)
    return redirect(signed_url)
A generic getter for retrieving an s3 location of an archive where the archive is based off a
src_url.

sub-dir: hg.mozilla.org supports archives of sub directories within a repository. This
flexibility allows for creating archives of only a portion of what would normally be an entire
repo archive.

logic flow:
 If their is already a key within s3, a re-direct link is given for the
s3 location. If the key does not exist, download the archive from src url, upload it to s3
for each region supported and return all uploaded s3 url locations.

 When the key does not exist, the remaining work will be assigned to a celery background task
with a url location returned immediately for obtaining task state updates.
def get_archive(src_url, key, preferred_region):
    '\n    A generic getter for retrieving an s3 location of an archive where the archive is based off a\n    src_url.\n\n    sub-dir: hg.mozilla.org supports archives of sub directories within a repository. This\n    flexibility allows for creating archives of only a portion of what would normally be an entire\n    repo archive.\n\n    logic flow:\n     If their is already a key within s3, a re-direct link is given for the\n    s3 location. If the key does not exist, download the archive from src url, upload it to s3\n    for each region supported and return all uploaded s3 url locations.\n\n     When the key does not exist, the remaining work will be assigned to a celery background task\n    with a url location returned immediately for obtaining task state updates.\n    '
    buckets = current_app.config['ARCHIVER_S3_BUCKETS']
    random_region = buckets.keys()[randint(0, (len(buckets.keys()) - 1))]
    region = (preferred_region if (preferred_region and (preferred_region in buckets)) else random_region)
    bucket = buckets[region]
    s3 = current_app.aws.connect_to('s3', region)
    session = current_app.db.session('relengapi')
    if (not s3.get_bucket(bucket).get_key(key)):
        task_id = key.replace('/', '_')
        tracker = tables.ArchiverTask.query.filter((tables.ArchiverTask.task_id == task_id)).first()
        if (tracker and (tracker.state in FINISHED_STATES)):
            log.info('Task tracker: {} exists but finished with state: {}'.format(task_id, tracker.state))
            delete_tracker(tracker)
            tracker = None
        if (not tracker):
            log.info('Creating new celery task and task tracker for: {}'.format(task_id))
            task = create_and_upload_archive.apply_async(args=[src_url, key], task_id=task_id)
            if (task and task.id):
                pending_expires_at = (now() + datetime.timedelta(seconds=PENDING_EXPIRES_IN))
                session.add(tables.ArchiverTask(task_id=task.id, s3_key=key, created_at=now(), pending_expires_at=pending_expires_at, src_url=src_url, state='PENDING'))
                session.commit()
            else:
                return ({}, 500)
        return ({}, 202, {'Location': url_for('archiver.task_status', task_id=task.id)})
    log.info('generating GET URL to {}, expires in {}s'.format(key, GET_EXPIRES_IN))
    signed_url = s3.generate_url(method='GET', expires_in=GET_EXPIRES_IN, bucket=bucket, key=key)
    return redirect(signed_url)