无法使用 PHP 显示 S3 存储桶

Posted

技术标签:

【中文标题】无法使用 PHP 显示 S3 存储桶【英文标题】:Can't get S3 Buckets to show using PHP 【发布时间】:2021-04-26 14:10:25 【问题描述】:

这是我的代码:

use Aws\S3\S3Client;  
use Aws\Exception\AwsException;

define('AWS_KEY', '****');
define('AWS_SECRET_KEY', '****');

// Instantiate the S3 class and point it at the desired host
$client = S3Client::factory(array(
'region' => 'us-east-1',
'version' => 'latest',
'endpoint' => "https://website.com",
'credentials' => [
    'key' => AWS_KEY,
    'secret' => AWS_SECRET_KEY
],
// Set the S3 class to use objects.dreamhost.com/bucket
// instead of bucket.objects.dreamhost.com
'use_path_style_endpoint' => true
));

$listResponse = $client->listBuckets();
print_r($listResponse);
$buckets = $listResponse['Buckets'];
foreach ($buckets as $bucket) 
    echo $bucket['Name'] . "\t" . $bucket['CreationDate'] . "\n";

这是我得到的回复:

Aws\Result 对象 ( [数据:Aws\Result:private] => 数组 ( [@metadata] => 数组 ( [状态代码] => 200 [有效Uri] => https://website.com/ [标题] => 数组 ( [服务器] => nginx/1.16.1 [日期] => 格林威治标准时间 2021 年 1 月 22 日星期五 04:57:56 [内容类型] => 文本/html;字符集=UTF-8 [传输编码] => 分块 [连接] => 保持活动状态 [x-xss-保护] => 1;模式=块 [x-frame-options] => SAMEORIGIN [x-content-type-options] => nosniff [expect-ct] => 强制执行,max-age=300,report-uri='https://www.website.com' [x-cache] => 旁路 [严格传输安全] => 最大年龄=31536000 )

                [transferStats] => Array
                    (
                        [http] => Array
                            (
                                [0] => Array
                                    (
                                    )

                            )

                    )

            )

    )

[monitoringEvents:Aws\Result:private] => Array
    (
    )

)

我似乎无法显示存储桶(我目前在 Amazon S3 上的存储桶上)。

关于为什么它不会显示的任何建议?先谢谢了。

【问题讨论】:

检查访问密钥策略是否有读或列出bucket的权限? 是的,它似乎可以访问 你到底想做什么? 我只是想列出我的存储桶。 【参考方案1】:

我可以使用以下代码获取存储桶列表。唯一的区别是我在创建 S3 客户端对象时没有使用端点。

<?php
/**
 * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * This file is licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License. A copy of
 * the License is located at
 *
 * http://aws.amazon.com/apache2.0/
 *
 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * ABOUT THIS PHP SAMPLE: This sample is part of the SDK for PHP Developer Guide topic at
 * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/s3-examples-creating-buckets.html
 *
 */
// snippet-start:[s3.php.list_buckets.complete]
// snippet-start:[s3.php.list_buckets.import]

require 'vendor/autoload.php';

use Aws\S3\S3Client;  
use Aws\Exception\AwsException;
// snippet-end:[s3.php.list_buckets.import]


/**
 * List your Amazon S3 buckets.
 *
 * This code expects that you have AWS credentials set up per:
 * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
 */

//Create a S3Client 
// snippet-start:[s3.php.list_buckets.main]

define('AWS_KEY', '*****');
define('AWS_SECRET_KEY', '***************');

// Instantiate the S3 class and point it at the desired host
$s3Client = S3Client::factory(array('credentials' => [
    'key' => AWS_KEY,
    'secret' => AWS_SECRET_KEY
],'region' => 'us-east-1','version' => 'latest'));

//Listing all S3 Bucket
$buckets = $s3Client->listBuckets();
foreach ($buckets['Buckets'] as $bucket) 
    echo $bucket['Name'] . "\n";

【讨论】:

这行得通 - 我不确定有什么不同,但谢谢!

以上是关于无法使用 PHP 显示 S3 存储桶的主要内容,如果未能解决你的问题,请参考以下文章

无法访问S3存储桶

无法在 asp.net mvc 中显示来自 s3 存储桶的上传图像

AWS php sdk 用于即时写入 s3 存储桶

即使存储桶策略允许此 IP 地址,也无法下载 S3 存储桶内容

在 Heroku 上使用 Boto 时,无法连接到名称中带有句点的 S3 存储桶

无法在特定区域中创建s3存储桶