Oracle Export BLOB as Image using PL/SQL

by

Extract images from Oracle BLOB.

Create an Oracle Directory Object: This object maps a logical name within the database to a physical directory on the database server’s file system.

    CREATE OR REPLACE DIRECTORY MY_IMAGES AS '/path/to/your/export/directory';
    GRANT READ, WRITE ON DIRECTORY MY_IMAGES TO YOUR_USER;

Ensure the specified directory exists on the database server and the Oracle database user has write permissions to it.

Write a PL/SQL procedure: Read the BLOB data from your table and write it to files.

    DECLARE
        l_file UTL_FILE.FILE_TYPE;
        l_buffer RAW(32767);
        l_amount BINARY_INTEGER := 32767;
        l_pos NUMBER := 1;
        l_blob BLOB;
        l_blob_len NUMBER;
    BEGIN
        FOR rec IN (SELECT image_id, image_data, image_name FROM your_image_table) LOOP
            l_blob := rec.image_data;
            l_blob_len := DBMS_LOB.GETLENGTH(l_blob);

            l_file := UTL_FILE.FOPEN('MY_IMAGES', rec.image_name || '.jpg', 'wb', 32767); -- Adjust extension as needed

            WHILE l_pos < l_blob_len LOOP
                DBMS_LOB.READ(l_blob, l_amount, l_pos, l_buffer);
                UTL_FILE.PUT_RAW(l_file, l_buffer, TRUE);
                l_pos := l_pos + l_amount;
            END LOOP;

            UTL_FILE.FCLOSE(l_file);
            DBMS_OUTPUT.PUT_LINE('Exported ' || rec.image_name || '.jpg');
            l_pos := 1; -- Reset position for next BLOB
        END LOOP;
    EXCEPTION
        WHEN OTHERS THEN
            IF UTL_FILE.IS_OPEN(l_file) THEN
                UTL_FILE.FCLOSE(l_file);
            END IF;
            RAISE;
    END;
    /

And you and use DBBlobEditor to Export images from Oracle to files.

See also: Oracle BLOB.