Amazon s3 Folders Problem
I need off help regarding amazon s3 with folders,
The problem i get with the Amazon s3 class by undesigned is it doesnt support folders, it will only show you the full file name it gives you these three options out off the array.
[Music/] => Array
(
[name] => Music/
[time] => 1296576896
[size] => 0
[hash] => d41d8cd98f00b204e9800998ecf8427e
)
[Music/Cocaine VIP_Shufunk_192.mp3] => Array
(
[name] => Music/dnb/Cocaine VIP_Shufunk_192.mp3
[time] => 1296577893
[size] => 8300933
[hash] => edfb1bcfad7edfaccd901b95541e8d45
)
[Music/dnb/Crazy Talk_Tantrum Desire_192.mp3] => Array
(
[name] => Music/dnb/Crazy Talk_Tantrum Desire_192.mp3
[time] => 1296577988
[size] => 9266562
[hash] => 0eb4ca6b53d78e1f976df9b488e0f4bf
)
[Music/dnb/Day_N_Nite_(TC_Remix)开发者_JAVA百科_Kid_Cudi_vs._Crookers_192.mp3] => Array
(
[name] => Music/dnb/Day_N_Nite_(TC_Remix)_Kid_Cudi_vs._Crookers_192.mp3
[time] => 1296578094
[size] => 6597705
[hash] => 376ed9479afc9657b40bc4fc3e885b65
)
so as you can see it gives you the options name time size and hash no folders options so i am trying to find a work around.
from above as you can see Cocaine VIP_Shufunk_192.mp3 is in the Music folder and their is also a folder Music/dnb/ which contains lots off files.
What i am looking to do is find a what just to show files that are within a certain folder.
so far ive tried.
ok so if i have a folder called Music
i can have the following.
$amazon_folder = "Music";
$contents = $s3->getBucket("MY-BUCKET",$amazon_folder);
foreach ($contents as $file){
$fname = $file['name'];
// So i need some code
}
ok so this will show all my files within music but the problem with this is it shows everything including folders within the music folder.
I dont want it to show files that are within a folder within the music folder say Music/Dnb i dont want it to show these files only files within the Music folder not the Music/dnb folder???
i have tried the following.
$test = substr($fname, 0, strpos($fname, '/'));
$more = explode("/", $fname);
can anyone think off a solution to this???
Thanks
Amazon S3 is a flat file system. There is no such thing as folders. There are simply really long file names with slashes in them. Most S3 tools will visually display these as folders, but that's not how S3 works under the hood.
I've never used the Undesigned Amazon S3 class before, so I'm not sure about the specifics of that library. I used CloudFusion's S3 class for a long time until Amazon forked it to create the official PHP SDK (which is what I use now).
In the PHP SDK, you can do:
$s3 = new AmazonS3();
$response = $s3->list_objects($bucket, array(
'prefix' => 'Music/dnb/'
));
print_r($response->body);
That will list all objects in your S3 bucket that have a file name (no real folders, remember?) that begins with Music/dnb/
.
$contents = $s3->getBucket("MY-BUCKET", $amazon_folder, null, null, "/");
There is no way around that in the Amazon API. You need to filter your amazon request using the prefix
as you are and then filter out the 'subfolders' on the client.
The best solution actually, is not to try to navigate your S3 storage. Instead you should maintain an 'index' of your files in a proper database (MySql, SimpleDb etc) that you search and query against, and then just retrieve the file from S3 when needed.
So, Here's the work around. To list your directories you do this.
`val objRequest = new ListObjectsRequest()
.withBucketName("MY-BUCKET")
.withPrefix("Music/").withDelimiter("/")
val result = s3Client.listObjects(objRequest)
result.getCommonPrefixes()`
This will list all the folders in MYBUCKET/Music.
This is a away you get aroung with having a flat file store. (This is written in scala)
I don´t know if this response it will be usefull after so long, but here we go:
To resolve this problem you have to do this in java:
List<S3ObjectSummary> files = s3client.listObjects(bucket.getName()).getObjectSummaries();
for (S3ObjectSummary file : files) {
if (file.getKey().endsWith("/"))
System.out.println("----" + file.getKey() + " (ES CARPETA)");
else
System.out.println("----" + file.getKey() + " NO NO NO");
}
With the method "endsWith("/")" you can detect if the S3ObjectSummary is a folder or not.
Hope this can helps someone.
Mike
精彩评论