php 列出 Amazon S3 上特定文件夹中的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18683206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:00:54  来源:igfitidea点击:

List objects in a specific folder on Amazon S3

phpsdkamazon-s3

提问by Dvir Levy

I am trying to get the list of Objectunder a specific folder in my bucket.

我正在尝试获取Object存储桶中特定文件夹下的列表。

I know that to get a list of all of my objects I do:

我知道要获取我所做的所有对象的列表:

    $objects = $client->getIterator('ListObjects', array(
    'Bucket' => $bucket
)); 

I want to get only the objects under the folder my/folder/test. I have tried adding

我只想获取文件夹下的对象my/folder/test。我试过添加

        'key' => "my/folder/test",

And

        'prefix' => "my/folder/test",

But it simply returns all of the objects in my bucket.

但它只是返回我存储桶中的所有对象。

回答by dcro

You need to use Prefixto restrict the search to a specific directory (a common prefix).

您需要使用Prefix将搜索限制在特定目录(公共前缀)。

$objects = $client->getIterator('ListObjects', array(
    "Bucket" => $bucket,
    "Prefix" => "your-folder/"
)); 

回答by William Worley

The answer is above however i figured i would supply a complete working example that can be copied and pasted directly into a php file and ran

答案在上面,但是我想我会提供一个完整的工作示例,可以将其直接复制并粘贴到 php 文件中并运行

use Aws\S3\S3Client;

require_once('PATH_TO_API/aws-autoloader.php');

$s3 = S3Client::factory(array(
    'key'    => 'YOUR_KEY',
    'secret' => 'YOUR_SECRET',
    'region' => 'us-west-2'
));

$bucket = 'YOUR_BUCKET_NAME';

$objects = $s3->getIterator('ListObjects', array(
    "Bucket" => $bucket,
    "Prefix" => 'some_folder/' //must have the trailing forward slash "/"
));

foreach ($objects as $object) {
    echo $object['Key'] . "<br>";
}

回答by Sol

"S3Client::factory is deprecated in SDK 3.x, otherwise the solution is valid" said by RADU

“S3Client::factory 在 SDK 3.x 中被弃用,否则解决方案是有效的” RADU 说

Here is the updated solution to help others who come across this answer:

这是更新的解决方案,以帮助遇到此答案的其他人:

# composer dependencies
require '/vendor/aws-autoloader.php';
//AWS access info  DEFINE command makes your Key and Secret more secure
if (!defined('awsAccessKey')) define('awsAccessKey', 'ACCESS_KEY_HERE');///  <- put in your key instead of ACCESS_KEY_HERE
if (!defined('awsSecretKey')) define('awsSecretKey', 'SECRET_KEY_HERE');///  <- put in your secret instead of SECRET_KEY_HERE


use Aws\S3\S3Client;

$config = [
    's3-access' => [
        'key' => awsAccessKey,
        'secret' => awsSecretKey,
        'bucket' => 'bucket',
        'region' => 'us-east-1', // 'US East (N. Virginia)' is 'us-east-1', research this because if you use the wrong one it won't work!
        'version' => 'latest',
        'acl' => 'public-read',
        'private-acl' => 'private'
    ]
];

# initializing s3
$s3 = Aws\S3\S3Client::factory([
    'credentials' => [
        'key' => $config['s3-access']['key'],
        'secret' => $config['s3-access']['secret']
    ],
    'version' => $config['s3-access']['version'],
    'region' => $config['s3-access']['region']
]);
$bucket = 'bucket';

$objects = $s3->getIterator('ListObjects', array(
    "Bucket" => $bucket,
    "Prefix" => 'filename' //must have the trailing forward slash for folders "folder/" or just type the beginning of a filename "pict" to list all of them like pict1, pict2, etc.
));

foreach ($objects as $object) {
    echo $object['Key'] . "<br>";
}