天天看點

阿裡雲oss_阿裡雲OSS設定Content-Type

發現阿裡雲OSS預設Content-Type類型都是application/octet-stream

這樣上傳圖檔等檔案的時候,浏覽器不能直接打開,直接下載下傳,很不友善

這個是送出阿裡工單,給到的回複

【問題定義】 oss修改content-type 【處理建議】 您好,關于批量修改meta資訊的問題,推薦您使用兩種方法: 1、使用sdk進行接口的調用處理,參考下:(Java為例子)

https://help.aliyun.com/document_detail/84840.html?spm=a2c4g.11186623.6.811.66f0c1f65NwVs5 2、使用ossutil指令行工具進行處理,支援字首以及目錄之類的方式哈。 參考下: https://help.aliyun.com/document_detail/120056.html?spm=a2c4g.11186623.6.735.181165d32BkZMj

我用的語言是php,幾乎沒有解決

用的laravel5.2架構,OSS用的johnlui/AliyunOSS

git位址是:https://github.com/johnlui/AliyunOSS

查了很多資料,一直進入的誤區是Content-Type是在$options = [];裡面

官方文檔也是

https://help.aliyun.com/document_detail/88508.html?spm=a2c4g.11186623.6.1097.4ffe720fx7gxsu

阿裡雲oss_阿裡雲OSS設定Content-Type

實際上檢視johnlui/AliyunOSS代碼

在路徑vendor/johnlui/aliyun-oss/src/AliyunOSS.php

修改AliyunOSS.php檔案裡uploadFile方法

public function uploadFile($key, $file, $options = [])

{

$handle = fopen($file, 'r');

$value = $this->ossClient->putObject(array_merge([

'Bucket' => $this->bucket,

'Key' => $key,

'Content' => $handle,

'ContentLength' => filesize($file),

'ContentType' => getimagesize($file)['mime'], //增加這一行

], $options));

fclose($handle);

return $value;

}

測試,修改成功

繼續閱讀