AWS S3Client PHP suddenly doesn't work anymore - php

today my web site which usually auto logs to aws s3 by php, can't connect anymore; the code hasn't changed at all.
$s3Client = new S3Client([
'region' => 'eu-west-1',
'version' => 'latest',
'credentials' => [
'key' => $key,
'secret' => $secret,
],
'http' => [
'verify' => '../ssl_crt/cacert.pem'
]
]);
//echo var_dump($s3Client);
//Listing all S3 Bucket
$result = $s3Client->listBuckets();
the code above which has worked fine till yesterday now gets tomeout connection and therefore fails listing buckets.
I have read amazon has changed something in the permissions/policy, but I can't find how this can be related to PHP s3Client... maybe I could just change some setting in AWS S3 control panel, but I can't figure out what...Has anyone experienced the same issue

Related

AWS PHP SDK - S3 failing silently

So I have a block of code to upload an image to an S3 Bucket. It's fairly boilerplate. Works perfectly running from localhost.
I push it to my Ubuntu EC2 server, and the code fails. No error, no exceptions, Debug => true outputs nothing.. Trying to var_dump the $s3Client variable reports nothing.
E_ALL Error reporting is on
PHP 8.1.4
Nginx 1.21.6
Compatability-test.php passes successfully - all required modules enabled.
Code:
<?php
require 'S3/aws-autoloader.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = 'XXXXX';
try {
//Create a S3Client
$s3Client = new S3Client([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2006-03-01',
'signature' => 'v4',
'debug' => true,
'credentials' => [
'key' => XXXXX,
'secret' => XXXXX,
]
]);
$result = $s3Client->putObject([
'Bucket' => $bucket,
'Key' => $fileName,
'SourceFile' => $filePath,
'ACL' => 'public-read'
]);
echo json_encode(array('success' => true, 'imageUrl' => $result->get('ObjectURL')));
} catch (S3Exception $exception) {
echo $exception->getMessage() . "\n";
}
Has anyone else experienced this 'quiet failure' with the AWS PHP-SDK S3?
I solved this, after 3 days. There were several problems, which I managed to solve and inspect by running the code from the PHP CLI.
First things to note:
Credentials must be stored in the /var/www/.aws/credentials file. This is where the S3Client class will look for your S3 credentials. Passing them into the S3 class instantiation does not appear to work.
Running from the CLI, indicated that I was missing the PHP module mbstring. I don't know why Amazon's compatibility-test.php file does not flag this module as missing. Go figure.
Why the script failed silently, I really don't know. Maybe some error suppression somewhere in the S3Client class. I highly recommend running from the PHP-CLI to diagnose any errors with the PHP AWS S3 SDK.
I realize this is a bit late, but I experienced a similar issue recently where specifically I did not want to use the credentials file, whereas your answer notes that the credentials must be stored in the credentials file.
In my experience, what wound up working for me was removing the 'profile' => 'default', in the s3Client creation. So like this ...
//Create a S3Client
$s3Client = new S3Client([
'region' => 'us-east-1',
'version' => '2006-03-01',
'signature' => 'v4',
'debug' => true,
'credentials' => [
'key' => XXXXX,
'secret' => XXXXX,
]
]);
So if you minimally want to use the inline credentials instead of the credentials file, this may help you out a bit.

Elastic Beanstalk not connecting to Amazon DynamoDB

This is a very strange problem. I have just setup my application on Elastic Beanstalk, the connection which I made with Amazon DynamoDB is working fine but when i made a connection through an AJAX call , it is not connecting and also it is not showing any error. Please help me guys
I have tried changing my role from aws-elasticbeanstalk-service-role to aws-elasticbeanstalk-ec2-role. But still no luck!
I am using the below code to make the connection -
$credentials = new \Aws\Credentials\Credentials(AWS_KEY, AWS_SECRET);
$client = new \Aws\DynamoDb\DynamoDbClient([
'region' => 'us-west-2',
'version' => 'latest',
'credentials' => $credentials
]);

How can I set a timeout in SNS publish method?

I'm using the AWS SNS service in my PHP application to send messages to a queue.
If internet connection is down, the application should keep running without writing the messages. But the method that publish messages on the queue has a very long timeout, it makes the application unusable without internet.
I didn't found any place in AWS SDK where I could set a smaller timeout.
Is there some way to do this?
Here is an example of the construct of the snsClient object.
I've edit the params to use http timeout as #Anthony Neace told, I also try to use request.options connect_timeout as I found here http://docs.aws.amazon.com/aws-sdk-php/v2/guide/configuration.html
I've changed the gateway of my PC to simulate when the internet is down and I got the exception [curl] 28: Resolving timed out after 3000 milliseconds https://sns.sa-east-1.amazonaws.com/
use Aws\Sns\SnsClient;
SnsClient::factory([
'version' => '...',
'region' => '...',
'credentials' => [
'key' => '...',
'secret' => '...',
],
'http' => [
'timeout' => 5
],
'request.options' => [
'connect_timeout' => 5
]
]);
You can set a timeout in your client's constructor. This will apply to all requests you make with that client, including SnsClient Publish.
Here's an example from the documentation, modified to use SnsClient:
use Aws\Sns\SnsClient;
// Timeout after 5 seconds.
$client = new SnsClient([
'region' => 'us-west-2',
'version' => 'latest',
'http' => [
'timeout' => 5
]
]);
Further Reading:
AWS SDK for PHP - AWSClient Constructor (SnsClient inherits this constructor)
AWS SDK for PHP - SnsClient
AWS SDK for PHP - Client Configuration Options - Timeout

Amazon SNS not working

I have imported 'aws-autoloader.php' and used AWS-SDK php library
$sns = SnsClient::factory(array('key' => "******",
'secret' => "*******",
'region' => "us-east-1"));
But I cant able to get any responds. I already used this key and secret in AWS S3 but here its not working help me regarding this.

setting up cloudfront with php sdk

I am trying to configure the the amazon cloud front, I have successfully created clouldflayer url and access private s3 bucket from it via console. Now I am trying to do it by php-sdk for that I have tried following code
use Aws\CloudFront\CloudFrontClient;
$cle = new CloudFrontClient([
'version' => 'latest',
'region' => 'us-west-2',
'credentials.ini' => [
'key' => 'credentials\pk-myKey.pem',
'secret' => 'secret',
],
]);
$result = $cle->getCloudFrontOriginAccessIdentity([
'Id' => '****', // REQUIRED
]);
print_r($result);
but I am getting error
Fatal error: Uncaught exception 'Aws\CloudFront\Exception\CloudFrontException' with message 'Error executing "GetCloudFrontOriginAccessIdentity" on "https://cloudfront.amazonaws.com/2015-04-17/origin-access-identity/cloudfront/SDF345G";
AWS HTTP error: Client error: 403 SignatureDoesNotMatch (client): Credential should be scoped to a valid region, not 'us-west-2'. -
and I have tried all reason one by one but its not working
Credential should be scoped to a valid region, not 'us-west-2'.
Unlike most of AWS, CloudFront is not a regional service, it's a global one, configured and managed through us-east-1, regardless of the region where any of the related services (S3, EC2, etc.) are deployed.
'region' => 'us-east-1',

Categories