使用PHP的AWS s3列出对象键

时间:2018-08-07 00:16:42

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

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

我已经安装了存储桶,并加载了一个测试文件:

Screenshot of my s3 bucket with one test file

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

AWS ListingObjectKeysUsingPHP webpage

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

TextView textView = new TextView(this); 
int maxLength = 25;
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(maxLength);
textView.setFilters(filterArray);

当我执行此PHP脚本时,它仅返回:“检索到的键!”没有列出测试文件。它应该列出文件夹中的文件。

当我添加“ var_dump($ objects);”时(在脚本中添加了注释,因此当我取消注释时),然后它将返回该对象的大量数据,如以下屏幕截图所示:

image for the object var dump

由于某些原因,亚马逊提供的这部分代码无法正常工作:

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

?>

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

不需要密钥和秘密吗?

请帮助

1 个答案:

答案 0 :(得分:0)

您的问题很可能是由于PHP AWS SDK无法从您的环境变量中获取凭据而引起的->凭据或者是从环境变量中获取的,或者您应该像这样直接在代码中指定它们:

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

(用您的凭据替换'<KEY>'和'<SECRET>'。)您可以在下面找到整个脚本。

更多信息可以在这里找到: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. 

希望对您有所帮助。