How to move or download twilio recording on amazon s3
Twilio the evangelist is here.
The records are displayed via a direct url, so to download them you need to use an HTTP client in your programming language of choice to make a GET request to the record url and save the returned data.
There are two ways to find out the URL:
-
If you provide a URL in a valid
<Record>
verb parameter , Twilio will make an HTTP request to that URL as soon as the entry is complete and includes as a parameter the URL that was stored in the entry. -
Make a GET request to the Twilio REST API Records resource . This will return you a list of recording resources, each with a URI parameter included in it. Add .mp3 or .wav to this URI to get the URL needed to request the recorded audio.
Once you have uploaded the entry, you can use the REST API to have Twilio remove it from our servers. Just issue an HTTP DELETE request on the uri records.
Hope it helps.
source to share
Here's a Ruby script I wrote for this. For the fastest results, run this from the server and copy it to the eastern US bucket where Twilio resides. I just did it from a Heroku app as Heroku is in the East US. Run heroku run bash -a my-app-name
. Install gems:
gem install twilio-ruby aws-sdk --no-ri --no-rdoc
Then start irb and run this code (update 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?
source to share