2014-10-07 136 views
0

我使用唯一標識符'd9127dfd01182afe7d34a37'作爲對象名稱將文件上傳到amazon s3,並將文件信息與本地數據庫(包括文件的原始名稱)存儲在一起。我正在使用CloudFront url來下載文件。如何使用CloudFront從aws s3獲取別名的對象url

如果我使用CloudFront下載文件的url文件名是d9127dfd01182afe7d34a37。但我需要再次將文件名更改爲它的原始名稱,我在我的數據庫中。我不想下載它。我想給客戶端提供原始名稱的URL(WebUI),客戶端可以通過url下載。

服務器端代碼

document_url = initialize_cloud_service(document.provider['primary']).get_object_url(document_id, expires_at, 'CloudFront') 

if document_url 
      item = {} 
      item['id'] = document['_id'].to_s 
      item['name'] = document['name'] 
      item['mime_type'] = document['mime_type'] 
      item['url'] = document_url 
    return {success: true, message: MESSAGES['get_url_succuss'],data: item}.to_json 
end 

客戶端代碼

download: function(response){ 
     file = response.data 
     link = document.createElement('a'); 
     link.download = file.name; 
     link.href = file.url; 
     link.click(); 
    }, 

有什麼辦法來實現這一目標?請幫助我。我在軌道上使用ruby和mongodb作爲本地數據庫。 感謝

回答

0

我已經做了以下變化

服務器端代碼

   begin 
       expires_at = Time.now.to_i + 30.seconds.to_i 

       options = nil 
       selected_provider = provider || document.provider['primary'] 

       case selected_provider 
       when "s3" 
        options = {"response-content-disposition" => "attachment; filename=#{document['name']}"} 
        downloadable_url = initialize_cloud_service(selected_provider).get_downloadable_url(document_id, expires_at, options) 

       when "google_cloud" 
        downloadable_url = initialize_cloud_service(selected_provider).get_downloadable_url(document_id, expires_at, options) 
        downloadable_url += "&response-content-disposition=attachment%3B%20filename%3D#{document['name']}" 
       end 

       item = {} 
       item['id'] = document['_id'].to_s 
       item['name'] = document['name'] 
       item['mime_type'] = document['mime_type'] 
       item['url'] = downloadable_url 
       return {success: true, message: MESSAGES['get_url_succuss'],data: item}.to_json 
      rescue Exception => e 
       puts 'Exception in download, message: ' + e.message 
       return {success: false, message: MESSAGES['default']}.to_json 
      end 

客戶端代碼

 download: function(response){ 
     var hiddenIFrameID = 'hiddenDownloader', 
     iframe = document.getElementById(hiddenIFrameID); 
     if (iframe === null) { 
     iframe = document.createElement('iframe'); 
     iframe.id = hiddenIFrameID; 
     iframe.style.display = 'none'; 
     document.body.appendChild(iframe); 
     } 
     iframe.src = response.data.url; 
    }, 
實現