PHP connect to aws. Connection refused - php

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";
}

Related

PHP SDK For AWS S3 Using ARN Bucket for Auth

We're using the latest PHP AWS S3 SDK.
We've been given a ARN Bucket name
eg:
arn:aws:s3:::project-s3-eu-west-2-d-app-uploads
Using the PHP Code, we're trying:
<?php
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
$s3 = new S3Client([
'version' => 'latest',
'region' => 'eu-west-2',
'use_arn_region' => TRUE, // or FALSE
]);
$s3->putObject([
'Bucket' => 'arn:aws:s3:::project-s3-eu-west-2-d-app-uploads',
// 'Bucket' => 'project-s3-eu-west-2-d-app-uploads.s3.eu-west-2.amazonaws.com', // this did not work
'Key' => 'random-unique-name.ext',
'Body' => fopen('/local/file/path/name.ext', 'r'),
'ACL' => ACL_PUBLIC_READ,
]);
?>
But - no matter what we try, we get the following error:
Bucket parameter parsed as ARN and failed with: Provided ARN was not a valid S3 access point ARN or S3 Outposts access point ARN.
Have you used an ARN for auth, not a Key/Secret?
Thanks

How to invoke an existing AWS Lambda function from laravel?

I am trying to invoke an existing aws-lambda function from my php code in laravel to get the data and save in my database.
The actual command I want to invoke is the following:
aws lambda invoke --function-name expo2020-metrics --region eu-west-3 response.json
Here is the code I'm using to invoke my function:
use Aws\Lambda\LambdaClient;use Aws\Credentials\Credentials;
$credentials = new Credentials('YOUR_ACCESS_KEY', 'YOUR_SECRET_KEY');
$client = LambdaClient::factory(array(
'credentials' => $credentials,
'region' => 'eu-west-3',
'version' => 'latest'
));
$result = $client->getFunction(array(
'FunctionName' => 'expo2020-metrics',
));
But I'm getting the following response:
{"Message":"User: arn:aws:iam::677537359471:user/customer/expo2020-metrics is not authorized to perform: lambda: GetFunction on resource: arn:aws:lambda:eu-west-3:677537359471:function:expo2020-metrics"}
I'm not sure that I'm using the right code to invoke the function or not. I am using the PHP sdk provided by AWS.
You are calling getFunction which just gives you information about the Lambda function. That is not equivalent to the aws lambda invoke CLI command you are trying to translate into PHP.
You should be calling the invoke() function of the Lambda client.
Here is a sample code on Gist: https://gist.github.com/robinvdvleuten/e7259939267ad3eb1dfdc20a344cc94a.
require_once __DIR__.'/vendor/autoload.php';
use Aws\Lambda\LambdaClient;
$client = LambdaClient::factory([
'version' => 'latest',
'region' => 'eu-west-1',
'credentials' => [
'key' => 'YOUR_AWS_ACCESS_KEY_ID',
'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
]
]);
$result = $client->invoke([
'FunctionName' => 'hello-world',
]);
var_dump($result->get('Payload'));

AWS SDK PHP - Cannot read credentials from /.aws/credentials

There seems to be an error either in the documentation or the SDK itself.
The SDK keeps looking for the standard credentials file while there is none.
require('aws/aws-autoloader.php');
use Aws\Credentials\CredentialProvider;
use Aws\S3\S3Client;
$profile = 'default';
$inipath = '/www/test/.test/credentials.ini';
$provider = CredentialProvider::ini($profile, $inipath);
$provider = CredentialProvider::memoize($provider);
use Aws\Exception\AwsException;
try {
$s3Client = new S3Client([
'profile' => 'default',
'region' => 'eu-central-1',
'version' => '2006-03-01',
'credentials' => $provider,
]);
It fails with this error:
Uncaught Aws\Exception\CredentialsException: Cannot read credentials from /.aws/credentials in /www/test/aws/Aws/Credentials/CredentialProvider.php:394
Does anybody have a clue on how fix this?
I removed 'profile' => 'default', from the s3client and it worked.
It seemed the profiles were defined twice.

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";
}

Amazon SNS to push topic using PHP

