Amazon s3 Folders Problem

0 votes

I need a help regarding amazon s3 with folders,

The problem I get with the Amazon s3 class by undesigned is it doesn't 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 of files.

What I am looking to do is find a what just to show files that are within a certain folder.

so far I've 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 get me a solution to this???

Jul 13, 2018 in AWS by Flying geek
• 3,280 points
513 views

1 answer to this question.

0 votes

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

answered Jul 13, 2018 by Cloud gunner
• 4,670 points

Related Questions In AWS

0 votes
2 answers

Copying folders inside Amazon S3 bucket

Here is the example to copy folder ...READ MORE

answered Nov 29, 2018 in AWS by Archana
• 5,640 points
8,597 views
+4 votes
5 answers

Usage of Amazon Cloudfront or S3

When to use S3? S3 is like many ...READ MORE

answered Apr 3, 2018 in AWS by brat_1
• 7,200 points
1,941 views
+1 vote
4 answers

How do I cache my images which are stored in Amazon S3?

when caching ec2 instance these can be ...READ MORE

answered Oct 23, 2018 in AWS by Nabarupa
9,051 views
0 votes
1 answer

How do I create folder under an Amazon S3 bucket through PHP API?

Of Course, it is possible to create ...READ MORE

answered Apr 24, 2018 in AWS by anonymous
11,370 views
+1 vote
2 answers

AWS CloudWatch Logs in Docker

The awslogs works without using ECS. you need to configure ...READ MORE

answered Sep 7, 2018 in AWS by bug_seeker
• 15,520 points
2,004 views
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,100 points
2,691 views
0 votes
1 answer

Trying to upload data stream in Amazon S3

Try adding the ContentType & ACL in ...READ MORE

answered Jun 28, 2018 in AWS by Cloud gunner
• 4,670 points
2,541 views
+1 vote
3 answers

Which is better ? AWS S3 bucket logs vs AWS cloudtrail

CloudTrail logs API calls accessed to your ...READ MORE

answered Aug 16, 2018 in AWS by Priyaj
• 58,100 points
7,330 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP