Amazon ec2 not instantiate with php - php

I am working on amazon ec2. When i integrated the code giving following error
Fatal error: Uncaught exception 'Aws\Exception\CredentialsException' with message 'Error retrieving credentials from the instance profile metadata server. (cURL error 28: Connection timed out after 1996 milliseconds
Here is my code
require __DIR__ . '/aws.phar';
$client = Aws\S3\S3Client::factory([
'key' => '[********]',
'secret' => '[************]',
'region' => '[us-east-1]', // (e.g., us-east-1)
'version' => 'latest'
]);
echo $client->listBuckets();

Key and Secret shouldn't include [ and ]. Can you try:
'key' => '********',
'secret' => '************',
'region' => 'us-east-1',
Passing credentials into a client factory method
// Instantiate the S3 client with your AWS credentials
$s3Client = S3Client::factory(array(
'credentials' => array(
'key' => 'YOUR_AWS_ACCESS_KEY_ID',
'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
)
));

Related

Delete object using v3 ASK PHP SDK

I'm having trouble logging in:
require 'aws-autoloader.php';
$client = new Aws\S3\S3Client([
'version' => 'latest',
'region' => REGION,
'endpoint' => HOST,
'credentials' => [
'key' => AWS_KEY,
'secret' => AWS_SECRET_KEY,
],
]);
$result = $client->deleteObject(array(
'Bucket' => $BucketName,
'Key' => $FileKey,
));
The $client object is never created. If I run it without the credentials it creates the $client. The Amazon docs say this is how to hardcode credentials. The docs have varying examples from previous sdk's which is really confusing. What's the correct way for deleting an object from V3? I'm not using composer if that matters.
UPDATE
The code works but the endpoint value is causing issues. if I use http://bucket.s3.amazonaws.com/ as the host it tries to fetch http://bucket.bucket.s3.amazonaws.com/key and throws a 404 not found, if i change it to http://s3.amazonaws.com it doesn't work at all, if I remove the host it tries fetching http://s3.amazonaws.com/bucket/key which gives a 204 status message and the deletemarker is still false and the file isn't deleted. I dunno how to make deleteObject create the correct effectiveuri in the result. I dunno what the 204 status means.
SOLUTION
The code works fine without the endpoint field. I hadn't put DELETE as allowed in the S3 CORRS. It works now.
require 'aws-autoloader.php';
$client = new Aws\S3\S3Client([
'version' => 'latest',
'region' => REGION,
'credentials' => [
'key' => AWS_KEY,
'secret' => AWS_SECRET_KEY,
],
]);
$result = $client->deleteObject(array(
'Bucket' => $BucketName,
'Key' => $FileKey,
));

AWS S3 cURL timed out

In general, AWS S3 works fine in my web. However, I keep getting randomly these errors when downloading:
Error retrieving credentials from the instance profile metadata server. (cURL error 28: Operation timed out after {>1000} milliseconds with 0 bytes received (see http://curl.haxx.se/libcurl/c/libcurl-errors.html))
Why? How can I prevent these errors from happening?
I am using AWS SDK PHP v3.
if are you using a Credential like below
$s3 = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-east-1',
'key' => "AKIAJAAAXXYASASASASDSUAG66MA",
'secret' => "8sZyAAAAXUSuUK3FJSDFSDS&D*SDSJFSFShjssa7Fx+GS9"
)
]);
then change to like this
$s3 = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => array(
'key' => "AKIAJAAAXXYASASASASDSUAG66MA",
'secret' => "8sZyAAAAXUSuUK3FJSDFSDS&D*SDSJFSFShjssa7Fx+GS9"
)
]);

Cant connect to AWS s3, using aws-php-sdk. Invalid Request

My error message:
Uncaught Aws\S3\Exception\InvalidRequestException:
AWS Error Code: InvalidRequest,
Status Code: 400, AWS Request ID: xxx,
AWS Error Type: client,
AWS Error Message: The authorization mechanism you have provided is not supported.
Please use AWS4-HMAC-SHA256., User-Agent: aws-sdk-php2/2.7.27 Guzzle/3.9.3 curl/7.47.0 PHP/7.0.15-0ubuntu0.16.04.4
My Code:
<?php
use Aws\S3\S3Client;
require 'vendor/autoload.php';
$config = array(
'key' => 'xxx',
'secret' => 'xxx',
'bucket' => 'myBucket'
);
$filepath = '/var/www/html/aws3/test.txt';
//s3
// Instantiate the client.
$s3 = S3Client::factory();
// Upload a file.
$result = $s3->putObject(array(
'Bucket' => $config['bucket'],
'Key' => $config['key'],
'SourceFile' => $filepath,
'Endpoint' => 's3-eu-central-1.amazonaws.com',
'Signature'=> 'v4',
'Region' => 'eu-central-1',
//'ContentType' => 'text/plain',
//'ACL' => 'public-read',
//'StorageClass' => 'REDUCED_REDUNDANCY',
//'Metadata' => array(
// 'param1' => 'value 1',
// 'param2' => 'value 2'
)
);
echo $result['ObjectURL'];
Cant figure out why I cant get through. I have the right parameters in my call. My code is mostly copied from their aws-sdk-php example page. So it shouldnt be to much fault there.
I am using aws cli and have set upp my configure and credentials file under ~/.aws/
EDIT:
Got it to work! This is my code now:
<?php
// Get dependencies
use Aws\S3\S3Client;
require 'vendor/autoload.php';
// File to upload
$filepath = '/var/www/html/aws3/test.txt';
// instantiate the Client
$s3Client = S3Client::factory(array(
'credentials' => array(
'key' => 'AKIAJVKBYTTADILGRTVQ',
'secret' => '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'
),
'region' => 'eu-central-1',
'version' => 'latest',
'signature_version' => 'v4'
));
// Upload a file.
$result = $s3Client->putObject(array(
'Bucket' => "myBucket",//some filebucket name
'Key' => "some_file_name.txt",//name of the object with which it is created on s3
'SourceFile' => $filepath,
)
);
// Echo results
if ($result){
echo $result['ObjectURL'];
} else{
echo "fail";
}
Try this, it will surely work, the problem in your code is you haven't supplied credentials to factory function.
<?php
ini_set('display_errors', 1);
use Aws\S3\S3Client;
require 'vendor/autoload.php';
//here we are creating client object
$s3Client = S3Client::factory(array(
'credentials' => array(
'key' => 'YOUR_AWS_ACCESS_KEY_ID',
'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
)
));
// Upload a file.
$filepath = '/var/www/html/aws3/test.txt';
$result = $s3Client->putObject(array(
'Bucket' => "myBucket",//some filebucket name
'Key' => "some_file_name.txt",//name of the object with which it is created on s3
'SourceFile' => $filepath,
'Endpoint' => 's3-eu-central-1.amazonaws.com',
'Signature' => 'v4',
'Region' => 'eu-central-1',
)
);
echo $result['ObjectURL'];

AWS SDK S3 Delete bucket not working

Trying to to delete a bucket with 2 html files on it, I folow this code.
but I am getting an error.
Fatal error: Class 'ClearBucket' not found in
require '../aws/aws-autoloader.php';
use Aws\S3\S3Client;
$client = new S3Client(array(
'credentials' => array(
'key' => $aws_access_key,
'secret' => $aws_secret_key,
),
'region' => $aws_region,
'version' => 'latest'
));
$clear = new ClearBucket($client, $bucket);
$clear->clear();
// Delete the bucket
$client->deleteBucket(array('Bucket' => $bucket));
// Wait until the bucket is not accessible
$client->waitUntil('BucketNotExists', array('Bucket' => $bucket));

Server error 500 while trying to instantiate an Amazon s3 client

I am getting server error 500 while opening my PHP file in a browser.
This is my config.php file:
<?php
return [
's3' => [
'key' => 'xxx',
'secret' => 'xxxx',
'region' => 'ap-southeast-1',
'bucket' => 'abc.def'
]
];
?>
The start.php file:
<?php
require '../vendor/autoload.php';
use Aws\S3\S3Client;
$config = require('config.php');
$s3 = new S3Client([
'region' => $config['s3Client']['region'],
'version' => 'latest',
'credentials' => [
'key' => $config['s3Client']['key'],
'secret' => $config['s3Client']['secret'],
],
]);
?>
I am getting a 500 error when I try to instantiate an Amazon S3 client. I am new to PHP and AWS. What is wrong with the code?
Please make sure you have the right permissions on the designated files you try to read (owner and group)

Categories