微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用于php的亚马逊s3 SDK无法获取Bucket对象列表

我从一个桶中获取对象列表,但它得到了端点错误.

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/

你能告诉我如何为亚马逊s3 PHP改变这条路径吗?

解决方法:

试试这个

<?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";
 }
?>

AWS PHP SDK下载sdk

UPDATE

首先使用以下文件检查系统的AWS兼容性.

https://github.com/amazonwebservices/aws-sdk-for-php/tree/master/_compatibility_test

您可以使用cacert.pem从AWS S3获取整个数据.

你可以从here下载cacert.pem并通过添加下面的代码修改你的PHP.ini.

curl.cainfo = D:\xampp\PHP\cacert.pem

以下是我在本地计算机上使用的代码,用于在不使用https的情况下获取数据.我有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());

如果您遇到以下格式的错误.您可以使用数组格式初始化S3对象.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