parse_s3_uri Subroutine

private subroutine parse_s3_uri(uri, bucket, key, success)

Parse an s3:// URI into bucket name and object key components.

Parses URIs of the format s3://bucket-name/path/to/object into separate bucket and key strings for use with S3 operations.

@param[in] uri The s3:// URI to parse @param[out] bucket The extracted bucket name (allocatable) @param[out] key The extracted object key/path (allocatable) @param[out] success .true. if URI was successfully parsed, .false. otherwise

Examples

! Parse URI with bucket and key
call parse_s3_uri('s3://my-bucket/data/file.txt', bucket, key, success)
! Result: bucket='my-bucket', key='data/file.txt'

! Parse URI with only bucket
call parse_s3_uri('s3://my-bucket', bucket, key, success)
! Result: bucket='my-bucket', key=''

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: uri
character(len=:), intent(out), allocatable :: bucket
character(len=:), intent(out), allocatable :: key
logical, intent(out) :: success

Source Code

    subroutine parse_s3_uri(uri, bucket, key, success)
        character(len=*), intent(in) :: uri
        character(len=:), allocatable, intent(out) :: bucket
        character(len=:), allocatable, intent(out) :: key
        logical, intent(out) :: success
        integer :: bucket_start, bucket_end, key_start

        success = .false.

        ! Check for s3:// prefix
        if (len(uri) < 6) return
        if (uri(1:5) /= 's3://') return

        ! Find bucket name (between s3:// and next /)
        bucket_start = 6
        bucket_end = index(uri(bucket_start:), '/') + bucket_start - 2

        if (bucket_end < bucket_start) then
            ! No key, just bucket
            bucket = uri(bucket_start:)
            key = ''
            success = .true.
            return
        end if

        ! Extract bucket and key
        bucket = uri(bucket_start:bucket_end)
        key_start = bucket_end + 2

        if (key_start <= len(uri)) then
            key = uri(key_start:)
        else
            key = ''
        end if

        success = .true.
    end subroutine parse_s3_uri