0

I am using Amazon S3 to store photos uploaded by users, using the following code:

        if(isset($_POST['Submit'])){

        $fileName = $_FILES['theFile']['name'];
        $fileTempName = $_FILES['theFile']['tmp_name'];
        $folderName = "{id}/{address}/$fileName";

        //create a new bucket
        $s3->putBucket("bucket_name", S3::ACL_PUBLIC_READ);

        //move the file
        if ($s3->putObjectFile($fileTempName, "bucket_name", $folderName, S3::ACL_PUBLIC_READ)) {
            echo ""; //etc, etc

The {id} and {address} are dynamic variables used to create the appearance of folders when the files are uploaded, based on each user's unique data stored in a mysql database.

The problem I'm having is displaying these files inside each user's password protected site. The code I'm using below pics up ALL user's photos and displays them to ALL users. If {id}/{address}/$fileName converts to 123/figuero/photo.jpg then user 123 should only be able to see 123/figuero/photo.jpg (or any other files with the preface 123/ or 123/xxx/

With the code I'm using below, ALL files inside bucket_name can be seen by ALL users, instead of just the files associated with the dynamic data associated with their photos:

    <ul>
        <?php
            $i = 0;
            $contents = $s3->getBucket("bucket_name");

             foreach($contents as $file)
             {

             $fname = $file['name'];
             $furl = 'http://bucket_name.s3.amazonaws.com/'.$fname;

                 echo "<li><img src='$furl'></li>";
                 {
                     $i = 0; 
                 }
             }
        ?>
    <ul>

In other words, ALL the files inside http://bucket_name.S3.amazonaws.com/ are being pulled up for display by the code, instead of just the photos pertaining to user 123 inside the files prefaced by 123/xxx/

What am I missing? Thanks much in advance.

user1322707
  • 281
  • 1
  • 7
  • 17

1 Answers1

0

I think the best way is to use some sort of prefix on the bucket name for your "directory" type needs. Other than that, you'll need to evaluate the directory name in the file name before echoing.

List all Files in a S3 Directory with Zend Framework

i.e. using bucket name to differentiate:

if(isset($_POST['Submit'])){

$fileName = $_FILES['theFile']['name'];
$fileTempName = $_FILES['theFile']['tmp_name'];
$folderName = "{id}/{address}/$fileName";
$bucketName = "bucket_name" . $folderName;

//create a new bucket
$s3->putBucket($bucketName, S3::ACL_PUBLIC_READ);

//move the file
if ($s3->putObjectFile($fileTempName, $bucketName, $folderName, S3::ACL_PUBLIC_READ)) {
    echo ""; //etc, etc

and

<ul>
    <?php
        $i = 0;
        $contents = $s3->getBucket($bucketName);

         foreach($contents as $file)
         {

         $fname = $file['name'];
         $furl = 'http://bucket_name.s3.amazonaws.com/'.$fname;

             echo "<li><img src='$furl'></li>";
             {
                 $i = 0; 
             }
         }
    ?>
<ul>
Community
  • 1
  • 1
Benjamin Powers
  • 3,186
  • 2
  • 18
  • 23
  • Hmmmm, this creates a bucket by the name `bucket_name{id}.s3.amazonaws.com/{id}/{address}` instead of `bucket_name.s3.amazonaws.com/{id}/{address}` – user1322707 Dec 01 '12 at 00:41
  • Sure... you could name the bucket anything you want really. The idea is that you can get that specific bucket using the S3 api call as opposed to iterating through all files in a bucket and evaluating within the loop. – Benjamin Powers Dec 01 '12 at 00:46
  • It's not the bucket name I'm having a problem with. The dynamic variable `{id}` is getting placed after `bucket_name` and creating a bucket name called `bucket_name{id}` instead of `bucket_name` – user1322707 Dec 01 '12 at 00:49
  • yes, that is the idea. You could make it bucket_name_id or bucket_name_id_address or bucket_name_. You're needing to do this so that you can use this name when you need to retrieve the bucket. – Benjamin Powers Dec 01 '12 at 00:50
  • That would create hundreds, eventually thousands of buckets (because there are hundreds of members), since {id} identifies a specific member folder to place their photos in. I want to create one bucket for all member folders, which are identified by their member id `{id}` – user1322707 Dec 01 '12 at 00:55
  • Thanks Benjamin. But the original question explains how I'm trying to create folders in ONE BUCKET. Folders are identified by member IDs, created on uploading a photo, resulting in `http://bucket_name.s3.amazonaws.com/123/figuero/photo.jpg`. In this example, the folder `/123/` is the result of picking up the member's ID, using the dynamic variable `{id}` – user1322707 Dec 01 '12 at 00:58
  • I understand your conundrum, but there are only so many things that you can do with S3. The alternative is to iterate through the bucket and evaluate the id as you are looping (which I also included in the original answer). Hope that helps. – Benjamin Powers Dec 01 '12 at 01:13
  • Maybe you could list the objects instead? http://stackoverflow.com/questions/4932892/amazon-s3-folders-problem – Benjamin Powers Dec 01 '12 at 01:16
  • The problem is not creating a bucket or even the folders with the existing code in my original question. It works just fine. The problem is in the DISPLAY (which is in the foreach argument). Because Amazon doesn't recognize folders, everything after `.amazonaws.com/` is seen as a file. That's why ALL USERS see ALL PHOTOS from the above code in the foreach command. I need something that's going to allow ONLY User 123 to see ONLY photos (ie files) beginning with `123/xxx` – user1322707 Dec 01 '12 at 01:37
  • It seems as though you have something in your head that you can't see past, so unfortunately the answers available to you are not the one's that you will accept. You either need to reduce the initial set (by using more buckets), or evaluate the name of the file to determine if the user's id that you're looking for is in the filepath (keep your loop, but add an if statement). Good luck to you. – Benjamin Powers Dec 01 '12 at 01:52