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

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'];

Related

AWS S3 URL is different from the original one

I am uploading files from php to aws s3. I have successfully uploaded the file.
The url it is returning is => https://BUCKETNAME.s3.ap-south-1.amazonaws.com/images1740/1550830121572.jpg
The actual url is => https://s3.ap-south-1.amazonaws.com/BUCKETNAME/images1740/1550830121572.jpg
(bucket name is coming in starting instead at the end of url)
Because of this it is giving me error while loading images => "Specified Key not found"
$source = $source;
$bucket = 'xxxxxxxxxxxxxxxxx';
$keyname = 'images'.$usr_id."/".$name;
// for push
$s3 = S3Client::factory(
array(
'credentials' => array(
'key' => "xxxxxxxxxxxxxx",
'secret' => "xxxxxxxxxxxxxxx"
),
'version' => 'latest',
'region' => 'ap-south-1'
)
);
try {
// Upload data.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $source,
'ServerSideEncryption' => 'AES256',
));
// Print the URL to the object.
print_r($result);
return $result['ObjectURL'] . PHP_EOL;
// print_r($result);
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
Set use_path_style_endpoint to true when initializing the S3 client to have it use the S3 path style endpoint by default when building the object URL. 1
Implementation details has the object URL to be in the path style if the bucket name makes a valid domain name otherwise it fallback to the S3 path style.
You want to keep the later behavior all the time.
$s3 = S3Client::factory(
array(
'credentials' => array(
'key' => "xxxxxxxxxxxxxx",
'secret' => "xxxxxxxxxxxxxxx"
),
'use_path_style_endpoint' => true,
'version' => 'latest',
'region' => 'ap-south-1'
)
);
You can also do as below if you wanted to disable it one-time for the PutObject operation.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $source,
'ServerSideEncryption' => 'AES256',
'#use_path_style_endpoint' => true
));

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));

Deleting a bucket on Amazon S3 AWS SDK for PHP - Version 3?

I see lots of tutorials even on Amazon to get this done. I follow it but for some reason it doesn't work.
I can do the other command below that works great, but deleting a bucket is not working, with no output for errors.
require 'vendor/autoload.php';
$key = 'file.txt'; // filename
$bucket = 'BUCKETNAME';
use Aws\S3\S3Client;
$client = S3Client::factory([
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => [
'key' => 'KEY',
'secret' => 'SECRET'
]
]);
$result = $client->deleteObject(array(
'Bucket' => $bucket,
'Key' => $key
));
This works, but it isn't a delete command (GetObject) :
$cmd = $client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => 'file.txt'
]);
$request = $client->createPresignedRequest($cmd, '+20 minutes');
echo $presignedUrl = (string) $request->getUri();
To delete a bucket:
// Delete the bucket
$client->deleteBucket(array('Bucket' => $bucket));
You are missing the array.
Here:
http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html#cleaning-up

Upload file to AWS S3 using php

I am trying to create a php script which will be able to upload a text file to my ASW S3 bucket.
I have tried the method which is there on AWS site but sadly that ain't proper, I mean it's not end to end.
I have installed the AWS PHP SDK on my instance.
Then I did what's written in the sample code i.e.
<?php
use Aws\S3\S3Client;
$bucket = 'cst';
$keyname = 'sampleUpload';
// $filepath should be absolute path to a file on disk
$filepath = '/var/www/html/po/si/mag/sahara.txt';
// Instantiate the client.
$s3 = S3Client::factory();
// Upload a file.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));
echo $result['ObjectURL'];
?>
Obviously, I haven't added the aws key nor the aws secret key so it won't work. But then nothing is specified in the tutorial either. So am kinda lost.
Secondly, I tried using this code :
It's also not working.
Thirdly, I tried this article.
It's working when am using it with html but I am not really able to create a php only script where I can just specify the file location, and the file get's uploaded to the server.
Any help is highly appreciated. I searched a lot, but couldn't find anything useful.
Just a guess, but did you add your credentials inside your HTML code using hidden inputs? Cause I just had a very quick look at this page: https://aws.amazon.com/articles/1434/ and it seems like you can set your credentials using HTML. And my guess is the class will automatically take care of that.
If my guess is right, you do need to add the credentials to your instance:
// Instantiate the client.
$s3 = S3Client::factory();
like
// Instantiate the client.
$s3 = S3Client::factory(array(
'version' => 'latest',
'region' => 'us-west-2', //add correct region
'credentials' => array(
'key' => <YOUR_AWS_KEY>,
'secret' => <YOUR_AWS_SECRET>
)
));
It probably depends on the version of the sdk you're using, whether you need above mentioned code or this one (notice the missing credentials array):
// Instantiate the client.
$s3 = S3Client::factory(array(
'version' => 'latest',
'region' => 'us-west-2', //add correct region
'key' => <YOUR_AWS_KEY>,
'secret' => <YOUR_AWS_SECRET>
));
EDIT:
Just to show what exactly worked in my case, this is my complete code. The path I executed:
http://myurl.com/index.php?path=./test.txt
code:
require __DIR__ . '/vendor/autoload.php';
use Aws\S3\S3Client;
$bucket = 'sdl-images';
$keyname = '*** Your Object Key ***';
// $filepath should be absolute path to a file on disk
$filepath = $_GET['path'];
// Instantiate the client.
$s3 = S3Client::factory(array(
'version' => 'latest',
'region' => <YOUR_REGION E.G. eu-west-1>,
'credentials' => array(
'key' => <YOUR_AWS_KEY>,
'secret' => <YOUR_AWS_SECRET>
)
));
// Upload a file.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY'
));
echo $result['ObjectURL'];

Upload to AWS S3 failing

I'm trying to upload a file from my Wordpress application to a S3 bucket by Ajax:
Somehow, I don't get an answer and the script fails with a 500 error when applying the 'putObject' method.
app/ajax.php
require_once 's3/start.php'
//wp_die(var_dump($s3)); Seems to be fine
$upload = $s3->putObject([
'Bucket' => $config['s3']['bucket'],
'Key' => 'video,
'Body' => fopen( $_FILES['file']['tmp_name'], 'r' ),
'ACL' => 'public-read',
]);
if ($upload) {
wp_die('Uploaded');
} else {
wp_die('Upload Error');
}
app/s3/start.php
use Aws\S3\S3Client;
require 'aws/aws-autoloader.php';
$config = require('config.php');
$s3 = new S3Client([
'key' => $config['s3']['key'],
'secret' => $config['s3']['secret'],
'region' => $config['s3']['region'],
'version' => 'latest',
]);
app/s3/aws
Latest version of the official AWS SDK for PHP
SOLUTION
The credentials in app/start.php where not assigned correctly when initialising the $s3 object. That's how it must look like
$s3 = S3Client::factory([
'region' => $config['s3']['region'],
'version' => 'latest',
'credentials' => [
'key' => $config['s3']['key'],
'secret' => $config['s3']['secret']
]
]);
If you upload a file, you should use SourceFile instead of Body.
Example code:
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));
More info from here - http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpPHP.html

Categories