How to move or download twilio recording on amazon s3

How to download recording file using twilio sdk or how to transfer recording file directly from twilio to amazon s3.

+3


source to share


2 answers


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:



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.

+4


source


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?

      

0


source







All Articles