2010-08-26 64 views
0

爲什麼會發生這種情況?圖像顯示爲白色,但是當我點擊'查看圖像'時,它會提示下載它

其ACL權限與所有其他照片完全相同。

這些照片是通過S3存儲區託管的,但是再次,沒有什麼比其他任何不同。

這裏是文件網址:

http://s3.amazonaws.com/hq-photo/root/system/images/340/resized_thumb/Osseo-Endo_System.png?1272485279 

它與在同一頁上的兩個圖像的發生。

回答

2

您需要設置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類型。

+0

您的epub與epub讀者的.epub文件擴展名相同嗎?不確定你在做什麼。也許你知道我可以閱讀的鏈接?在這件事上抓住我的大腦有困難。 – Trip 2010-08-26 15:06:58

+0

對不起,如果上述是混亂。是的,在我的代碼中使用epub標誌來說明文件是否是epub文件。 Epub文件不需要有.epub後綴。由於一些電子書閱讀器顯示文件後綴,因爲它看起來很醜,所以你不需要一個。我註釋了上面的代碼。 – 2010-08-26 16:27:07

+0

它是一個令人難以置信的答案。對此,我真的非常感激。謝謝。我希望我能想出一個方法來在s3實例中定位文件。使用s3sh,似乎無法探索存儲桶中的子文件夾。希望我知道一些常見的命令。 http://stackoverflow.com/questions/3576832/common-commands-for-exploring-buckets-with-s3sh-or-awss3 – Trip 2010-08-26 16:52:05

相關問題