コード例 #1
0
ファイル: resize.py プロジェクト: vallard/K8sServerless
def thumbnail(event, context):
    print("Event: ", event)
    print("Event Data: ", event['data'])
    bucket = os.path.dirname(event['data']['Key'])
    _, file_extension = os.path.splitext(event['data']['Key'])
    filename = os.path.basename(event['data']['Key'])

    print file_extension.upper()

    if file_extension.upper() not in [".JPEG", ".JPG", ".PNG", ".GIF"]:
        return "Not a picture"

    if event['data'][
            'EventName'] == "s3:ObjectCreated:Put" and bucket == 'uploads':

        tf = tempfile.NamedTemporaryFile(delete=False)
        tf_thumb = tempfile.NamedTemporaryFile(delete=False)

        try:
            client.fget_object(bucket, filename, tf.name)
        except ResponseError as err:
            print("Can't get object from the bucket: ", bucket, filename,
                  tf.name)
            print err

        size = (120, 120)
        img = Image.open(tf.name)
        img.thumbnail(size)
        img.save(tf_thumb.name, "JPEG")

        # puts the thumbnail in a thumbnail bucket
        thumb_name = filename + '.thumb'
        try:
            client.fput_object('thumbs', thumb_name, tf_thumb.name)
        except ResponseError as err:
            print err

    else:
        print "Minio file deletion event"

    return "Thumbnail creation triggered"
コード例 #2
0
ファイル: ocr.py プロジェクト: ngtuna/functions
def handler(context):
    if context['EventType'] == "s3:ObjectCreated:Put":
        tf = tempfile.NamedTemporaryFile(delete=False)
        bucket = context['Key'].split('/')[0]
        filename = context['Key'].split('/')[1]

        # Fetching source file from Minio
        try:
            print('Fetching file')
            client.fget_object(bucket, filename, tf.name)
        except ResponseError as err:
            print('Error fetching file')
            print err

        # OCR text extraction performed by Tika
        print 'Sending file to Tika'
        parsed = parser.from_file(tf.name, 'http://tika-tika-server:80/tika')
        ocrdata = json.dumps(parsed, ensure_ascii=True)

        # MongoDB document insertion
        db = mongo['ocr']
        result = db.processed.insert_one(parsed)
        print 'Document Saved!'
        print('Document proccessed: {0}'.format(result.inserted_id))

        # move OCRd file to done bucket
        try:
            # Copy from input bucket to done bucket
            fullpath = 'input/' + filename
            client.copy_object('done', filename, fullpath)
            # Remove from input bucket
            client.remove_object('input', filename)
        except ResponseError as err:
            print err
    else:
        print "Minio file deletion event"

    return "OCR Finished"
コード例 #3
0
def ocr(context):
    if context['EventType'] == "s3:ObjectCreated:Put" and context['Key'].split(
            '/')[0] == 'foobar':

        tf = tempfile.NamedTemporaryFile(delete=False)
        bucket = context['Key'].split('/')[0]
        filename = context['Key'].split('/')[1]

        try:
            client.fget_object(bucket, filename, tf.name)
        except ResponseError as err:
            print err

        # puts the same file in a OCR bucket
        ocr_name = filename + '.ocr'
        try:
            client.fput_object('ocr', ocr_name, tf.name)
        except ResponseError as err:
            print err

    else:
        print "Minio file deletion event"

    return "OCR called"
コード例 #4
0
def thumbnail(context):
    bucket = os.path.dirname(context['Key'])
    _, file_extension = os.path.splitext(context['Key'])
    filename = os.path.basename(context['Key'])

    print file_extension.upper()

    if file_extension.upper() != ".JPEG":
        return "Not a picture"

    if context['EventType'] == "s3:ObjectCreated:Put" and bucket == 'foobar':

        tf = tempfile.NamedTemporaryFile(delete=False)
        tf_thumb = tempfile.NamedTemporaryFile(delete=False)

        try:
            client.fget_object(bucket, filename, tf.name)
        except ResponseError as err:
            print err

        size = (120, 120)
        img = Image.open(tf.name)
        img.thumbnail(size)
        img.save(tf_thumb.name, "JPEG")

        # puts the thumbnail in a thumbnail bucket
        thumb_name = filename + '.thumb'
        try:
            client.fput_object('thumb', thumb_name, tf_thumb.name)
        except ResponseError as err:
            print err

    else:
        print "Minio file deletion event"

    return "Thumbnail creation triggered"