如何确定没有扩展名的 AWS S3 托管文件的文件类型? [英] How does one determine the filetype on an AWS S3 hosted file without the extension?

查看:24
本文介绍了如何确定没有扩展名的 AWS S3 托管文件的文件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我目前正在使用表单将项目直接上传到 S3 存储桶.在测试时,我没有指定任何预期的文件名或扩展名.

As an example, I'm currently uploading items directly to an S3 bucket using a form. While I was testing, I didn't specify any expected filenames or extensions.

我上传了一个生成此直接链接的 .png:

I uploaded a .png which produced this direct link:

https://s3-us-west-2.amazonaws.com/easyhighlighting2/2015-07-271438019663927upload94788

当我将它放在 img 标记中时,它会正确显示在网页上.

When I place this inside an img tag, it displays on a web page properly.

我的问题是,如果没有扩展,我的浏览器如何知道它正在加载什么类型的文件?在存储桶中,甚至没有填写文件的元数据.

My question is, without an extension, how would my browser know what type of file it's loading? Inside the bucket, the file's metadata isn't even filled out.

有没有办法以编程方式获取该文件扩展名?

Is there any way to get that file extension, programmatically?

我已准备好尝试任何可用的客户端方法;我的服务器端语言是 ColdFusion,这有点限制,但我也愿意接受建议.

I'm ready to try any clientside methods available; my server-side language is ColdFusion which is somewhat limiting, but I'm open to suggestions for that as well.

推荐答案

好吧,经过一番深入挖掘,我找到了一个检索文件类型的方法,该方法是自CF10发布以来才添加的;这可以解释缺乏文档.

Okay, so after some more extensive digging, I found a method of retrieving the file's type that was only added since CF10 was released; that would explain the lack of documentation.

答案在于 FileGetMimeType功能.

The answer lies in the FileGetMimeType function.

<cfset someVar = "https://s3-us-west-2.amazonaws.com/easyhighlighting2/2015-07-271438019663927upload94788">
<cfset FileType = FileGetMimeType(someVar)>
<cfoutput>#FileType#</cfoutput>

此代码将输出 image/png - 这是正确的,并且适用于我迄今为止测试过的每种文件类型.

This code would output image/png - which is correct and has worked for every filetype I have tested thus far.

我很惊讶这种问题以前没有出现过,但这似乎是最好的答案,至少对于 CFML 的用户而言.

I'm surprised this kind of question hasn't popped up before, but this appears to be the best answer, at least for users of CFML.

ColdFusion 通过读取文件内容或信任其扩展名来完成此操作.此函数中使用了一个隐式属性 'strict'.如果为真,它会读取文件的内容.如果为 false,则使用提供的扩展名.

ColdFusion accomplishes this by either reading the contents of a file, or by trusting its extension. An implicit attribute, 'strict', is used in this function. If true, it reads the file's contents. If false, it uses the provided extension.

True 是默认值.

链接:

https://wikidocs.adobe.com/wiki/display/coldfusionen/FileGetMimeType

这篇关于如何确定没有扩展名的 AWS S3 托管文件的文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