s3_object_exists Function

public function s3_object_exists(key) result(exists)

Check if an object exists in S3.

Performs an HTTP HEAD request to check if an object exists without downloading it. This is more efficient than attempting a GET request when you only need to verify existence.

@param[in] key The S3 object key to check @return .true. if object exists, .false. if not found or on error

Note

The module must be initialized with s3_init() before calling this function.

Example

logical :: exists

exists = s3_object_exists('data/input.nc')
if (exists) then
    print *, 'File found, proceeding with download'
else
    print *, 'File not found, using defaults'
end if

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: key

Return Value logical


Source Code

    function s3_object_exists(key) result(exists)
        character(len=*), intent(in) :: key
        logical :: exists
        character(len=2048) :: url
        character(len=4096) :: cmd
        integer :: ios

        exists = .false.
        if (.not. initialized) return

        ! Build URL
        if (current_config%use_https) then
            write(url, '(A,A,A,A,A,A)') 'https://', &
                trim(current_config%bucket), '.', &
                trim(current_config%endpoint), '/', &
                trim(key)
        else
            write(url, '(A,A,A,A,A,A)') 'http://', &
                trim(current_config%bucket), '.', &
                trim(current_config%endpoint), '/', &
                trim(key)
        end if

        ! Use curl HEAD request to check existence
        write(cmd, '(A,A,A)') 'curl -s -I "', trim(url), '" | grep "HTTP" | grep -q "200 OK"'

        call execute_command_line(cmd, exitstat=ios)
        exists = (ios == 0)
    end function s3_object_exists