Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws sdk php for listing s3 folders

few things I do know. Folders are not a concept of S3 they are merely a zero byte object with key names that use the '/' as prefixes. Assuming that we call them folders and files for simplicity would make my question easier to phrase.

It is possible to do a s3->get_object_list(..) with prefix ="root/" and get all the files/folders. My problem is this.

Given a list of folders and subfolders for e.g.

root/  
root/folder1/  
root/folder1/file1.txt  
root/folder2/ 
root/folder3/file2.txt

How can i list only the folders. I was hoping to prevent listing all files (even in response they need not be returned) but get_object_list in php does not current support commonPrefixes ref: http://docs.amazonwebservices.com/AWSSDKforPHP/latest/#m=AmazonS3/get_object_list even though the doc says so the code clearly does not and using the delimiter will actually only return files in the root folder (prefix)

I am wondering if anyone has used any technique post-response or request based to help with such a situation.

I have already tried every combo of prefix,delimiter and marker i could think of. Not so good with PCRE but sure I could use that I only have one level deep folders but I don't know how to construct one for this case. Since my requests were not working I proceeded to accept the response with all files and folders and hopefully sort it out in php... or so I thought

I have tried in post response: dirname() - no use misses out some folders (empty ones), sure I can remove duplicates in array but the problem of missed folder exists pathinfo() - behaves the same way as dirname(), assumes that all paths end with filename. glob - no luck, same as above.

Since I have already wasted half a day on this I am hoping someone has dealt with this and would care to share:

note: I have no problem doing this in python and boto. But this is not python and I have to do this in PHP for this project.

like image 326
Abhishek Dujari Avatar asked Oct 07 '12 16:10

Abhishek Dujari


People also ask

What is Amazon S3 SDK for PHP?

Amazon S3 provides easy to use object storage, with a simple web service interface to store and retrieve any amount of data from anywhere on the web. All the example code for the AWS SDK for PHP Version 3 is available here on GitHub .

What is the SDK for AWS?

The SDK is a modern, open-source PHP library that makes it easy to integrate your PHP application with AWS services like Amazon S3, Amazon Glacier, and Amazon DynamoDB.

What services does AWS SDK for PHP support?

Supported services include Amazon EC2, Amazon S3, Amazon SNS, Amazon SQS, AWS IAM, Amazon Glacier, and AWS CloudFormation, and more services will be added in the future. Looking for old versions of AWS SDK for PHP?

How to create a bucket in AWS S3?

How to create a bucket in AWS S3 ? Click the Services dropdown and select the S3 service. Click Create Bucket. Give it a name, region then hit next through each step.


1 Answers

You've confused get_object_list() with list_objects(). See: https://forums.aws.amazon.com/thread.jspa?threadID=106277&tstart=0

like image 138
Ryan Parman Avatar answered Sep 21 '22 13:09

Ryan Parman