简体   繁体   English

下载后更改存储在Amazon S3上的文件的文件名

[英]Changing the filename of a file stored on Amazon S3 upon download

So I have an upload service with many people uploading the same files to my Amazon S3 bucket. 因此,我有一个上传服务,许多人将相同的文件上传到我的Amazon S3存储桶。 I changed my app design so the SHA1 of the file is calculated upon upload and checked against the list of uploaded files. 我更改了应用程序的设计,以便在上传时计算文件的SHA1,并对照上传的文件列表进行检查。

If it exists, I simply assign the file to the new uploader as well. 如果存在,我也只需将文件分配给新的上传器。

The problem with this is, the file being named as the first uploader named it. 问题是文件被命名为第一个上载程序。 All subsequent uploaders will get the same first name. 随后的所有上传者将获得相同的名字。

I can use download="" attribute in HTML5 but it doesn't work in IE: http://caniuse.com/#search=download 我可以在HTML5中使用download =“”属性,但在IE中不起作用: http : //caniuse.com/#search=download

The files are stored remotely so I can't change the header unless I download it first to my local server which is illogical. 这些文件是远程存储的,因此除非我先将其下载到不合逻辑的本地服务器上,否则就无法更改标头。

Please advice. 请指教。

The only way I see this possible is to add a meta-data just before redirecting user to download. 我认为这可能的唯一方法是在将用户重定向到下载之前添加元数据。

You can add meta-data to your files in S3. 您可以在S3中将元数据添加到文件中。 With key as "Content-Disposition" and value as 'attachment; 键为“ Content-Disposition”,值为“ attachment”; filename="~actual file name~"'. filename =“〜实际文件名〜”'。 You can force the name and trigger download. 您可以强制名称并触发下载。

This way, you don't have to download any files to local file system. 这样,您不必将任何文件下载到本地文件系统。

The caveat to this is if someone else is also requesting the same file with in milli-seconds, first user might get the name as requested by second user. 需要注意的是,如果其他人也在毫秒内请求同一文件,则第一个用户可能会获得第二个用户所请求的名称。

use the rename() method 使用rename()方法

example: 例:

$file = "boo.png";
$newName = "scary";
$ext = substr( $file, strpos( "." ), strlen( $file ) );
$path = "/images/downloaded/";

rename($path . $file, $path . $newName . $ext);

php documentation PHP文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM