Download Recordings Using Java

Overview

This guide shows how to retrieve recordings and download them to local storage. Plivo begins charging for stored recordings after 90 days. To avoid these charges, you can download recordings and store them elsewhere.

Prerequisites

To use Plivo APIs, follow our instructions to set up a Java development environment and a web server and safely expose that server to the internet.

Download recordings to local storage

Here’s sample code you can use to retrieve recordings to a local directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.plivo.examples;

import com.plivo.api.Plivo;
import com.plivo.api.exceptions.PlivoRestException;
import com.plivo.api.exceptions.PlivoValidationException;
import com.plivo.api.models.base.ListResponse;
import com.plivo.api.models.recording.Recording;
import com.plivo.api.util.PropertyFilter;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Example script for downloading recording files
 */
public class DownloadRecordings {

  private static final String AUTH_ID = "<auth_id>";
  private static final String AUTH_TOKEN = "<auth_token>";

  public static void main(String[] args) {
    Plivo.init(AUTH_ID, AUTH_TOKEN);
    try {
      String greaterThan = "2023-04-01 00:00:00";
      String lessThan = "2023-04-30 00:00:00";
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Date greaterThanDate = formatter.parse(greaterThan);
      Date lessThanDate = formatter.parse(lessThan);

      ListResponse<Recording> response = Recording.lister().addTime(new PropertyFilter<Date>().greaterThan(greaterThanDate).lessThan(lessThanDate)).offset(0).limit(5).list();

      System.out.println("Found " + response.getObjects().size() + " recordings.");

      for (Recording recording : response.getObjects()) {
        String recordingURL = recording.getRecordingUrl();
        String recordingId = recording.getRecordingId();
        String format = recording.getRecordingFormat();

        System.out.println("Downloading recording: " + recordingURL);

        // Directory where the recordings will be saved
        File outputFile = new File("recordings/" + recordingId + "." + format);
        outputFile.getParentFile().mkdirs();

        try {
          // Download the file
          FileUtils.copyURLToFile(new URL(recordingURL), outputFile);
          System.out.println("Downloaded file to: " + outputFile.getPath());
        } catch (IOException e) {
          System.out.println("Error downloading file: " + e.getMessage());
        }
      }
    } catch (PlivoRestException | IOException e) {
      e.printStackTrace();
    } catch (PlivoValidationException e) {
      throw new RuntimeException(e);
    } catch (ParseException e) {
      throw new RuntimeException(e);
    }
  }
}

Delete recordings from Plivo storage

You can delete a recording by using the Delete a Recording API and specifying a recording ID, which you can retrieve from list all recordings API or the HTTP callback details stored in your database. You can also delete recordings from the Voice Recordings page of the Plivo console.