How does one determine the filetype on an AWS S3 hosted file without the extension?

非 Y 不嫁゛ 提交于 2019-12-06 09:27:41

问题


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.

I uploaded a .png which produced this direct link:

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

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?

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.


回答1:


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.

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>

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

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.

Edit:

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 is the default.

Link:

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




回答2:


Check the Content-Type HTTP response header returned by Amazon S3.

For example, curl -I https://s3.amazonaws.com/path/to/file fetches only the headers.



来源:https://stackoverflow.com/questions/31661965/how-does-one-determine-the-filetype-on-an-aws-s3-hosted-file-without-the-extensi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!