S3 using PHP Platform API

I need to create a new image file in a new folder on S3 using the php platform api. Below is what I have atm in my server script. I am piecing together docs as available but there is no example I can find to do this in any form. The only example I found is to upload a file to an existing folder, but nothing to create the folder in the first place:

//binary content pulled from another service
$image_payload = $image['content'];

// path from the root
$full_path = 'my_images/dir1/dir2/dir3/image.jpg';

$curl_options = array(
    'CURLOPT_HTTPHEADER'=> 'Content-Type: multipart/form-data'

//create the folder path and upload image "ap-images" is the S3 service
$post_svc_path = 'ap-images' . $full_path;
$s3_response = $platform['api']->post->__invoke($post_svc_path,$image_payload,$curl_options);

The error response is:

array(4) {
  array(1) {
    array(4) {
      string(88) "Folder 'my_images/dir1/dir2/dir3' does not exist."

So the question is, “Using the php platform api how do I create a new folder and then upload a file stored in a var into the new folder?”

Just a little bump. I’m stumped here.

In this scenario, the folder(s) need to be created first. On local file service, it will create any number of levels, but looks like the s3 service requires them to be created individually (will look into that).

//binary content pulled from another service
$image_payload = "whole lotta nothin";

// path from the root
$file_path = 'my_images/dir1/dir2/dir3/image.jpg';

//create the folder path and upload image "ap-images" is the S3 service
$s3_response = $platform['api']->post->__invoke('s3/my_images/');
$s3_response = $platform['api']->post->__invoke('s3/my_images/dir1/');
$s3_response = $platform['api']->post->__invoke('s3/my_images/dir1/dir2/');
$s3_response = $platform['api']->post->__invoke('s3/my_images/dir1/dir2/dir3/');
$s3_response = $platform['api']->post->__invoke('s3/' . $file_path,$image_payload);
echo print_r($s3_response);
1 Like

That’s works. Little “cloogey” having to loop through new folder but it does work. Thanks much!

Actually hold the phone. I’m getting an exception in the log when one of the folders in the path already exists (see below). The file is being created in S3 but because this is being called by a post process on a record add to a sql table, the record isn’t being written to the table. Is there a way to ignore this exception or force the post process to continue? Or does this have to do with the fact that S3 isn’t working the same way as local file for path creation?

[2016-02-25 23:56:33] local.ERROR: Exception: Folder ‘form_images/ABC University/’ already exists. {“response”:"[object] (DreamFactory\Core\Utility\ServiceResponse: {})"}

Below is the code I’m currently using. I tried to add a try/catch to ignore the exception but no dice. Still nothing being written to the db because of the exception. Looking for ideas.

$image_payload = $image['content']; //create the folder path and upload image "ap-images" is the S3 service $fpath = 'ap-images/form_images/'; foreach ($loop_folders as $folder) { $fpath .= $folder . '/'; if ($debug) echo print_r('Path : ' . $fpath . PHP_EOL); try { $s3_response = $s3_response = $post->__invoke($fpath); } catch (Exception $e) {} } $s3_response = $post->__invoke('ap-images' . $file_path,$image_payload);

I even tried to get the folder first and create the folder in the catch but got an exception there too. Even worse it didn’t create file on S3

@Mike_Harvey You said when you check if a folder already exists it gives you an exception and the script stops there and doesn’t proceed further to perform your SQL call. But we’ve tried this both as a custom script as well as a event script and it works. The exception doesn’t block anything.
We would be curious to know more about your specific use case and if at all possible to see the entire script. Also is this running as a custom script or a pre/post event script?

Actually as I said, the script doesn’t fail. But the record written by the post process is never committed. Sending script in email.

@mattschaer @leehicks In the example above “dir1” (or any of the directories expect the root) may or may not exist. If it worked as designed (with the full path), I would not have to write each folder as I nest in the loop. As the docs say, it should create folders if they don’t exist.

I also tried to “get” the folder with a try and post in the subsequent catch but that did not work for me either.

@mattschaer Bump Any ideas on this?

@Mike_Harvey After looking at the script with an engineer we don’t understand how this his script would stop you from writing to database. Running this in post-process event which occurs after the db service call the db call has already processed when this script is run. Secondly since you’re not making any additional POST calls within the db service to write anything to your database. So were not really sure what is meant by file service exceptions preventing you from writing to database. Please share any additional info you can and we’ll investigate this further for you.

All I can tell you is the the post job is firing but the record is not committed to table that called the post job in the first place. Would not an exception thrown in the post job make the post fail? The exception is coming from the S3 service as explained above for duplicate folder (which should not be an exception).

edit PS. If the S3 service didn’t throw exceptions for existing folders, I would consider this my issue to solve and not a bug. But since the post to the db worked fine without the S3 bit, I have to assume the exception from S3 is the cause of the failed commit to the db.

@Mike_Harvey Sure, We’ll look at this a bit more. Is there any additional info you can provide that may help us identify what the issue is?

@Mike_Harvey I am working on getting some additional feedback from engineers for you. They were able to run the scripts successfully. I hope to get a working set up soon to share with you. Thanks for hanging in there, I’ll try to get some feedback for you shortly.

@mattschaer Much obliged. Looking forward to it.