我正在从桶中获取对象列表,但它得到端点错误。
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);
}
我正在得到响应。
[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
我认为错误是在URL代码调用"主机名/桶名"在这里我已经更改了我的桶名https://s3.amazonaws.com/pics.online.com/它应该调用https://pics.online.com.s3.amazonaws.com/
谁能告诉我如何改变这个路径为amazon s3 PHP?
try this
<?php
require 'aws-autoloader.php';
$credentials = new AwsCredentialsCredentials('XXXXXXXXXXXXX', 'XXXXXXXXXXXXXX');
$bucket = "";
$s3 = AwsS3S3Client::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";
}
?>
从AWS PHP下载sdk
首先使用以下文件检查您的系统是否兼容AWS。
https://github.com/amazonwebservices/aws-sdk-for-php/tree/master/_compatibility_test可以使用cacert。. pem用于从AWS S3获取整个数据。
可以下载cacert。pem from here。并通过添加以下代码来修改php.ini
。
curl.cainfo = D:xamppphpcacert.pem
以下是我在本地机器上使用的代码,用于在没有https的情况下获取数据。我有PHP V5.6。require 'aws-autoloader.php';
$s3 = AwsS3S3Client::factory(array(
'credentials' => array(
'key' => '************',
'secret' => '*************',
),
'signature' => 'v4',
'version' => 'latest',
'region' => 'ap-southeast-1',
));
print_r($result = $s3->listBuckets());
如果遇到以下格式的错误。您可以使用数组格式初始化S3对象
可以使用set_hostname方法指定桶名…
$Connection->set_hostname($HOST); // Set the hostname
$Connection->allow_hostname_override(false); // Stop the hostname being changed later.
你的代码应该如下所示…
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);
}
我不确定你的桶在哪个区域。我还以为你在不同地区有很多桶呢。您不能在不同的区域使用S3Client
调用对象相关操作。它只适用于特定区域。
<?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 AwsS3S3Client(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";
}
}
如果您在多个区域工作,您可以为每个区域创建单独的S3Client