出于数据传输安全考虑,使用OSS默认域名或传输加速域名访问某个时间点创建的Bucket内的特定类型文件时(例如Content-Type为text/html、image/jpeg等),OSS会强制在返回头中增加下载Header(x-oss-force-download: true和Content-Disposition: attachment)。标准浏览器检测到Content-Disposition: attachment时,会出现强制下载而不是预览行为。
def get_doc_temp_url(self, object_name, expire_time, file_name=None, is_public=True):
headers = dict()
headers['content-disposition'] = 'inline'
params = {'response-content-disposition': f"attachment; filename*=UTF-8''{filename}"}
token = self._get_sts_token(object_name, action="GetObject")
auth = oss2.StsAuth(token['token']['AccessKeyId'],
token['token']['AccessKeySecret'],
token['token']['SecurityToken'])
bucket = oss2.Bucket(auth, self.host, self.bucket)
headers['file_name'] = file_name
url = bucket.sign_url('GET', key=object_name, expires=expire_time, headers=headers, params=params)
if is_public:
internal_host = urlparse(self.host)
public_host = urlparse(self.public_host)
url = url.replace(self.bucket + '.' + internal_host.hostname, public_host.hostname)
return url