Amazon S3更改文件下载名称

时间:2023-02-06 00:15:18

I have files stored on S3 with a GUID as the key name.

我有文件存储在S3上,GUID作为密钥名称。

I am using a pre signed URL to download as per S3 REST API

我正在使用预签名的URL按照S3 REST API下载

I store the original file name in my own Database. When a user clicks to download a file from my web application I want to return their original file name, but currently all they get is a GUID. How can I achieve this?

我将原始文件名存储在我自己的数据库中。当用户单击从我的Web应用程序下载文件时,我想返回其原始文件名,但目前他们获得的只是一个GUID。我怎样才能做到这一点?

My web app is in salesforce so I do not have much control to do response.redirects all download the file to the web server then rename it due to governor limitations.

我的网络应用程序在salesforce中,所以我没有太多控制权来做响应。所有人都将文件下载到网络服务器,然后由于州长限制重命名。

Is there some HTML redirect, meta refresh, Javascript I can use? Is there some way to change the download file name for S3 (the only thing I can think of is coping the object to a new name, downloading it, then deleting it).

是否有一些HTML重定向,元刷新,我可以使用Javascript?有没有办法改变S3的下载文件名(我唯一能想到的是将对象复制到一个新名称,下载它,然后删除它)。

I want to avoid creating a bucket per user as we will have a lot of users and still no guarantee each file with in each bucket will have a unique name

我想避免为每个用户创建一个存储桶,因为我们会有很多用户,但仍然不能保证每个存储桶中的每个文件都有一个唯一的名称

Any other solutions?

还有其他方法吗?

3 个解决方案

#1


62  

I guess your cross posted this questions to Amazon S3 forum, but for the sake of others I'd like to post the answer here:

我猜你的十字架已经将这些问题发布到了亚马逊S3论坛,但是为了别人的缘故,我想在这里发布答案:

If there is only ever one "user filename" for each S3 object, then you can set the Content-Disposition header on your s3 file to set the downloading filename: Content-Disposition: attachment; filename=foo.bar

如果每个S3对象只有一个“用户文件名”,则可以在s3文件上设置Content-Disposition标头以设置下载文件名:Content-Disposition:attachment;文件名= foo.bar

For the sake of fairness I'd like to mention that it was not me to provide the right answer on Amazon forum and all credits should go to Colin Rhodes ;-)

为了公平起见,我想提一下,我不是要在亚马逊论坛上提供正确的答案而且所有的积分应该归到科林罗德;-)

#2


19  

In early January 2011 S3 added request header overrides. This functionality allows you to 'dynamically' alter the Content-Disposition header for individual requests.

在2011年1月初,S3添加了请求标头覆盖。此功能允许您“动态”更改单个请求的Content-Disposition标头。

See the S3 documentation on getting objects for more details.

有关详细信息,请参阅有关获取对象的S3文档。

#3


11  

While the accepted answer is correct I find it very abstract and hard to utilize.

虽然接受的答案是正确的,但我发现它非常抽象,难以利用。

Here is a piece of node.js code that solves the problem stated. I advise to execute it as the AWS Lambda to generate pre-signed Url.

这是一段解决所述问题的node.js代码。我建议将其作为AWS Lambda执行以生成预先签名的Url。

var AWS = require('aws-sdk');
var s3 = new AWS.S3({
    signatureVersion: 'v4'
});
const s3Url = process.env.BUCKET;

module.exports.main = (event, context, callback) => {
var s3key = event.s3key
var originalFilename = event.originalFilename

var url = s3.getSignedUrl('getObject', {
        Bucket: s3Url,
        Key: s3key,
        Expires: 600,
        ResponseContentDisposition: 'attachment; filename ="' + originalFilename + '"'
    });

[... rest of Lambda stuff...]

}

Please, take note of ResponseContentDisposition attribute of params object passed into s3.getSignedUrl function.

请注意传递给s3.getSignedUrl函数的params对象的ResponseContentDisposition属性。

More information under getObject function doc at http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getObject-property

有关getObject function doc下的更多信息,请访问http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getObject-property

#1


62  

I guess your cross posted this questions to Amazon S3 forum, but for the sake of others I'd like to post the answer here:

我猜你的十字架已经将这些问题发布到了亚马逊S3论坛,但是为了别人的缘故,我想在这里发布答案:

If there is only ever one "user filename" for each S3 object, then you can set the Content-Disposition header on your s3 file to set the downloading filename: Content-Disposition: attachment; filename=foo.bar

如果每个S3对象只有一个“用户文件名”,则可以在s3文件上设置Content-Disposition标头以设置下载文件名:Content-Disposition:attachment;文件名= foo.bar

For the sake of fairness I'd like to mention that it was not me to provide the right answer on Amazon forum and all credits should go to Colin Rhodes ;-)

为了公平起见,我想提一下,我不是要在亚马逊论坛上提供正确的答案而且所有的积分应该归到科林罗德;-)

#2


19  

In early January 2011 S3 added request header overrides. This functionality allows you to 'dynamically' alter the Content-Disposition header for individual requests.

在2011年1月初,S3添加了请求标头覆盖。此功能允许您“动态”更改单个请求的Content-Disposition标头。

See the S3 documentation on getting objects for more details.

有关详细信息,请参阅有关获取对象的S3文档。

#3


11  

While the accepted answer is correct I find it very abstract and hard to utilize.

虽然接受的答案是正确的,但我发现它非常抽象,难以利用。

Here is a piece of node.js code that solves the problem stated. I advise to execute it as the AWS Lambda to generate pre-signed Url.

这是一段解决所述问题的node.js代码。我建议将其作为AWS Lambda执行以生成预先签名的Url。

var AWS = require('aws-sdk');
var s3 = new AWS.S3({
    signatureVersion: 'v4'
});
const s3Url = process.env.BUCKET;

module.exports.main = (event, context, callback) => {
var s3key = event.s3key
var originalFilename = event.originalFilename

var url = s3.getSignedUrl('getObject', {
        Bucket: s3Url,
        Key: s3key,
        Expires: 600,
        ResponseContentDisposition: 'attachment; filename ="' + originalFilename + '"'
    });

[... rest of Lambda stuff...]

}

Please, take note of ResponseContentDisposition attribute of params object passed into s3.getSignedUrl function.

请注意传递给s3.getSignedUrl函数的params对象的ResponseContentDisposition属性。

More information under getObject function doc at http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getObject-property

有关getObject function doc下的更多信息,请访问http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getObject-property