Amazon SNS using PHP SDK - createPlatformEndpoint "InvalidArgumentException" exception - php

I'm using latest PHP SDK(V3) for Amazon SNS. I have a problem when I'm trying to create an endpoint for a platform (Registering user devices to a platform). The error says I have an "InvalidArgumentException", but I have double checked with the document and I'm passing correct arguments. Please find below my code.
try {
$credentials = new Credentials($SNS_ACCESS_KEY, $SNS_SECRET_KEY);
$s3Client = new S3Client([
'version' => 'latest',
'region' => 'us-west-2',
'credentials' => $credentials
]);
$SNSEndPointData = $s3Client->createPlatformEndpoint([
'PlatformApplicationArn' => $SNS_APP_ARN,
'Token' => $device_token
]);
}
catch(exception $e) {
print $e->__toString();
}
If anyone can help or point me to right direction it is highly appreciated.

Here is the full answer just in case if anyone interested,
require 'vendor/autoload.php';
use Aws\Credentials\Credentials;
use Aws\Sns\SnsClient;
try {
$credentials = new Credentials($SNS_ACCESS_KEY, $SNS_SECRET_KEY);
$client = new SnsClient([
'version' => 'latest',
'region' => 'us-west-2',
'credentials' => $credentials
]);
$SNSEndPointData = $client->createPlatformEndpoint([
'PlatformApplicationArn' => $SNS_APP_ARN,
'Token' => 'phone token'
]);
print $SNSEndPointData;
}
catch(exception $e) {
$message = $e->getMessage();
print $message;
}

Related

PHP connect to aws. Connection refused

