From: Michael Gauthier on
-------- Forwarded Message --------
From: Jason <jasondaly(a)gmail.com>
To: Michael Gauthier <mike(a)silverorange.com>
Subject: Re: Guidance Regarding Loading S3 Files Locally
Date: Thu, 28 Jan 2010 06:28:45 -0500

Hi Michael-


Thanks for taking the time to reply so thoroughly to my email; it's very
much appreciated. Absolutely you can feel free to post this to the
mailing list.


It turned out my problem was that I am simultaneously maintaining a
cached version of recently used resource object classes locally, and was
base64_encode'ing the $object->data before serializing the class to
store in the cache file. I as inadvertently pushing this encoded ->$data
to S3, consequently providing me with data in a format I was not
expecting (not to mention it prevents me from linking to the file
directly).


I ended up just needing to clone the $object before preparing it for the
cache :-)


Thanks again for your time.

On Wed, Jan 27, 2010 at 9:18 AM, Michael Gauthier
<mike(a)silverorange.com> wrote:
On Tue, 2010-01-26 at 22:19 +0000, Jason Daly wrote:
> [This message has been brought to you via pear.php.net.]
>
> Hi Michael-
>
> Looking at one of the examples in the S3.php class itself
>
> $object = $bucket->getObject('foo.gif');
> $object->load();
> $img = imagecreatefromstring($object->data);
>
> My question is, is there a proper, more generic way to handle
the
> re-creation of a file locally? I am storing much more than
images in my
> S3 buckets, but it is a bit unclear how to properly store the
file with
> the appropriate contenttype/other headers necessary to allow
the file to
> be properly recognized by a local OS.
>
> Can you provide any guidance regarding this?

Hi Jason,

S3 is a bit-bucket, so you can store any information in pretty
much any
format and S3 doesn't care. How you save objects to your system
from S3
depends on how you store them.

I save files on S3 using the same file extension as the local
file. This
helps me quickly distinguish file types on S3 without having to
download
the data. For example:

$remote_filename = 'photos/image.jpg';
$local_filename = dirname(__FILE__).'/image.jpg';
$object = $bucket->getObject($remote_filename);
$object->data = file_get_contents($local_filename);
$object->save();

Alternatively, you could store each filename in a local database
with
some meta-information about the file, such as the file type,
local
filename, size, MD5, etc.

When saving back to my system from S3, I would use:

$remote_filename = 'photos/image.jpg';
$local_filename = dirname(__FILE__).'/image.jpg';
$object = $bucket->getObject($remote_filename);
if
($object->load(Services_Amazon_S3_Resource_Object::LOAD_DATA)) {
file_put_contents($local_filename);
}

When saving a file from my system to S3, I typically also set
the S3
metadata field for the file's content-type. Setting the
content-type on
S3 is important if you are serving any files directly from S3. I
use the
PHP fileinfo extension to get mime-types and add the following
lines
when I'm saving the object:

$finfo = finfo_open(FILEINFO_MIME);
$object->contentType = finfo_file($finfo, $local_filename);

There are a few other header fields you can set for objects
which can be
found in Services_Amazon_S3_Resource_Object::$allowedHeaders
field. They
are set through the Services_Amazon_S3_Resource_Object::
$httpHeaders
field.

Hope this answers your question. Also, let me know if I can
forward this
to pear-dev so it is helpful to other users.

Cheers,


Mike




--
Jason Daly
D4Ly.com