在 Laravel Storage 和 AWS S3 中使用通配符列出文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40407893/
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
Listing files using wildcards with Laravel Storage and AWS S3
提问by Jonathan Hyams
I am converting a Laravel (5.3) app to use AWS S3 as image storage. I need to programmatically get list of images whose names comply with a specific mask (eg 'FS_1054_*.JPG') which when I used local storage I could do easily with the glob() function.
我正在转换 Laravel (5.3) 应用程序以使用 AWS S3 作为图像存储。我需要以编程方式获取名称符合特定掩码(例如“FS_1054_*.JPG”)的图像列表,当我使用本地存储时,我可以使用 glob() 函数轻松完成。
Any suggestions as to how I could do this with S3?
关于我如何用 S3 做到这一点的任何建议?
回答by pankaj kumar
Suppose If you want to get all files then you can use this
假设如果你想获取所有文件,那么你可以使用这个
Storage::disk('s3')->allFiles('');
It will return All file that have in your bucket. but if you want to look particular then
它将返回您存储桶中的所有文件。但如果你想看起来特别
Storage::disk('s3')->allFiles('FolderName');
or
或者
Storage::disk('s3')->allFiles('FolderName/2FolderName');
have a look of this image. when you want to look all files.
看看这张图片。当您想查看所有文件时。
回答by Jonathan Hyams
I have found an answer, though am happy to know if there is a better one.
我找到了答案,但很高兴知道是否有更好的答案。
Use Storage::files(folder_name)
to list all files in the folder, this returns an array. Then use array_where
and starts_with
to filter the list:
使用Storage::files(folder_name)
列出文件夹中的所有文件,这将返回数组。然后使用array_where
和starts_with
过滤列表:
$files = Storage::files(folder_name);
$files = array_where($files, function ($value, $key) use ($mask) {
return starts_with(basename($value), $mask);
});
回答by Jorge Mu?oz
$storage = Storage::disk('s3');
$client = $storage->getAdapter()->getClient();
$command = $client->getCommand('ListObjects');
$command['Bucket'] = $storage->getAdapter()->getBucket();
$command['Prefix'] = 'path/to/FS_1054_';
$result = $client->execute($command);
foreach ($result['Contents'] as $file) {
//do something with $file['Key']
}
回答by markdwhite
Try creating subfolders based on the filename as the "index" by which you need to search, so you have something like:
尝试根据文件名创建子文件夹作为您需要搜索的“索引”,因此您有类似的内容:
s3://bucket/foo/bar/FS_1054/FS_1054_123.jpg
s3://bucket/foo/bar/FS_1054/FS_1054_124.jpg
s3://bucket/foo/bar/FS_1055/FS_1055_123.jpg
s3://bucket/foo/bar/FS_1055/FS_1055_124.jpg
etc...
So you can get the list using
所以你可以使用
Storage::disk('s3')->files('foo/bar/FS_1054')
Depending on how fine-grained you want to be able to search, you might need more nested folders:
根据您希望能够搜索的细粒度,您可能需要更多嵌套文件夹:
s3://bucket/foo/bar/FS/1054/FS_1054_123.jpg
s3://bucket/foo/bar/FS/1054/FS_1054_124.jpg
s3://bucket/foo/bar/FS/1055/FS_1055_123.jpg
s3://bucket/foo/bar/FS/1055/FS_1055_124.jpg
s3://bucket/foo/bar/FT/1054/FT_1054_123.jpg
s3://bucket/foo/bar/FT/1054/FT_1054_124.jpg
s3://bucket/foo/bar/FT/1055/FT_1055_123.jpg
s3://bucket/foo/bar/FT/1055/FT_1055_124.jpg
etc...
Though this would allow you to:
虽然这将允许您:
Storage::disk('s3')->allFiles('foo/bar/FS')
(note: allFiles() used for recursive listing)
and
和
Storage::disk('s3')->files('foo/bar/FS/1054')