SDK version 2 updated code

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.1 kB
2
Indexable
public static Map<String, Object> getTranscriptContentFromUrl(String transcriptionUrl) {
    try {
        // Create an S3Client instance
        S3Client s3Client = S3Client.builder()
                                    .region("your-region")
                                    .credentialsProvider(provideCredentials())
                                    .build();

        // Parse the transcription URL
        URI transcriptionUri = new URI(transcriptionUrl);
        String bucketName = transcriptionUri.getAuthority();
        if (Objects.isNull(bucketName)) {
            log.error("Invalid TranscriptionUrl {}", transcriptionUrl);
            return null;
        }
        String objectKey = transcriptionUri.getPath().substring(OBJECT_KEY_STRING_INDEX);

        // Get the S3 object
        GetObjectRequest getObjectRequest = GetObjectRequest.builder()
                                                            .bucket(bucketName)
                                                            .key(objectKey)
                                                            .build();
        ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(getObjectRequest);
        byte[] contentBytes = objectBytes.asByteArray();

        // Write the content to a temporary file
        File tempFile = File.createTempFile("temp", ".json");
        try (FileOutputStream fos = new FileOutputStream(tempFile)) {
            fos.write(contentBytes);
        }

        // Parse the JSON content from the temporary file
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> transcriptContent = objectMapper.readValue(tempFile, new TypeReference<Map<String, Object>>() {});

        // Delete the temporary file
        Files.delete(tempFile.toPath());

        return transcriptContent;
    } catch (IOException | URISyntaxException | S3Exception e) {
        log.error("Exception occurred while retrieving transcript content", e);
        throw new CallRecordingBaseRuntimeException(ERROR_TRANSCRIPTION_URI_SYNTAX_EXCEPTION, false);
    }
}
Leave a Comment