[英]Amazon s3 SDK for php can not get Bucket object list
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/ 我认为错误是在URL代码调用“主机名/存储桶名称”这里我已经更改了我的存储桶名称https://s3.amazonaws.com/pics.online.com/它应该调用https://pics.online.com。 s3.amazonaws.com/
can you anyone tell me how to change this path for amazon s3 PHP? 你能告诉我如何为亚马逊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 从AWS PHP SDK下载sdk
UPDATE UPDATE
First check you system for AWS Compatibility by using the following files. 首先使用以下文件检查系统的AWS兼容性。
https://github.com/amazonwebservices/aws-sdk-for-php/tree/master/_compatibility_test https://github.com/amazonwebservices/aws-sdk-for-php/tree/master/_compatibility_test
you can use cacert.pem for fetching whole data from AWS S3. 您可以使用cacert.pem从AWS S3获取整个数据。
you can download cacert.pem from here . 你可以从这里下载cacert.pem 。 and modify your php.ini
by adding below code. 并通过添加以下代码修改您的php.ini
。
curl.cainfo = D:\xampp\php\cacert.pem
Here is the code I use on my local machine for fetching data without https. 以下是我在本地计算机上使用的代码,用于在不使用https的情况下获取数据。 I have PHP V5.6 . 我有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. 您可以使用数组格式初始化S3对象。
You can specify the bucket name using the set_hostname method... 您可以使用set_hostname方法指定存储桶名称...
$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. 您不能使用S3Client
为不同的区域调用与对象相关的操作。 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. 如果您正在处理多个区域, S3Client
以为每个区域创建单独的S3Client
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.