Download Recordings Using Ruby

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 Ruby 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
#
# Example script for downloading recording files
#
require 'rubygems'
require 'plivo'
require 'open-uri'
require 'fileutils'

include Plivo
include Plivo::Exceptions

AUTH_ID = "<auth_id>"
AUTH_TOKEN = "<auth_token>"

api = RestClient.new(AUTH_ID,AUTH_TOKEN)

begin
  response = api.recordings.list(
    add_time__gt: "2023-04-01 00:00:00",
    add_time__lt: "2023-04-30 00:00:00",
    limit: 5,
    offset: 0
  )

  puts "Found #{response[:objects].length} recordings."

  response[:objects].each do |recording|
    recording_url = recording.recording_url
    recording_id = recording.recording_id
    format = recording.recording_format
    puts "Downloading recording: #{recording_url}"

    output_file = "recordings/#{recording_id}.#{format}"

    # Directory where the recordings will be saved
    FileUtils.mkdir_p 'recordings'

    begin
      # Download the file
      open(recording_url) do |file|
        File.open(output_file, "wb") do |output|
          output.write(file.read)
        end
      end
      puts "Downloaded file to: #{output_file}"
    rescue StandardError => e
      puts "Error downloading file: #{e.message}"
    end
  end

rescue PlivoRESTError => e
  puts 'Exception: ' + e.message
end

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.