I was getting list of object from a bucket but it getting error of endpoint.
define('AWS_KEY', 'xxxxxx');
define('AWS_SECRET_KEY', 'x+x/xxxxxxxx/');
define('AWS_CANONICAL_ID','xx');
define('AWS_CANONICAL_NAME', 'xxxxx');
$HOST = 's3.amazonaws.com';
require_once 'php_plugins/aws/v1/sdk.class.php';
$Connection = new AmazonS3(array(
'key' => AWS_KEY,
'secret' => AWS_SECRET_KEY,
'canonical_id' => AWS_CANONICAL_ID,
'canonical_name' => AWS_CANONICAL_NAME,
));
$ListResponse = $Connection->list_buckets();
$Buckets = $ListResponse->body->Buckets->Bucket;
foreach ($Buckets as $Bucket) {
echo $Bucket->Name . "\t" . $Bucket->CreationDate . "\n";
$response = $Connection->list_objects($Bucket->Name);
}
I am getting response.
[body] => CFSimpleXML Object
(
[Code] => PermanentRedirect
[Message] => The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
[Bucket] => pics.online.com
[Endpoint] => pics.online.com.s3.amazonaws.com
[RequestId] => 5F102571A54DA3BA
[HostId] => tBBxwxfUbdlV+m1R/Z9BnjLViyjROdzXrhPfc28WHaZYo/1zAwof2C0G5CVpZvkP8oZERTL0CD8=
)
[status] => 301
I think error is in URL code call "host name/bucket name" here I have change my bucketname https://s3.amazonaws.com/pics.online.com/ it should call https://pics.online.com.s3.amazonaws.com/
can you anyone tell me how to change this path for amazon s3 PHP?
try this
<?php
require 'aws-autoloader.php';
$credentials = new Aws\Credentials\Credentials('XXXXXXXXXXXXX', 'XXXXXXXXXXXXXX');
$bucket = "";
$s3 = \Aws\S3\S3Client::factory([
'signature' => 'v4',
'version' => 'latest',
'region' => 'ap-southeast-1',
'credentials' => $credentials,
'http' => [
'verify' => '/home/ubuntu/cacert.pem'
],
'Statement' => [
'Action ' => "*",
],
// 'debug' => [
// 'logfn' => function ($msg) {
// echo $msg . "\n";
// },
// 'stream_size' => 0,
// 'scrub_auth' => true,
// 'http' => true,
// ]
]);
try {
$objects = $s3->getIterator('ListObjects', array(
'Bucket' => $bucket //bucket name
));
echo "Keys retrieved!\n";
foreach ($objects as $object) {
echo $object['Key'] . "\n";
}
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}
?>
download the sdk from AWS PHP SDK
UPDATE
First check you system for AWS Compatibility by using the following files.
https://github.com/amazonwebservices/aws-sdk-for-php/tree/master/_compatibility_test
you can use cacert.pem for fetching whole data from AWS S3.
you can download cacert.pem from here . and modify your php.ini
by adding below code.
curl.cainfo = D:\xampp\php\cacert.pem
Here is the code I use on my local machine for fetching data without https. I have PHP V5.6 .
require 'aws-autoloader.php';
$s3 = \Aws\S3\S3Client::factory(array(
'credentials' => array(
'key' => '************',
'secret' => '*************',
),
'signature' => 'v4',
'version' => 'latest',
'region' => 'ap-southeast-1',
));
print_r($result = $s3->listBuckets());
if you face errors in following format. you can use array format for initializing S3 object.
You can specify the bucket name using the set_hostname method...
$Connection->set_hostname($HOST); // Set the hostname
$Connection->allow_hostname_override(false); // Stop the hostname being changed later.
Your code should then read as follows...
define('AWS_KEY', 'xxxxxx');
define('AWS_SECRET_KEY', 'x+x/xxxxxxxx/');
define('AWS_CANONICAL_ID','xx');
define('AWS_CANONICAL_NAME', 'xxxxx');
$HOST = 's3.amazonaws.com';
require_once 'php_plugins/aws/v1/sdk.class.php';
$Connection = new AmazonS3(array(
'key' => AWS_KEY,
'secret' => AWS_SECRET_KEY,
'canonical_id' => AWS_CANONICAL_ID,
'canonical_name' => AWS_CANONICAL_NAME,
));
$Connection->set_hostname('pics.online.com.s3.amazonaws.com');
$Connection->allow_hostname_override(false);
$ListResponse = $Connection->list_buckets();
$Buckets = $ListResponse->body->Buckets->Bucket;
foreach ($Buckets as $Bucket) {
echo $Bucket->Name . "\t" . $Bucket->CreationDate . "\n";
$response = $Connection->list_objects($Bucket->Name);
}
I'm not sure which region is your buckets in. I thought you have multiple buckets in multiple regions. You can not call object related operation using S3Client
for different region. It's only working for a specific region.
<?php
define('AWS_KEY', 'AKIAXXXXXXXXXXXXXXXX');
define('AWS_SECRET_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define('REGION', 'ap-southeast-1');
require_once 'aws-sdk-php-3.4.0/aws-autoloader.php';
$client = new Aws\S3\S3Client(array(
'credentials' => array(
'key' => AWS_KEY,
'secret' => AWS_SECRET_KEY,
),
'region' => REGION,
'version' => 'latest',
));
$listBuckets = $client->listBuckets();
foreach ($listBuckets['Buckets'] as $bucket) {
// Get bucket location
$location = $client->getBucketLocation(array(
'Bucket' => $bucket['Name']
));
// Print bucket information
echo $bucket['Name'] . "\t" . $bucket['CreationDate'] . "\t" . $location['LocationConstraint'] . "\n";
// Check if the bucket location is in client region
if ($location['LocationConstraint'] == $client->getRegion()) {
$listObjects = $client->listObjects(array(
'Bucket' => $bucket['Name']
));
foreach ($listObjects['Contents'] as $object) {
echo $object['Key'] . "\t" . $object['Size'] . "\n";
}
} else {
echo "--> The bucket is not in " . $client->getRegion() . ". Skipped.\n";
}
}
If you are working on multiple regions, you can create separated S3Client
for each region.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.