s3_open Subroutine

public subroutine s3_open(unit, key, mode, iostat)

Open an S3 object for reading or writing.

Opens an S3 object and returns a unit number for subsequent I/O operations. For read mode, the object is downloaded immediately. For write mode, content is buffered in memory until s3_close() is called.

@param[out] unit The allocated unit number (set to -1 on error) @param[in] key The S3 object key to open @param[in] mode Open mode: 'read'/'r' for reading, 'write'/'w' for writing @param[out] iostat Status code: 0 on success, -1 on error

Example

integer :: unit, iostat

! Open for reading
call s3_open(unit, 'data/input.txt', 'read', iostat)
if (iostat == 0) then
    ! Read operations...
    call s3_close(unit, iostat)
end if

Arguments

Type IntentOptional Attributes Name
integer, intent(out) :: unit
character(len=*), intent(in) :: key
character(len=*), intent(in) :: mode
integer, intent(out) :: iostat

Source Code

    subroutine s3_open(unit, key, mode, iostat)
        integer, intent(out) :: unit
        character(len=*), intent(in) :: key
        character(len=*), intent(in) :: mode
        integer, intent(out) :: iostat
        integer :: i
        character(len=:), allocatable :: content

        iostat = 0
        unit = -1

        ! Find available unit
        do i = 1, MAX_FILES
            if (.not. files(i)%is_open) then
                unit = i
                exit
            end if
        end do

        if (unit < 0) then
            iostat = -1
            return
        end if

        files(unit)%key = key
        files(unit)%is_open = .true.
        files(unit)%position = 1

        select case(mode)
        case('read', 'r')
            files(unit)%is_write = .false.
            ! Download the file content
            if (s3_get_object(key, content)) then
                files(unit)%buffer = content
            else
                iostat = -1
                files(unit)%is_open = .false.
            end if

        case('write', 'w')
            files(unit)%is_write = .true.
            files(unit)%buffer = ''

        case default
            iostat = -1
            files(unit)%is_open = .false.
        end select
    end subroutine s3_open