I'm trying to understand amazon php sdk for AWS but I really can't use it.
I find some basic classes to create, display and push topic but I doesn't work either.
I just want to find a way (the simplest way) to push a topic from my website.
First and foremost, to get acquainted with Amazon Simple Notification Service (SNS), I recommend to perform all required step manually once via the AWS Management Console as explained in the Getting Started Guide, i.e. Create a Topic, Subscribe to this Topic and Publish a message to it.
Afterwards it should be fairly straight forward to facilitate the sample fragments provided in the documentation of the AWS SDK for PHP running, see e.g. method publish() within class AmazonSNS:
$sns = new AmazonSNS();
// Get topic attributes
$response = $sns->publish(
'arn:aws:sns:us-east-1:9876543210:example-topic',
'This is my very first message to the world!',
array(
'Subject' => 'Hello world!'
)
);
// Success?
var_dump($response->isOK());
For a more complete example you might want to check out the the example provided in Sending Messages Using SNS [...].
If none of this works out, you'll have to provide more details regarding the specific problems you are encountering, as requested by tster already.
Good luck!
As told to you by #SteffenOpel, you should once try to perform all required steps manually once via the AWS Management Console.
Then you may use the AWS SDK for PHP(v3) as below to create the SNS client(or infact any service's client, surely with some changes) and then to create a SNS Topic.
<?php
//assuming that use have downloaded the zip file for php sdk
require 'C:/wamp/www/aws sdk/aws-autoloader.php'; //Change the path according to you
use Aws\Sns\SnsClient;
try{
/*-------------METHOD 1----------------*/
// Create a new Amazon SNS client using AWS v3
//$sns = new Aws\Sns\SnsClient([
$sns = new SnsClient([
'region' => 'us-west-2', //Change according to you
'version' => '2010-03-31', //Change according to you
'credentials' => [
'key' => '<Your root AWS Key',
'secret' => '<Your root AWS Secret>',
],
'scheme' => 'http', //disables SSL certification, there was an error on enabling it
]);
$result = $sns -> createTopic([
'Name' => '<Your Topic>',
]);
/*-------------METHOD 2----------------*/
/*
// Create a new Amazon SNS client using AWS v2
$sns = SnsClient::factory(array(
'region' => 'us-west-2',
'version' => '2010-03-31',
'credentials' => [
'key' => '<Your root AWS Key',
'secret' => '<Your root AWS Secret>',
],
'scheme' => 'http',
));
$result = $sns -> createTopic([
'Name' => '<Your Topic>',
]);
*/
/*-------------METHOD 3----------------*/
/*
// Create a new Amazon SNS client using AWS SDK class
// Use the us-west-2 region and latest version of each client.
$sharedConfig = [
'region' => 'us-west-2',
'version' => '2010-03-31',
'credentials' => [
'key' => '<Your root AWS Key',
'secret' => '<Your root AWS Secret>',
],
//'ssl.certificate_authority' => '/path/to/updated/cacert.pem',
'scheme' => 'http',
];
// Create an SDK class used to share configuration across clients.
$sdk = new Aws\Sdk($sharedConfig);
$sns = $sdk -> createSns();
$result = $sns -> createTopic([
'Name' => '<Your Topic>',
]);
*/
if ($result)
echo "Yes";
else
echo "No";
}
catch(Exception $e){
echo 'Caught Exception: ', $e->getMessage(), "\n";
}
?>
NOTE: This code illustrates creating the client for SNS in three different methods.
You may uncomment and use one according to your need.
Method 1 (version 3) is the best if you're creating a single client, else use Method 3. Method 2 is soon gonna depreciate (as its version 2)
I success to do it by using this classes->
Amazon-SNS-client-for-PHP
Very good, easy to use and working just great.
According to the AWS official docs here, we will have to create a SnsClient and call it's publish method. You can get topic's ARN from AWS console.
$SnSclient = new SnsClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-03-31'
]);
$message = 'This message is sent from a Amazon SNS code sample.';
$topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic';
try {
$result = $SnSclient->publish([
'Message' => $message,
'TopicArn' => $topic,
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}

Categories