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

查看:234
本文介绍了如何确定不带扩展名的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'.如果为true,它将读取文件的内容.如果为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天全站免登陆