-->

How does one determine the filetype on an AWS S3 h

2020-07-22 19:14发布

问题:

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.