[AWS][DynamoDB] Connection to the DB through the PhP SDK - php

I am new to AWS. I have installed an EC2 server which processes PhP code. I am able to administrate the DB through the Amazon website. I'm trying to access my DynamoDB table with the following code:
use Aws\DynamoDb\DynamoDbClient;
try {
$client = DynamoDbClient::factory(array(
'profile' => 'default', // access ID + secret are in the .aws/credentials file
'region' => Region::EU_WEST_1 // also tried with "eu-west-1"
));
echo "after client instanciation"; // this is not displayed
$response = $client->getItem([
'TableName' => 'Child',
'Key' => [
'ChildID' => 'Nicolas'
]
]);
print_r ($response['Item']);
} catch (Exception $e) {
echo '<p>Exception received : ', $e->getMessage(), "\n</p>";
}
I'm not getting any exception. The child I'm trying to get isn't displayed (I did create it). Also tried with the putItem method but it didn't add anything to the DB.

I think you are missing the data type in the key:
$response = $dynamodb->getItem([
'TableName' => 'Child',
'Key' => [
'ChildID' => [ 'S' => 'Nicolas' ]
]
]);
Now you should get the output, you can refer this Link.

Try using the following code, it allows you to pass public and secret keys through parameters.
$client = new DynamoDbClient([
'version' => 'latest',
'region' => 'ap-northeast-1',
'credentials' => [
'key' => 'A5ITUTLAK7W47NNNNQ',
'secret' => 'DrsEjmEMs4PUPIY5/12a/cpUB7JVVcKLahFz826p'
]
]);
try {
$result = $client->getItem(array(
'ConsistentRead' => true,
'TableName' => 'fruits',
'Key' => array(
'id' => array('S' => '1')
)
));
Replace the keys with yours.
Reference:
https://solutionarchitects.guru/viewtopic.php?f=30&t=27

Related

Upload Object (image file) to Linode Object Storage using PHP (Laravel) with S3Client

What could be the possible solution for this? I'm trying to upload an Object (Image file) to my Linode Object Storage, I have already set up the configuration and credentials but I'm still having trouble doing it. Can someone help me?
Code:
$config = [
'version' => 'latest',
'region' => config('filesystems.disks.s3.region'),
'scheme' => 'http',
'endpoint' => config('filesystems.disks.s3.endpoint'),
'credentials' => [
'key' => config('filesystems.disks.s3.key'),
'secret' => config('filesystems.disks.s3.secret'),
],
'debug' => true
];
$client = new S3Client($config);
return $client->putObject([
'Bucket' => config('filesystems.disks.s3.bucket'),
'Key' => $filename,
'SourceFile' => $file
]);
Here's the exception error:
check me please
Hoping to have some feedback here, thank you!

How to log in to AWS cognito user pool using PHP SDK

I am trying to log in the user to the AWS Cognito user pool using PHP SDK. I am following this tutorial, https://sanderknape.com/2017/02/getting-started-with-aws-cognito/. But I am getting the error.
Here is my code:
$credentials = array(
'key' => env('AWS_IAM_KEY', ''),
'secret' => env('AWS_IAM_SECRET', '')
);
//2014-06-30
$client = CognitoIdentityClient::factory(array('region' => env('AWS_REGION',''), 'version' => 'latest', $credentials));
$result = $client->adminInitiateAuth([
'AuthFlow' => 'ADMIN_NO_SRP_AUTH',
'ClientId' => COGNITO_APP_CLIENT_ID,
'UserPoolId' => COGNITO_USER_POOL_ID,
'AuthParameters' => [
'USERNAME' => "name",
'PASSWORD' => 'password',
],
]);
$accessToken = $result->get('AuthenticationResult')['AccessToken'];
When I run the code, I got this error:
InvalidArgumentException
Operation not found: AdminInitiateAuth
It is saying the AdminInitiiateAuth does not exist. But I am correctly following the tutorial. What is missing in my code?
The problem here is that CognitoIdentityClient does not contain the adminInitiateAuth functionality.
You will need to use the CognitoIdentityProviderClient

AWS MediaLive with PHP SDK not return channel lists

I try to use AWS SDK for PHP with MediaLive service. I just wonder if aws-cli command is work fine
aws medialive list-channels
Below code is return result, but "Channels" not return channel lists. What's wrong?
<?php
require 'vendor/autoload.php';
$client = new Aws\MediaLive\MediaLiveClient ([
'version' => '2017-10-14',
'region' => 'ap-southeast-1',
'debug' => false,
'validate' => true,
'credentials' => [
'key' => '<MYKEY>',
'secret' => '<MYSECRET>'
],
]);
$result = $client->listChannels();
print_r($result);
?>
Thank you.
Below code worked for me.
<?php
require 'vendor/autoload.php';
$MediaLiveClient = new MediaLiveClient([
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => [
'key' => '<MYKEY>',
'secret' => '<MYSECRET>'
]
]);
$ListChannels = $MediaLiveClient->getPaginator('ListChannels');
$ChannelsArray = [];
foreach($ListChannels as $Result){
if (count($Result->get('Channels')) > 0){
foreach($Result->get('Channels') as $key => $value){
array_push($ChannelsArray, $value);
}
}
}
print_r($ChannelsArray);

SenderID using Amazon Web Services PHP SDK

I'm trying to send an SMS through my PHP site using the AWS SDK. I use the code from Sending SMS with Amazon AWS services PHP.
require $_SERVER['DOCUMENT_ROOT'] . '/inc/aws/aws-autoloader.php';
error_reporting(E_ALL);
ini_set("display_errors", 1);
$params = array(
'credentials' => array(
'key' => 'abc',
'secret' => 'abc',
),
'region' => 'eu-west-1', // < your aws from SNS Topic region
'version' => 'latest'
);
$sns = new \Aws\Sns\SnsClient($params);
$args = array(
"SenderID" => "TestSender",
"SMSType" => "Transactional",
"Message" => "Sending SMS with PHP",
"PhoneNumber" => "+87654321"
);
$result = $sns->publish($args);
echo "<pre>";
var_dump($result);
echo "</pre>";
This is not working. I have tested with a lot of different SenderIDs and all messages are received from NOTICE.
However, when I send a message from the AWS console, the message is received with the correct SenderID. So I assume my code is wrong.
I found the solution. Set the args this way. It works!
$args = array(
'MessageAttributes' => [
'AWS.SNS.SMS.SenderID' => [
'DataType' => 'String',
'StringValue' => 'YourSenderName'
]
],
"SMSType" => "Transactional",
"PhoneNumber" => "+87654321",
"Message" => "Hello World!"
);

Deserialize WCF web service request in PHP

I have a WCF web service with a Login operation taking a company name, user name and password as the three parameters. I am trying to create a PHP client app to communicate with this service. No matter what I pass to the Login operation I get the following error:
OperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'Login' and namespace ''. Found node type 'Element' with name 'parameters' and namespace ''
My client app:
<?php
try
{
$client = new SoapClient("https://somewhere.com/DataServiceRxPublic.svc?wsdl");
//$params = array(
// 'parameters' => array(
// 'Param' => array(
// array('Name' => 'loginCompany', 'Value' => 'XXX'),
// array('Name' => 'loginId', 'Value' => 'XXX'),
// array('Name' => 'loginPwd', 'Value' => 'XXX')
//)));
//$params = array(
// 'Login' => array(
// array('Name' => 'loginCompany', 'Value' => 'XXX'),
// array('Name' => 'loginId', 'Value' => 'XXX'),
// array('Name' => 'loginPwd', 'Value' => 'XXX')
//));
//$params = array(
// 'Login' => array(
// 'parameters' => array(
// array('Name' => 'loginCompany', 'Value' => 'XXX'),
// array('Name' => 'loginId', 'Value' => 'XXX'),
// array('Name' => 'loginPwd', 'Value' => 'XXX')
//)));
//$params = array(
// array('Name' => 'loginCompany', 'Value' => 'XXX'),
// array('Name' => 'loginId', 'Value' => 'XXX'),
// array('Name' => 'loginPwd', 'Value' => 'XXX')
//);
$params = array(
'loginCompany' => 'XXX',
'loginId' => 'XXX',
'loginPwd' => 'XXX'
);
$obj->loginCompany = 'XXX';
$obj->loginId = 'XXX';
$obj->loginPwd = 'XXX';
//$result = $client->Login($obj);
//$result = $client->Login($params);
}
catch (Exception $e)
{
print_r($e);
}
}
?>
$params being the different array permutations I've based off several different examples online.
Any help is appreciated.
The error message is telling you that there should have been something named "Login", but was named "parameters".
Go get SoapUI and with your WSDL follow the steps I described here to debug what you are actually sending. If you cannot compare what the service expects vs. what you are sending, this will be way too much trial-and-error.
If you need more external help, we'd need the WSDL resource - without it, nobody knows which request structure is expected.
Turns out I needed to extend the SOAPClient and override the __doRequest() method to replace the mismatched soap headers.
I managed to fix this error by modifying service namespace on server side.
[ServiceContract(Name="Service", Namespace = "https://sample.eu")]
Namespace was empty before.

Categories