簡體   English   中英

如何使用CloudFront從AWS S3獲取具有別名的對象URL

[英]How to get the object url with alias name from aws s3 using CloudFront

我正在將具有唯一ID(例如“ d9127dfd01182afe7d34a37”)的文件作為對象名稱上傳到亞馬遜s3,並將文件信息與本地數據庫一起存儲,包括文件的原始名稱。 我正在使用CloudFront網址下載文件。

如果我使用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 on rails和mongodb作為本地數據庫。 謝謝

我通過以下更改實現了

服務器端代碼

             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;
    },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM