Creating Client for Amazon Comprehend with AWS for PHP - php

I recently updated from version 1 of the AWS SDK for PHP to version 3 of the AWS SDK so that I could start testing scripts using the Comprehend and Textract applications. I was able to connect through version 3 and utilize S3 using the "new S3Client()" command. There's extensive documentation regarding functions for Comprehend and Textract, but I can't figure out what the similar new client string is for each service. I've tried:
$cc = new comprehendClient();
$cc = new AWSComprehend();
$cc = new createComprehend();
and more and none of these have worked. If anyone can recommend a fix that would be really helpful. Likewise, if there's an online code repository I should look at that would be helpful. I see plenty of code samples for S3, but none for other applications (at least with SDK for PHP).
Thanks!

From the AWS Comprehend PHP documentation provided, a Comprehend client can be instantiated and called like below :
require 'vendor/autoload.php';
use Aws\Exception\AwsException;
use Aws\Comprehend\ComprehendClient;
/**
* 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
*/
$ComprehendClient = \Aws\Comprehend\ComprehendClient::factory(array(
'credentials' => [
'key' => 'AKIAXXXXXX',
'secret' => '+TsIDxxxxxxx',
],
'region' => 'us-east-1',
'version' => 'latest',
));
$result = $ComprehendClient->detectDominantLanguage([
'Text' => "Nakabibili pala ng durian sa U.S. supermarkets kasama ng mga epol. Galing siguro sa Thailand.", // REQUIRED
]);
echo $result;

Related

How to authenticate SpeechClient V1 using keyfile (PHP / Laravel)

I'm trying to authenticate the SpeechClient using 'keyFilePath' and 'projectId' parameters like so:
$speech = new SpeechClient([
'projectId' => 'actualProjectId,
'keyFilePath' => $key_path,
]);
If I use Google\Cloud\Speech\SpeechClient - Everything works fine, but if I use Google\Cloud\Speech\V1\SpeechClient I end up with an error: Could not construct ApplicationDefaultCredentials
I've read Google docs for Setting Up Authentication but still don't understand what am I doing wrong.
I need the V1 (in fact V1p1beta1) for additional features that are not available using the old SpeechClient.
Any ideas?
P.S. Using Laravel as the back-end.
For clients within the V1, etc. namespaces, pass the keyFilePath as credentials.
new SpeechClient([
'credentials' => $key_path
]);

Google Cloud - BigQuery - PHP use proxy

I created a PHP script to retrieve some datas from my Google Cloud Platform account. Below is how I did :
<?php
require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\BigQuery\BigQueryClient;
putenv('GOOGLE_APPLICATION_CREDENTIALS=key.json');
$projectId = 'xxxxx';
$datasetId = 'xxxxxx';
$table = 'xxxxx';
$bigQuery = new BigQueryClient([
'projectId' => $projectId
]);
// etc...
Everything works fine on my local computer (WAMP) but when I migrate my script to my company production environment, there is a problem :
Fatal error: Uncaught exception
'Google\Cloud\Core\Exception\ServiceException' with message 'cURL
error 6: Couldn't resolve host 'www.googleapis.com'
In fact I was excepting this message because every time I use Curl, I need to set our company proxy info :
<?php
curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, true);
curl_setopt($curl, CURLOPT_PROXY, 'xxx.xxx.xxx.xxx');
By the way, i'm 100% sure that googleapis.com is white-listed by our proxies... but how to do it with the BigQueryClient ? I searched in the official documentation, no way to find how to use a proxy.
I would try passing blindly these constructions to one of the connection builder classes, hopefully one picks this up
'restOptions' => [
'proxy', 'xxx.xxx.xxx.xxx'
]
on the other hand if you have a trace log you could see if Guzzle or something else is used. Consider opening an issue tracking at: https://github.com/GoogleCloudPlatform/google-cloud-php/issues
Bigquery uses www.googleapis.com as its endpoint.
The command line instructions have global flags to specify an address, password, port and user name regarding a proxy usage, however, for client libraries you'll need to verify the access with your infrastructure team.
Use php's putenv() to set the proxy before using BigQueryClient.
putenv('HTTPS_PROXY=192.168.1.1:8080');
use Google\Cloud\BigQuery\BigQueryClient;
use GuzzleHttp\Client;
use Psr\Http\Message\RequestInterface;
$guzzleClient = new Client();
$config = [
'projectId' => 'xxx',
'keyFilePath' => 'key_path',
'restOptions' => [
'proxy' => 'xxx.xxx.xxx.xxx:xx'
],
'authHttpHandler' => function (RequestInterface $request, array $options = []) use ($guzzleClient) {
return $guzzleClient->send(
$request,
$options + [
'proxy' => 'xxx.xxx.xxx.xxx:xx'
]
);
}
];
$bigQueryClient = new BigQueryClient($config);
It works for me with php library google/cloud-bigquery version 1.8.0

Can't pass my credentials to AWS PHP SDK

I installed AWS PHP SDK and am trying to use SES. My problem is that it's (apparently) trying to read ~/.aws/credentials no matter what I do. I currently have this code:
$S3_AK = getenv('S3_AK');
$S3_PK = getenv('S3_PK');
$profile = 'default';
$path = '/home/franco/public/site/default.ini';
$provider = CredentialProvider::ini($profile, $path);
$provider = CredentialProvider::memoize($provider);
$client = SesClient::factory(array(
'profile' => 'default',
'region' => 'us-east-1',
'version' => "2010-12-01",
'credentials' => [
'key' => $S3_AK,
'secret' => $S3_PK,
]
));
And am still getting "Cannot read credentials from ~/.aws/credentials" error (after quite a while).
I tried 'credentials' => $provider of course, that was the idea, but as it wasn't working I reverted to hardcoded credentials. I've dumped $S3_AK and $S3_PK and they're fine, I'm actually using them correctly for S3, but there I have Zend's wrapper. I've tried ~/.aws/credentials (no ".ini") to the same result. Both files having 777 permissions.
Curious information: I had to set memory limit to -1 so it would be able to var_dump the exception. The html to the exception is around 200mb.
I'd prefer to use the environment variables, all though the credentials file is fine. I just don't understand why it appears to be trying to read the file even though I've hardcoded the credentials.
EDIT: So a friend showed me this, I removed the profile and also modified the try/catch and noticed the client seems to be created properly, and the error comes from trying to actually send an email.
The trick is just remove 'profile' => 'default' from the factory params, if this is defined we can't use a custom credentials file or environment variables. Is not documented but just works.
I'm using Sns and Sdk v3.
<?php
use Aws\Credentials\CredentialProvider;
$profile = 'sns-reminders';
$path = '../private/credentials';
$provider = CredentialProvider::ini($profile, $path);
$provider = CredentialProvider::memoize($provider);
$sdk = new Aws\Sdk(['credentials' => $provider]);
$sns = $sdk->createSns([
// 'profile' => $profile,
'region' => 'us-east-1',
'version' => 'latest',
]);
This solution will probably only work if you're using version 3 of the SDK. I use something similar to this:
$provider = CredentialsProvider::memoize(CredentialsProvider::ini($profile, $path));
$client = new SesClient([
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => $provider]);
I use this for S3Client, DynamoDbClient, and a few other clients, so I am assuming that the SesClient constructor supports the same arguments.
OK, I managed to fix it.
I couldn't read the credentials file but it wasn't exactly my idea.
What was happening was that the actual client was being created successfully, but the try/catch also had the sendEmail included. This was what was failing.
About creating the client with explicit credentials: If you specify region, it will try and read a credentials file.
About the SendEmail, this is the syntax that worked for me, I'd found another one also in the AWS docs site, and that one failed. It must've been for an older SDK.

create new user IAM with aws php sdk and limit access of one bucket

i am using aws php sdk for creating bucket in S3
i want to create new user IAM using aws php sdk.. .and then i want to save userkey and acceskey. ..
I got the tutorial for limit the access to user,but not get any for creating new user.
is there any way to create new user?
Install AWS SDK - http://docs.aws.amazon.com/aws-sdk-php/guide/latest/installation.html
Create an Iam client however you like by supplying your AWS credentials. The easiest example to demonstrate is putting the credentials in the PHP file directly - See http://docs.aws.amazon.com/aws-sdk-php/guide/latest/credentials.html
Then this example will create a user
<?php
require 'vendor/autoload.php';
$iamClient = \Aws\Iam\IamClient::factory(
[
'credentials' => [
'key' => 'YOUR_ACCESS_KEY',
'secret' => 'YOUR_SECRET_KEY'
]
]
);
$result = $iamClient->createUser(
[
// UserName is required
'UserName' => 'carlton',
// Optional
'Path' => '/packager/dev/'
]
);
4.Check http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.Iam.IamClient.html for all of the methods available on the $iamClient variable we created. A good IDE will provide code completion on $iamClient so you can see available methods that way.

Using AWS SES API

I want to access the AWS SES Webservice to programmatically add new verified Email identities. The API reference does not give the relevant information or at least I can't find it there.
When I try to access the api I get an error due to the missing signature.
https://email.us-east-1.amazonaws.com?AWSAccessKeyId=EXAMPLEKeyId&Action=VerifyEmailIdentity&EmailAddress=someone#somewhere.org&Timestamp=2013-04-27T19:30:00Z&Version=2010-12-01&Signature=
How do I create this signature exactly, for example using php's hash_hmac()?
Do I need to hash the entire parameters using the SES secret key?
Is there a newer version of the SES API than the one documented (2010-12-01)?
You should really go through the documentation (again).
Take a look at the AWS PHP SDK which would help you a lot.
A sample implementation would be something like:
<?php
require 'aws.phar';
use Aws\Common\Enum\Region;
use Aws\Ses\SesClient;
try {
$ses = SesClient::factory(array(
'key' => 'YOUR_KEY',
'secret' => 'YOUR_SECRET',
'region' => Region::US_EAST_1
));
$ses->verifyEmailIdentity( array(
'EmailAddress' => 'the_mail_adress_to_verify#example.com'
));
}
catch( Exception $e )
{
echo $e->getMessage();
}

Categories