13

I would like to enable the use of my Amazon S3 bucket for my Heroku application.

Before Heroku, I was using s3fs to mount the directory directly. Now that this is not possible on Heroku, is there any other way to "link" them together, possibly by still keeping the upload (& resize) scripts still working?

hakre
  • 193,403
  • 52
  • 435
  • 836
carmelo arena
  • 551
  • 1
  • 4
  • 15
  • How did you proceed with this issue? Any of the answers below were helpful? How did you solve it? Or didn't you solve it so far? Also can you give an example of your upload and resize scripts? – hakre Oct 28 '12 at 14:19
  • 2
    Are you doing a batch process? Or are you uploading files via a form and moving them to a s3 bucket on the fly? Or other implementation? This info will help us answer. – fideloper Nov 03 '12 at 15:57
  • [http://net.tutsplus.com/tutorials/php/how-to-use-amazon-s3-php-to-dynamically-store-and-manage-files-with-ease/?search_index=3](http://net.tutsplus.com/tutorials/php/how-to-use-amazon-s3-php-to-dynamically-store-and-manage-files-with-ease/?search_index=3 "http://net.tutsplus.com/tutorials/php/how-to-use-amazon-s3-php-to-dynamically-store-and-manage-files-with-ease/?search_index=3")
    this link provide step by step instruction how to upload files to amazon s3. please check out this link.
    – Jaykesh Patel Dec 24 '12 at 10:28
  • There are several php libraries for [S3 on github](https://github.com/search?langOverride=PHP&q=s3&repo=&start_value=1&type=Repositories&utf8=%E2%9C%93). – rwb Sep 27 '12 at 05:10
  • Your question is not getting answered because it's not clear. What do you mean by "linking"? What is it that you want to achieve, exactly? There are several PHP libraries that can help you upload files to S3. – regulatethis Jan 01 '13 at 23:47

3 Answers3

1

I guess that should still be possible. Have you taken a look at Using AWS S3 to Store Static Assets and File Uploads?

hakre
  • 193,403
  • 52
  • 435
  • 836
Benjamin Tan Wei Hao
  • 9,621
  • 3
  • 30
  • 56
1

Depending on what your scripts do, you may have to change them a bit.

I should state first of all that I have never done this, so I could be missing some details, but from my experience with PHP, Heroku and S3, it should be as simple as accepting the uploaded file (which will be stored in a temporary directory somewhere on Heroku's server), resize it, and finally upload it to S3.

If you already had an upload workflow working, the only difference should be that instead of saving the file to a directory in your server (which, from what you say, was actually a sort of a symlink to an S3 bucket), you will now upload it to S3. You can do this easily with this library: https://github.com/tpyo/amazon-s3-php-class

Something like this:

// Ink is expensive, let's write less
$file = $_FILES['uploadedfile']['tmp_name'];
$name = $_FILES['pictures']['name'];

// Resize the image
resize($file);

// Normally you would do this for storing the file locally
// move_uploaded_file($file, "$destinationdir/$name");

// Now you want to upload to S3
$s3 = new S3($awsAccessKey, $awsSecretKey);
$s3->putObject($s3->inputFile($file, false), $bucketName, $uploadName, S3::ACL_PUBLIC_READ)

Depending on how you organise your uploads, you may also want to keep a record of the bucket name and the file name, maybe in a database table where you can search for the file name and get the bucket name, because you will need both to retrieve the file.

borfast
  • 2,194
  • 1
  • 15
  • 34
0

You need to manually get the uploaded file and upload to S3, Keep in mind, Heroku has a read only file system, so you can not create temp file on Heroku app, every operation need to be done in memory.

Here is the S3 php api

A example:

// get uploaded file
$file = $_FILES['uploadedfile']['tmp_name'];

// do whatever manipulation on the file in memory
// $file = resize($file)

// upload file to S3, note, S3->putObjectFile won't work, as it require $file parameter to be string
if (S3::putObject(S3::inputFile($file), $bucket, $uri, S3::ACL_PRIVATE)) {
    echo "File uploaded.";
} else {
    echo "Failed to upload file.";
}

Update

it looks like S3 php lib does not have a api for uploading resource from memory, S3::inputFile need a string as input, not in memory resource. So the conclusion is: it it not possible to do so with S3 PHP client on Heroku

fuyi
  • 2,573
  • 4
  • 23
  • 46