Tuesday, 17 October 2017

How to use aws S3 and SQS service


Use AWS S3 and SQS service

To use AWS S3 and SQS service we need to install AWS library, here we are installing library for PHP.
So, first go to your project folder and type below commands from terminal as root user

cd /var/www/html/awstest

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

php composer-setup.php

php -r "unlink('composer-setup.php');"

Now, create composer.json file with below content in it.

nano composer.json

{
"require": {
"aws/aws-sdk-php": "2.*"
}
}

Install aws library with below command

php
composer.phar install

This will create a vendor directory in your project with the required libraries and an autoloader script used to load them for your project.


To push file into aws S3 bucket below is sample code:

<?php
require '/var/www/html/vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = 'bucketname';
$sourceFile = '/var/www/html/4903.csv';
$s3FileName = '49031.csv';
$client = S3Client::factory(array(
'key' => "yourawsaccesskey",
'secret' => "yourawssecretkey"
));
try {
$client->putObject(array(
'Bucket'=> $bucket,
'Key' => $s3FileName,
'SourceFile' => $sourceFile,
'StorageClass' => 'REDUCED_REDUNDANCY',
'ACL' => 'public-read'
));
echo "File uploaded successfully\n";
} catch (S3Exception $e) {
// Catch an S3 specific exception.
echo $e->getMessage();
echo "\n";
}
?>


To send message into queue use below code:

<?php
require '/var/www/html/awstest/vendor/autoload.php';
use Aws\Sqs\SqsClient;
use Aws\Common\Credentials\Credentials;
$credentials = new Credentials('AKIAJYJU67ADHXI7ILKA', 'wOIi+6jP7wTUU8bfM8ywtijP1ShjNQiuXsyx4Ugo');
// Instantiate the SQS client with your AWS credentials
$client = SqsClient::factory(array(
'credentials' => $credentials,
'region' => 'us-west-2'
));
$result = $client->createQueue(array('QueueName' => 'testque'));
$queueUrl = $result->get('QueueUrl');
//echo "SQS queue url: $queueUrl";
$client->sendMessage(array(
'QueueUrl' => $queueUrl,
'MessageBody' => '{"TransID":"707"}',
));
?>



To receive message from queue use below code

<?php
require '/var/www/html/awstest/vendor/autoload.php';
use Aws\Sqs\SqsClient;
use Aws\Common\Credentials\Credentials;
$credentials = new Credentials('AKIAJYJU67ADHXI7ILKA', 'wOIi+6jP7wTUU8bfM8ywtijP1ShjNQiuXsyx4Ugo');
// Instantiate the SQS client with your AWS credentials
$client = SqsClient::factory(array(
'credentials' => $credentials,
'region' => 'us-west-2'
));
$result = $client->createQueue(array('QueueName' => 'testque'));
$queueUrl = $result->get('QueueUrl');
$data = $client->receiveMessage(array(
'QueueUrl' => $queueUrl
//'MaxNumberOfMessages' => 10
));
if(!empty($data['Messages'])){
$msg = $data['Messages'][0]['Body'];
$msg_receipt_handle = $data['Messages'][0]['ReceiptHandle'];
$obj = json_decode($msg);
echo $obj->{'TransID'}."\n";
$client->deleteMessage(array(
'QueueUrl' => $queueUrl,
'ReceiptHandle' => $msg_receipt_handle
));
}
else{
$no_msg['Message'] = "No Message";
$no_msg_res = json_encode($no_msg);
echo $no_msg_res."\n";
}
?>

Wednesday, 4 October 2017

Telnyx SMS profile setup


Telnyx SMS profile configuration


To enable SMS configuration in telnyx you need to make Messaging Profile from telnyx portal.
Once you logged in telnyx portal click on Messaging as shown below in image.

Click on Add Messaging Profile as shown in below

Now fill data as below and create messaging profile, give name of messaging profile and assign connection to it either existing or new.



Now edit this profile, you will have Inbound and Outbound tabs settings, in Inbound tab you can set your publically accessible web url to receive incoming SMS


In Outbound tab you will have messaging profile secret key as shown below, which we can use in sending SMS api request.

Save all settings, once you modified it correctly. Now you can use SMS service