示例#1
0
文件: s3_loader.py 项目: sayeghr/aws
def load(context, url, callback):
    """
    Loads image
    :param Context context: Thumbor's context
    :param string url: Path to load
    :param callable callback: Callback method once done
    """
    if _use_http_loader(context, url):
        http_loader.load_sync(context, url, callback, normalize_url_func=_normalize_url)
        return

    bucket, key = _get_bucket_and_key(context, url)

    if _validate_bucket(context, bucket):
        bucket_loader = Bucket(bucket, context.config.get('TC_AWS_REGION'))

        def handle_data(file_key):
            if not file_key or 'Error' in file_key or 'Body' not in file_key:
                logger.warn("ERROR retrieving image from S3 {0}: {1}".format(key, str(file_key)))
                # If we got here, there was a failure. We will return 404 if S3 returned a 404, otherwise 502.
                result = LoaderResult()
                result.successful = False
                if file_key and file_key.get('ResponseMetadata', {}).get('HTTPStatusCode') == 404:
                    result.error = LoaderResult.ERROR_NOT_FOUND
                else:
                    result.error = LoaderResult.ERROR_UPSTREAM
                callback(result)
            else:
                callback(file_key['Body'].read())

        bucket_loader.get(key, callback=handle_data)
    else:
        callback(None)
示例#2
0
def load(context, url, callback):
    """
    Loads image
    :param Context context: Thumbor's context
    :param string url: Path to load
    :param callable callback: Callback method once done
    """
    if _use_http_loader(context, url):
        http_loader.load_sync(context, url, callback, normalize_url_func=http_loader._normalize_url)
        return

    bucket, key = _get_bucket_and_key(context, url)

    if not _validate_bucket(context, bucket):
        result = LoaderResult(successful=False,
                              error=LoaderResult.ERROR_NOT_FOUND)
        callback(result)
        return

    loader = Bucket(bucket, context.config.get('TC_AWS_REGION'), context.config.get('TC_AWS_ENDPOINT'))
    handle_data = HandleDataFunc.as_func(key,
                                         callback=callback,
                                         bucket_loader=loader,
                                         max_retry=context.config.get('TC_AWS_MAX_RETRY'))

    loader.get(key, callback=handle_data)
示例#3
0
文件: s3_loader.py 项目: voxmedia/aws
def load(context, url, callback):
    """
    Loads image
    :param Context context: Thumbor's context
    :param string url: Path to load
    :param callable callback: Callback method once done
    """
    if _use_http_loader(context, url):
        http_loader.load_sync(context, url, callback, normalize_url_func=_normalize_url)
        return

    bucket, key = _get_bucket_and_key(context, url)

    if _validate_bucket(context, bucket):
        bucket_loader = Bucket(bucket, context.config.get('TC_AWS_REGION'))

        def handle_data(file_key):
            if not file_key or 'Error' in file_key:
                logger.warn("ERROR retrieving image from S3 {0}: {1}".format(key, file_key['Error']['Message']))
            else:
                callback(file_key['Body'].read())

        bucket_loader.get(key, callback=handle_data)
    else:
        callback(None)
示例#4
0
def load_sync(context, url, callback):
    if _use_http_loader(context, url):
        http_loader.load_sync(
            context, url, callback, normalize_url_func=_normalize_url)
    else:
        bucket, key = _get_bucket_and_key(context, url)

        if _validate_bucket(context, bucket):
            presigned_url = _generate_presigned_url(context, bucket, key)
            http_loader.load_sync(
                context, presigned_url, callback, normalize_url_func=_normalize_url)
        else:
            callback(None)
