Paulo Hgo
Paulo Hgo

Reputation: 860

How to use SimpleXml in PHP with files in AWS S3 buckets

I have a routine that iterates through the elements of an XML using Xmliterator, loading it first with simplexml_load_file(). It works fine if it's in a specific folder of the web server but I'm struggling to understand how to read it directly from the S3 bucket.

Here's the code that reads from the web server folder location:

$xml=simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/uploads/' .     $_SESSION["username"] . "/" . $fnametp, 'SimpleXMLIterator');

Now if I want to read it directly from the S3 bucket (is that even a best practice?), what would the line look like? Here's what I tried to no avail:

$xml=simplexml_load_file($s3->getObjectUrl($config['s3']['bucket'], $fnametp), 'SimpleXMLIterator');

All S3 parameters are correct, I use these parameters elsewhere in my app and it's properly populating the bucket and reading from it. I'm most likely using the wrong method for what I'm trying to achieve but can't figure out how to navigate this. The error message is 403/forbiden. I do have the proper rights and the file has been uploaded as public-read.

Thanks!

Upvotes: 1

Views: 811

Answers (1)

Paulo Hgo
Paulo Hgo

Reputation: 860

There was a typo in the path to the S3 file (key). Simplexml actually works fine if you point it to a file in the S3 bucket using getObjectUrl.

Upvotes: 1

Related Questions