Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)_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

like image 815
iSimpleDesign Avatar asked Dec 21 '22 18:12

iSimpleDesign


2 Answers

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/.

like image 166
Skyler Johnson Avatar answered Jan 09 '23 06:01

Skyler Johnson


$contents = $s3->getBucket("MY-BUCKET", $amazon_folder, null, null, "/");
like image 21
Chris Avatar answered Jan 09 '23 05:01

Chris