Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a object exists on amazon s3 using AWS api

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.

like image 939
KillABug Avatar asked Jul 16 '13 15:07

KillABug


2 Answers

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.

like image 51
Dan Avatar answered Sep 30 '22 06:09

Dan


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

like image 37
Ryan Parman Avatar answered Sep 30 '22 06:09

Ryan Parman