I can't connect to AWS S3 using the official PHP SDK from Amazon.
This code
<?php
....
$s3Client = new S3Client([
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => [
'key' => 'key',
'secret' => 'secret',
],
]);
// Use the S3 client to perform actions, such as listing buckets
$result = $s3Client->listBuckets();
foreach ($result['Buckets'] as $bucket) echo $bucket['Name'] . "\n";
I get this error.
PHP Fatal error: Uncaught exception 'Aws\S3\Exception\S3Exception' with message 'Error executing "ListBuckets" on "https://s3.amazonaws.com/"; AWS HTTP error: Connection refused for URI https://s3.amazonaws.com/'
GuzzleHttp\Exception\ConnectException: Connection refused for URI https://s3.amazonaws.com/ in C:\Users\bgaliev\PhpstormProjects\Cash2u.MigrationUtil\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php:328
Stack trace:
I tried to do same thing using C#, that worked without any errors.
Here is the basic sample code on C#
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
var amazonS3Client = new AmazonS3Client(
new BasicAWSCredentials("key", "secret"),
new AmazonS3Config
{
RegionEndpoint = RegionEndpoint.USEast1
}
);
var listBuckets = await amazonS3Client.ListBucketsAsync(new ListBucketsRequest());
foreach (var listBucketsBucket in listBuckets.Buckets)
{
Console.WriteLine(listBucketsBucket.BucketName);
}
In my experience you need to declare an object of AWS Credentials class first
So for your case, please change to something like:
<?php
....
$credentials = new Aws\Credentials\Credentials('key', 'secret');
$s3Client = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => $credentials
]);
Refer to the PHP S3 example in AWS Github that specifies how to use the S3Client to perform this use case.
Notice the mention about creds in this doc topic:
https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
// snippet-end:[s3.php.list_buckets.import]
/**
* List your Amazon S3 buckets.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
//Create a S3Client
// snippet-start:[s3.php.list_buckets.main]
$s3Client = new S3Client([
'profile' => 'default',
'region' => 'us-west-2',
'version' => '2006-03-01'
]);
//Listing all S3 Bucket
$buckets = $s3Client->listBuckets();
foreach ($buckets['Buckets'] as $bucket) {
echo $bucket['Name'] . "\n";
}

Sending SmS using Aws

I am implementing an SMS sending server, for that I am using AWS. I have already used their documentation to carry out all the configuration and installation. I have also placed the files and SDK on the AWS server to run the application. But when running the application, I have no error return, but the SMS does not arrive either.
My code is:
public function SendAwsSms(){
// Passing Aws\Credentials\AssumeRoleCredentialProvider options directly
$profile = new InstanceProfileProvider();
$ARN = "arnkeyprovidedfromaws";
$sessionName = "nameaccessrandom";
$assumeRoleCredentials = new AssumeRoleCredentialProvider([
'client' => new StsClient([
'region' => 'sa-east-1',
'version' => 'latest',
'credentials' => $profile
]),
'assume_role_params' => [
'RoleArn' => $ARN,
'RoleSessionName' => $sessionName,
],
]);
$provider = CredentialProvider::memoize($assumeRoleCredentials);
$SnSclient = new SnsClient([
'region' => 'sa-east-1',
'version'=> 'latest',
]);
try {
$result = $SnSclient->publish([
"SenderID"=>"Idsender",
"SMSType"=>"Promocional",
"Message"=>"Message test",
"PhoneNumber"=>"+1111111111111",
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
}
When I run the code, no error is displayed, but the SMS does not arrive at the destination.
Can someone show me what's wrong in there? I using PHP, with the CodeIgniter framework, and my system is located in AWS servers.

How Do I Create a Dynamic Keyname Using Php AWS S3 API

Have been testing the latest Php AWS S3 API and I not sure how to dynamically name a keyname when a user uploads a new book, reason being I need to be able to retrieve the uploaded book by XYZ customer. Thanks in advance!
<?php
require 'aws/aws-autoloader.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
//AWS S3 SHITE BELOW
$bucket = 'acmebooks';
//CREATE DYNAMIC KEYNAME??
$keyname = 'RANDOM KEYNAME';
$secret = 'FOOBAR1345';
$credentials = new Aws\Credentials\Credentials($keyname, $secret);
$s3 = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-east-2',
'credentials' => $credentials
]);
try {
// Upload data.
$result = $s3->putObject([
'Bucket' => $bucket,
'Key' => $keyname,
//Body' => 'Hello, world!',
'Body' => 'https://booksrun.com/image-loader/350/https:__images-na.ssl-images-amazon.com_images_I_41sYJq3nAWL.jpg',
'ACL' => 'public-read'
]);
// Print the URL to the object.
echo $result['ObjectURL'] . PHP_EOL;
print_r($result['Body']);
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
?>
Was overlooking and confusing $keyname which is my unique AWS S3 "Key", for the "Key name" of what is PUT into the AWS S3 Bucket.
FIX BELOW
<?php
require 'aws/aws-autoloader.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
//TEST FOR AWS S3
$key_input = 'jaybookcover1';
//AWS S3 SHITE BELOW
$bucket = 'acmebooks';
//UNIQUE AWS S3 KEYNAME??
$keyname = ' my unique AWS S3 key';
$secret = 'FOOBAR1345';
$credentials = new Aws\Credentials\Credentials($keyname, $secret);
$s3 = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-east-2',
'credentials' => $credentials
]);
try {
// Upload data.
$result = $s3->putObject([
'Bucket' => $bucket,
'Key' => $key_input,
//Body' => 'Hello, world!',
'Body' => 'https://booksrun.com/image-loader/350/https:__images-na.ssl-images-amazon.com_images_I_41sYJq3nAWL.jpg',
'ACL' => 'public-read'
]);
// Print the URL to the object.
echo $result['ObjectURL'] . PHP_EOL;
print_r($result['Body']);
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
?>

getting error AWS HTTP error: cURL error 6: when creation bucket using 3.52 php sdk

I am using own cloud storage Rados s3 server and trying to create a bucket using 3.52 php AWS sdk. Following is the code I am running in my console:
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Credentials\CredentialProvider;
$Connection = new S3Client([
'region' => 'us-west-2',
'version' => 'latest',
'endpoint' => 'http://XXX.XX.XX.XXX',
'credentials' => [
'key' => 'xx',
'secret' => 'XX'
],
]);
//create a bucket
$promise =$Connection->createBucket(array('Bucket' => 'pankaj'));
I am getting below fatal error
Fatal error: Uncaught exception 'Aws\S3\Exception\S3Exception' with message 'Error executing "CreateBucket" on "http://pankaj.XXX.XX.XX.XXX/"; AWS HTTP error: cURL error 6: Could not resolve host: pankaj.XXX.XX.XX.XXX; Name or service not known (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)' in /var/www/html/object/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php on line 191
I think it's not accepting your end point which you define.
please use add this key in your client connection
'use_path_style_endpoint' => true
Example :
$s3Client = new S3Client([
'region' => 'us-west-2',
'version' => '2006-03-01',
'use_path_style_endpoint' => true
]);
Remove the endpoint from the client configuration.
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
$BUCKET_NAME='<BUCKET-NAME>';
//Create a S3Client
$s3Client = new S3Client([
'region' => 'us-west-2',
'version' => '2006-03-01'
]);
//Creating S3 Bucket
try {
$result = $s3Client->createBucket([
'Bucket' => $BUCKET_NAME,
]);
}catch (AwsException $e) {
// output error message if fails
echo $e->getMessage();
echo "\n";
}

Laravel AWS PHP SDK - CreatePlatformEndpoint throws 400 Bad Request

I am using "aws/aws-sdk-php-laravel": "~3.0"
When i am trying to register the android device its throwing the error.
Below is the snapshot of it.
I tried with 2 different code formats but both gives the same exception.
$device = 'gcm_id_here';
$sns = App::make('aws')->createClient('sns');
$result = $sns->createPlatformEndpoint(['Token' => 'device_unique_id',
'PlatformApplicationArn' => $device]);
// or
$credentials = new Credentials('key', 'secret');
$client = new SnsClient([
'version' => 'latest',
'region' => 'us-west-2',
'credentials' => $credentials
]);
$SNSEndPointData = $client->createPlatformEndpoint([
'PlatformApplicationArn' => $device,
'Token' => 'device_unique_id'
]);
Please help me solve this issue.

Categories