Reputation: 51
How to download the recording file using twilio sdk or how to move the recording file directly from twilio to amazon s3 .
Upvotes: 5
Views: 4853
Reputation: 2997
Here's a Ruby script I wrote to do this. For fastest results, run this from a server and copy to a bucket in US East, which is where Twilio is. I just did this from a Heroku app, since Heroku is in US East. Run heroku run bash -a my-app-name
. Install the gems:
gem install twilio-ruby aws-sdk --no-ri --no-rdoc
Then fire up irb and run this code (updating your credentials and bucket name).
require 'twilio-ruby'
account_sid = 'your_account_sid'
auth_token = 'your_auth_token'
twilio_rest_client = Twilio::REST::Client.new account_sid, auth_token
require 'aws-sdk'
access_key_id = 'your_access_key_id'
secret_access_key = 'your_secret_access_key'
region = 'us-east-1'
bucket = 'your-bucket-name'
Aws.config.update({
region: region,
credentials: Aws::Credentials.new(access_key_id, secret_access_key)
})
s3 = Aws::S3::Resource.new(region: region)
recordings = twilio_rest_client.account.recordings.list(page_size: 1000)
begin
begin
recordings.each do |recording|
recording.mp3! do |file|
begin
path = recording.mp3.gsub('https://api.twilio.com/', '')
object = s3.bucket(bucket).object(path)
object.put(body: file.body)
rescue Aws::S3::Errors::ServiceError => error
puts error.message
puts recording.mp3
end
end
recording.delete
end
rescue Twilio::REST::RequestError => error
puts error.message
puts recording.mp3
end
recordings = recordings.next_page
end while !recordings.empty?
Upvotes: 0
Reputation: 10366
Twilio evangelist here.
Recordings are exposed via a direct URL, so in order to download them you'll need to use a the HTTP Client in your programming language of choice to make a GET request to the recording URL and save the data that is returned.
There are two ways to find out the URL:
If you specifya URL in the action parameter of the <Record>
verb Twilio will make an HTTP request to that URL once the recording is complete and include as a parameter the URL that the recording has been stored at.
Make a GET request to the recordings resource of the Twilio REST API. This will return to you a list of recording resources, each of which has a URI parameter included in it. Add .mp3 or .wav to that URI to get the URL needed to request the recorded audio.
Once you've downloaded the recording, you can use the REST API to have Twilio delete it from our servers. Just issue an HTTP DELETE request to the recordings uri.
Hope that helps.
Upvotes: 5