示例#5
0
文件: s3_loader.py 项目: Bladrak/aws
def load(context, url, callback):
    enable_http_loader = context.config.get('AWS_ENABLE_HTTP_LOADER', default=False)

    if enable_http_loader and url.startswith('http'):
        return http_loader.load_sync(context, url, callback, normalize_url_func=_normalize_url)

    url = urllib2.unquote(url)

    bucket = context.config.get('S3_LOADER_BUCKET', default=None)

    if not bucket:
        bucket, url = _get_bucket(url)

    if _validate_bucket(context, bucket):
        bucket_loader = Bucket(
            connection=thumbor_aws.connection.get_connection(context),
            name=bucket
        )
        file_key = None
        try:
            file_key = bucket_loader.get_key(url)
        except Exception, e:
            logger.warn("ERROR retrieving image from S3 {0}: {1}".format(url, str(e)))

        if file_key:
            callback(file_key.read())
            return
示例#6
0
def load(context, url, callback):
    """
    Loads image
    :param Context context: Thumbor's context
    :param string url: Path to load
    :param callable callback: Callback method once done
    """
    if _use_http_loader(context, url):
        http_loader.load_sync(context, url, callback, normalize_url_func=http_loader._normalize_url)
    else:
        bucket, key = _get_bucket_and_key(context, url)

        if _validate_bucket(context, bucket):
            def on_url_generated(generated_url):
                http_loader.load_sync(context, generated_url, callback, normalize_url_func=http_loader._normalize_url)

            _generate_presigned_url(context, bucket, key, on_url_generated)
        else:
            callback(None)
示例#7
0
文件: zloader.py 项目: gnprice/zulip
def load(context, url, callback):
    # type: (Context, str, Callable[..., Any]) -> None
    url = urllib.parse.unquote(url)
    url_params = get_url_params(url)
    source_type = url_params.get('source_type')

    if not sign_is_valid(url, context) or source_type not in (
            THUMBOR_S3_TYPE, THUMBOR_LOCAL_FILE_TYPE, THUMBOR_EXTERNAL_TYPE):
        callback(get_not_found_result())
        return

    url = url.rsplit('?', 1)[0]
    if source_type == THUMBOR_S3_TYPE:
        s3_loader.load(context, url, callback)
    elif source_type == THUMBOR_LOCAL_FILE_TYPE:
        file_loader.load(context, url, callback)
    elif source_type == THUMBOR_EXTERNAL_TYPE:
        http_loader.load_sync(
            context,
            url,
            callback,
            normalize_url_func=http_loader._normalize_url)
def load(context, url, callback):
    for loader in context.config.PROXY_LOADER_LOADERS:
        if loader in modules:
            mod = modules[loader]
        else:
            logger.debug('Importing: %s' % loader)
            mod = importlib.import_module(loader)
            modules[loader] = mod

        if mod.should_run(url):
            return mod.load_sync(context, url, callback)

    return http_loader.load_sync(
        context,
        url,
        callback,
        normalize_url_func=_normalize_url
    )
示例#9
0
文件: s3_loader.py 项目: pgr0ss/aws
def load_sync(context, url, callback):
    if _use_http_loader(context, url):
        return http_loader.load_sync(context, url, callback, normalize_url_func=_normalize_url)

    bucket, key = _get_bucket_and_key(context, url)

    if _validate_bucket(context, bucket):
        bucket_loader = Bucket(
            connection=get_connection(context),
            name=bucket
        )
        file_key = None
        try:
            file_key = bucket_loader.get_key(url)
        except Exception, e:
            logger.warn("ERROR retrieving image from S3 {0}: {1}".format(url, str(e)))

        if file_key:
            callback(file_key.read())
            return
示例#10
0
def load(context, url, callback):
    return http_loader.load_sync(context, url, callback, normalize_url_func=_normalize_url)
示例#11
0
 def on_url_generated(generated_url):
     def noop(url):
         return url
     http_loader.load_sync(context, generated_url, callback, normalize_url_func=noop)
示例#12
0
def load(context, url, callback):
    return http_loader.load_sync(context,
                                 url,
                                 callback,
                                 normalize_url_func=_normalize_url)
示例#13
0
 def on_url_generated(generated_url):
     http_loader.load_sync(context,
                           generated_url,
                           callback,
                           normalize_url_func=_normalize_url)
示例#14
0
 def on_url_generated(generated_url):
     http_loader.load_sync(context, generated_url, callback, normalize_url_func=http_loader._normalize_url)