您需要设置MIME类型。浏览器不一致地使用文件扩展名。
当您存储文件,这样做:
opt = [] # default options for the S3Object.Store call. See the method's
# source for info on all the options
epub = an_epub_file?(obj) # my private method to determine if I'm storing
# an epub file.
extname = File.extname(obj) # Gets the suffix from the file name. Eg ".js"
mimes = MIME::Types.type_for(obj) # if the mime library finds anything, an array
# of mime values is returned
mime = (mimes && mimes[0] && mimes[0].content_type) || # did we get a value from
# the mime library?
( # Nothing from the mime library. Sigh.
# Let's try other ways to get the right mime value...
case
when epub: "application/epub+zip"
when extname == ".js" : "application/x-javascript"
else "application/octet-stream" # default mime value
end)
opt[:content_type] = mime
AWS::S3::S3Object.store(obj, data, bucket, opt)
注意的情况下表达代码示例中使用。这不是一个案例声明。案例表达式使用案例的匹配部分来返回一个值。
在上面的赋值语句中,如果OR表达式的左侧结束为false或null,则计算case表达式。
我使用了一个case表达式,因为我预计将来需要添加其他专门的MIME类型。
您的epub与epub读者的.epub文件扩展名相同吗?不确定你在做什么。也许你知道我可以阅读的链接?在这件事上抓住我的大脑有困难。 – Trip 2010-08-26 15:06:58
对不起,如果上述是混乱。是的,在我的代码中使用epub标志来说明文件是否是epub文件。 Epub文件不需要有.epub后缀。由于一些电子书阅读器显示文件后缀,因为它看起来很丑,所以你不需要一个。我注释了上面的代码。 – 2010-08-26 16:27:07
它是一个令人难以置信的答案。对此,我真的非常感激。谢谢。我希望我能想出一个方法来在s3实例中定位文件。使用s3sh,似乎无法探索存储桶中的子文件夹。希望我知道一些常见的命令。 http://stackoverflow.com/questions/3576832/common-commands-for-exploring-buckets-with-s3sh-or-awss3 – Trip 2010-08-26 16:52:05