Checking if a object exists on amazon s3 using AWS api

11,572

Solution 1

You may want to give a little more thought to the whole "flat file system". When I first started writing PHP to interface with the S3 API I was expecting to be able to iterate over a directory, and the files in each folder. That's not how it works though!

This is from Ryan Parman, one of the responses in a question you referenced:

"S3 is a flat file system. There are no folders. There are simply filenames with slashes in them. Some S3 browsing tools choose to display the concept of "folders" in their software, but they're just pretend.

"/albums/Carcassonne-France/" returns false because there is not a singular object with that name."

If you have objects with the following paths:

s3://myBucket/myFolder/taco.jpg

s3://myBucket/myFolder/jambox.jpg

If you check for existence of the object s3://myBucket/myFolder/ it will return false since there is no object with that specific name.

It might be helpful to know more about why you want to create the folder if it doesn't exist, but if you want to place an object into S3 in a specific "folder", you can just use the putObject() method.

Solution 2

@Dan is correct. However, it might be worthwhile to check out the S3 StreamWrapper, which enables you to use PHP's built-in file system functions with S3.

Solution 3

I acheived the same thing using below a simple hack. we have to include a file in each of the folder and can check for that file so if that file exists that means folder path is valid. In my case the file I took was donotdelete.txt so that any s3 viewer can understand that this file should not be deleted.Now check for this file using getObjectInfo function. For Example:

    if($s3->getObjectInfo("bucketName","folder_name/donotdelete.txt")))
    { 
        //Do Whatever you want to do if folder exists
    }
    else
    {
        //Do whatever you want to do if folder doesn't exist
    }
Share:
11,572
KillABug
Author by

KillABug

Updated on June 08, 2022

Comments

  • KillABug
    KillABug almost 2 years

    I am working on the AWS api and having an issue with the check for existing objects(FOLDERS).
    I went through this question and it does not help me because I am using the latest updated SDK.
    I searched the SDK and found this which should work i.e. doesObjectExist,but I am not able to find the function definition anywhere.My s3.php file doesn't have this function.Here is my S3.php class.
    Also I read that S3 does not support folder structures but just visually makes it look like its stored in a folder due to the flat file system. Now,if I have to search a folder 1024x768 on S3,do I have just check the root of the bucket? I mean like this

         $chkFileExist = $s3->doesObjectExist($bucketName,'1024x768');
    

    I need to check if the folder exists and if not create it on the fly dynamically using the API functions.If it exists store the files over there.
    I need help in achieving this functionality.Please can anyone suggest solution with their experience.
    Thank you for your attention.