【问题标题】:AWS s3 Listing Object Keys Using PHPAWS s3 使用 PHP 列出对象键
【发布时间】:2018-08-08 00:55:50
【问题描述】:

我正在使用 AWS s3 使用 PHP 列出对象键。

我已经加载了一个测试文件的存储桶设置:

Screenshot of my s3 bucket with one test file

这是我在 AWS 文档中使用的参考:

AWS ListingObjectKeysUsingPHP webpage

这是我在页面中使用的代码:

<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

$bucket = 'apollos-integrations-public';

// Instantiate the client.
$s3 = new S3Client([
    'version' => 'latest',
    'region'  => 'us-east-1'
]);

// Use the high-level iterators (returns ALL of your objects).
try {
    $objects = $s3->getPaginator('ListObjects', [
        'Bucket' => $bucket
    ]);

        //var_dump($objects);

    echo "Keys retrieved!" . PHP_EOL;
    foreach ($objects as $object) {
        echo $object['Key'] . PHP_EOL;
    }
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

// Use the plain API (returns ONLY up to 1000 of your objects).
try {
    $result = $s3->listObjects([
        'Bucket' => $bucket
    ]);

    echo "Keys retrieved!" . PHP_EOL;
    foreach ($result['Contents'] as $object) {
        echo $object['Key'] . PHP_EOL;
    }
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

?>

当我执行这个 PHP 脚本时,它只返回:“Keys retrieved!”没有列出测试文件。它应该列出文件夹中的文件。

当我添加一个“var_dump($objects);” (在脚本中注释了,所以当我取消注释时),然后它会为对象返回大量数据,如下图所示:

image for the object var dump

由于某种原因,亚马逊提供的这部分代码不起作用:

    foreach ($objects as $object) {
        echo $object['Key'] . PHP_EOL;
    }

为什么 AWS 提供的这段代码不起作用?

不应该需要密钥和秘密吗?

请帮忙

【问题讨论】:

  • 您应该将 var_dump 结果粘贴为文本而不是图片 - 您会注意到此对象内没有 Key 元素。如果您尝试访问http://apollos-integrations-public.s3.amazonaws.com/,您将获得一个 XML 文档,其中列出了文件及其密钥。

标签: php amazon-web-services amazon-s3


【解决方案1】:

您的问题很可能是由于 PHP AWS SDK 无法从您的环境变量中获取凭证——>凭证要么取自环境变量,要么您应该直接在代码中指定它们,如下所示:

// Instantiate the client.
$credentials = new Aws\Credentials\Credentials('<KEY>', '<SECRET>');
$s3 = new S3Client([
    'version' => 'latest',
    'region'  => 'us-east-1',
    'credentials' => $credentials
]);

(将“&lt;KEY&gt;”和“&lt;SECRET&gt;”替换为您的凭据。)。您可以在下面找到整个脚本。

更多信息可以在这里找到:https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_configuration.html

整个脚本(注意前两行,我在其中启用了调试信息。这将显示错误消息以防出现问题 - 就像在这种情况下使用凭据):

<?php
error_reporting(-1);
ini_set('display_errors', 'On');

require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

$bucket = 'apollos-integrations-public';

// Instantiate the client.
$credentials = new Aws\Credentials\Credentials('<KEY>', '<SECRET>');

$s3 = new S3Client([
    'version' => 'latest',
    'region'  => 'us-east-1',
    'credentials' => $credentials
]);

// Use the high-level iterators (returns ALL of your objects).
try {
    $objects = $s3->getPaginator('ListObjects', [
        'Bucket' => $bucket
    ]);

    echo "Keys retrieved!" . PHP_EOL;
    foreach ($objects as $object) {
        echo $object['Key'] . PHP_EOL;
    }
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

// Use the plain API (returns ONLY up to 1000 of your objects).
try {
    $result = $s3->listObjects([
        'Bucket' => $bucket
    ]);

    echo "Keys retrieved!" . PHP_EOL;
    foreach ($result['Contents'] as $object) {
        echo $object['Key'] . PHP_EOL;
    }
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

顺便说一句。这是您在启用调试信息时会看到的错误消息:

Fatal error: Uncaught Aws\Exception\CredentialsException: Error retrieving credentials from the instance profile metadata server. 

希望对您有所帮助。

【讨论】:

  • @ApollosEasy:这个答案有帮助吗?
  • 是的,这确实回答了这个问题。不幸的是,该项目被取消了,很抱歉我没有尽快回复您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 2014-07-01
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
相关资源
最近更新 更多