Google Cloud Storage 在我的静态文件中发现错误的内容类型

Google Cloud Storage seeing incorrect content type on my static files

我的 google 云存储浏览器在我的所有静态文件(css、js、jpg 等)上看到不正确的内容类型,这导致 mime 类型错误提供这些文件导致我的网站无法应用我的 css 样式。为什么会发生这种情况,我该如何解决?我正确地链接了我的静态文件并指定了正确的内容类型,但这个错误仍然存​​在。

<link rel="stylesheet" type="text/css" media="screen,print" href="index_files/index.css" />
<script type="text/javascript" src="index_files/index.js"></script>

正如您在下图中看到的,google 云存储看到这些内容类型为 application/octet-stream 的静态文件,而本应将它们视为 text/csstext/javascript.

更新

文件已手动上传到存储桶。

您必须在控制台中手动编辑内容类型。说明在 documentation:

  1. Open the Cloud Storage browser in the Google Cloud Console.

  2. In the list of buckets, click on the name of the bucket that contains the desired object, and navigate to the object.

  3. Certain pieces of object metadata, such as the object's size and storage class, are displayed along with the object's name.

  4. Click the more actions menu () associated with the object.

  5. Click Edit metadata. The overlay window that appears shows the current values for the object's editable metadata.

如果您想将本地文件批量上传到 Cloud Storage 而不会出现 octet-stream 问题,您可以使用 Cloud SDK 使用 gsutil 命令上传它:

gsutil -m cp -r dir_of_folder_to_upload gs://bucket_name/folder

您还可以像这样批量更新您的回复 headers(Google 云称之为元数据):

为 CSS
设置 header 内容类型
gsutil -m setmeta -h "Content-Type:text/css" gs://your-bucket-name/any-folder/*.css

您也可以使用一条命令批量设置多个 header:

为 HTML 示例设置多个 header 内容类型:

gsutil -m setmeta -h "Content-Type:text/html;charset=UTF-8" -h "Cache-Control:public, max-age=3600" gs://your-bucket-name/any-folder/*.html

您可以在此处查看您可以设置的所有 header 的列表:https://cloud.google.com/storage/docs/gsutil/addlhelp/WorkingWithObjectMetadata