# Attachment\_fu with S3: custom path?

**URL:** <https://www.ruby-forum.com/t/attachment-fu-with-s3-custom-path/90006>\
**Category:** Rails\
**Created:** [March 24, 2007, 10:13pm UTC](https://www.ruby-forum.com/t/attachment-fu-with-s3-custom-path/90006 "2007-03-24T22:13:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [March 24, 2007, 10:13pm UTC](https://www.ruby-forum.com/t/attachment-fu-with-s3-custom-path/90006/1 "2007-03-24T22:13:22Z")

</div>

Hi,

I recently saw the light and have moved my storage from the server to  
the wonderful Amazon S3 service. I am also using the equally great  
attachment\_fu plugin.

I would like to store the uploaded files in a path such as:

/photos/[user\_id]/[photo.id].jpg

Instead of the default:

/photos/[photo.id]/[filename]

Has anyone customised the path like this? Any tips or pointers?

Thanks,  
GiantCranes

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [March 24, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/attachment-fu-with-s3-custom-path/90006/2 "2007-03-24T22:34:58Z")

</div>

On 3/24/07, Giant C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Instead of the default:
> 
> /photos/[photo.id]/[filename]
> 
> Has anyone customised the path like this? Any tips or pointers?
> 
> Thanks,  
> GiantCranes

You can just redefine the full\_filename method to fit your custom needs.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [March 24, 2007, 10:54pm UTC](https://www.ruby-forum.com/t/attachment-fu-with-s3-custom-path/90006/3 "2007-03-24T22:54:42Z")

</div>

Rick O. wrote:

> You can just redefine the full\_filename method to fit your custom needs.

Thanks for point out that Rick, I have tried doing the following (Photo  
belongs to Vehicle belongs to Seller):

def full\_filename  
return “photos/#{self.vehicle.seller.id}/#{self.vehicle.id}/#{id}” +  
File.extname(filename)  
end

This works on photos that have already been saved, but throws an  
exception when uploading new photos:

‘You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.seller’

This is puzzling to me, as the photo does have a Vehicle ion the  
controller:

def upload\_vehicle\_photo  
vehicle = Vehicle.find(params[:id])  
@photo = Photo.new(params[:photo])  
vehicle.photos \<\< @photo

```
if @photo.save
  flash[:information] = 'The photo was successfully uploaded.'
end

redirect_to :action => 'edit_vehicle_photos', :id => params[:id]

```

end

Am I doing anything obviously wrong here?

Thanks for any insight and for providing this great plugin.

GiantCranes

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [March 25, 2007, 2:38pm UTC](https://www.ruby-forum.com/t/attachment-fu-with-s3-custom-path/90006/4 "2007-03-25T14:38:09Z")

</div>

I posted a short guide on how to do this at  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/6a4aa492722da26a?hl=en](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/6a4aa492722da26a?hl=en)

Eric

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [March 25, 2007, 3:02pm UTC](https://www.ruby-forum.com/t/attachment-fu-with-s3-custom-path/90006/5 "2007-03-25T15:02:04Z")

</div>

Eric N. wrote:

> I posted a short guide on how to do this at  
> [http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/6a4aa492722da26a?hl=en](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/6a4aa492722da26a?hl=en)
> 
> Eric

Thanks Eric, that look like exactly what I need.
