亚马逊S3只接受的文件,没有空间,在标题没有数字? [英] Amazon S3 only accepting files with no spaces, no numbers in the title?
问题描述
这是一个奇怪的问题,但只能用奇怪的行为我看到相称。我的应用程序的Rails 3 /回形针/ S3。
This is an odd question, but only commensurate with the strange behavior I'm seeing. My app is Rails 3/Paperclip/S3.
症状:
- 所有图像都上传不论其称号。
- 当上传一个.pdf或.doc,如果标题有一个像my_doc.pdf没有空格,它上传罚款。
- 当上传一个PDF或.doc与空间,如我的Doc.pdf,失败了,无论是与误差
水管坏
或文件默默未能上载到S3。 - 当上传一个PDF或.doc与数字,如mydoc20.pdf,它还上面的失败。
- All images are uploading regardless of their title.
- When uploading a .pdf or .doc, if the title has no spaces like my_doc.pdf, it uploads fine.
- When uploading a .pdf or .doc with spaces, such as My Doc.pdf, it fails, either with error
broken pipe
or by the file silently failing to upload to S3. - When uploading a .pdf or .doc with numbers, such as mydoc20.pdf, it also fails as above.
我想有两种可能的解决了这个问题。
I imagine there are two possible solutions to this problem.
- 直接修复
水管坏
错误(preferred法)。 - 自动重命名每一个上传的文件,删除空格和数字之前,它被保存到S3 - 而在源不固定的话,我想这将减轻这一问题
- Fix the
broken pipe
error directly (preferred method). - Automatically rename every uploaded file to remove spaces and numbers before it is saved to S3 - while not fixing it at the source, I imagine this would allay the issue.
我将不胜AP preciate任何帮助,您可以给我固定在1和/或2。
I would greatly appreciate any help you can give me in fixing 1 and/or 2.
# Upload.rb model
class Upload < ActiveRecord::Base
has_attached_file :document,
:storage => :s3,
:s3_credentials => "#{::Rails.root.to_s}/config/s3.yml",
:path => "/docs/:style/:id/:basename.:extension"
has_attached_file :photo,
:styles => {:medium => "200x300>", :thumb => "100x150>" },
:storage => :s3,
:s3_credentials => "#{::Rails.root.to_s}/config/s3.yml",
:path => "/photos/:style/:id/:basename.:extension"
# s3.yml
development:
bucket: dev_bucket_name
access_key_id: dev_acc_key
secret_access_key: dev_sec_key
production:
bucket: my_production_bucket
access_key_id: my_access_key_id
secret_access_key: my_secret_key
# environment.rb is empty with regard to uploading.
# uploads_controller.rb
def edit
@candidate = Candidate.find(current_user.user_type_id)
render :layout => 'forms'
end
def update
@candidate = Candidate.find(params[:id])
if @candidate.update_attributes(params[:candidate])
flash[:notice] = "Profile updated successfully."
redirect_to :action => "show", :id => params[:id]
else
flash[:notice] = "There was an error updating your profile."
render :action => "edit", :id => params[:id]
end
end
我不相信有任何涉及的方法。我几乎希望有一些明显的错误与我的做法,因为这意味着它会得到修复。)
I don't believe there are any methods involved. I almost hope there is something obviously wrong with my approach because that means it'll get fixed :).
推荐答案
有关第二部分本应该这样做:
For part two this should do it:
@s = "Really Important!*() Document version#123123.newest.pdf"
@s.gsub!(' ','_').downcase! #this will make everything lowercase and replace all spaces with underscores
@s.gsub!(/[^a-zA-Z._]+/,'') #this will remove all numbers and special characters except . and _
puts @s #prints "really_important_document_version.newest.pdf"
编辑:经过一些研究,回形针,我发现了以下内容:的 http://blog.wyeworks.com/2009/7/13/paperclip-file-rename
After some more research into paperclip I found the following: http://blog.wyeworks.com/2009/7/13/paperclip-file-rename
检查链接的时候,我相信这是你所期待的。
Check that link out, I believe it is what you are looking for.
编辑2:在您的文章中我最初读我错过地掏出号码以及一部分,我已经修改了regulat EX pression code考虑到这一点。
Edit 2: In my initial read of your post I missed the part about pulling out numbers as well, I have modified the regulat expression code to account for that.
这篇关于亚马逊S3只接受的文件,没有空间,在标题没有数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!