繁体   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