我想在我的存储桶中的特定文件夹下获取Object
列表。
我知道要获取我所有对象的列表:
$objects = $client->getIterator('ListObjects', array(
'Bucket' => $bucket
));
我想只获取文件夹my/folder/test
下的对象。我试过添加
'key' => "my/folder/test",
并且
'prefix' => "my/folder/test",
但它只是返回我桶中的所有对象。
答案 0 :(得分:55)
您需要使用Prefix
将搜索限制在特定目录(公共前缀)。
$objects = $client->getIterator('ListObjects', array(
"Bucket" => $bucket,
"Prefix" => "your-folder/"
));
答案 1 :(得分:24)
答案在上面,但我想我会提供一个完整的工作示例,可以直接复制并粘贴到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>";
}
答案 2 :(得分:0)
RADU说“ SDK 3.x中不推荐使用S3Client :: factory,否则解决方案有效”
这是更新的解决方案,可帮助遇到此问题的其他人
# 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>";
}