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.
Related
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";
}
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'));
I am trying to delete a message from SQS in AWS using the php SDK. I have the following configuration.
$sqsClient = new SqsClient([
'version' => '2012-11-05',
'region' => 'us-east-1',
'credentials' => [
'key' => KEY,
'secret' => SECRET
]
]);
Then I am trying to delete like the following :
$sqsClient->deleteMessage([
'QueueUrl' => quque-url
'ReceiptHandle' => handle
]);
I am getting the following error on initialization :
Credentials must be an instance of Aws\\Credentials\\CredentialsInterface, an associative array that contains \"key\", \"secret\", and an optional \"token\" key-value pairs, a credentials provider function, or false."
The credentials I am using is correct. Before I was not passing the config and then also the same error was appearing. How this can be fixed ?
I found this code example and I think your code is correct.
How about create instance of Credentials?
You can use these code for initiating instance.
$credentials = new Aws\Credentials\Credentials(KEY, SECRET);
$sqsClient = new SqsClient([
'version' => '2012-11-05',
'region' => 'us-east-1',
'credentials' => $credentials
]);
Check this document.
And for security reason, I recommend that you'd better use credentials file or set environment variable.
I hope all is well.
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";
}
I was using aws/aws-sdk-php-zf2 1.2.* and had to upgrade to 2.0.* and the AWS SDK is v3 now.
Before I called it using the code:
$this->s3 = $serviceLocator->get('aws')->get('s3');
But now its returning this error:
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for aws
Couldn't find the difference in the migration documentation.
Any tips?
Instead of fetching the service by the key: Aws you should now use the FQCN of the SDK class.
use Aws\Sdk as Aws;
$aws = $serviceLocator->get(Aws::class);
See the module.config.php of the aws/aws-sdk-php-zf2 module.
I found an way that worked for me.
It's like that now:
use Aws\S3\S3Client;
$this->s3 = S3Client::factory(array(
'credentials' => array(
'key' => $this->config['aws']['key'],
'secret' => $this->config['aws']['secret'],
),
'region' => $this->config['aws']['region'],
'version' => '2006-03-01'
));