更新时间:2024-06-21 GMT+08:00
重写响应头
下载对象时,可以重写部分HTTP/HTTPS响应头信息。可重写的响应头信息见下表:
参数 |
作用 |
OBS Android SDK对应方法 |
---|---|---|
contentType |
重写HTTP/HTTPS响应中的Content-Type |
ObjectRepleaceMetadata.setContentType |
contentLanguage |
重写HTTP/HTTPS响应中的Content-Language |
ObjectRepleaceMetadata.setContentLanguage |
expires |
重写HTTP/HTTPS响应中的Expires |
ObjectRepleaceMetadata.setExpires |
cacheControl |
重写HTTP/HTTPS响应中的Cache-Control |
ObjectRepleaceMetadata.setCacheControl |
contentDisposition |
重写HTTP/HTTPS响应中的Content-Disposition |
ObjectRepleaceMetadata.setContentDisposition |
contentEncoding |
重写HTTP/HTTPS响应中的Content-Encoding |
ObjectRepleaceMetadata.setContentEncoding |
以下代码展示了如何重写响应头:
// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY_ID。 // 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/intl/zh-cn/usermanual-ca/ca_01_0003.html String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); String endPoint = "https://your-endpoint"; // 创建ObsClient实例 ObsClient obsClient = new ObsClient(ak, sk, endPoint); GetObjectRequest request = new GetObjectRequest("bucketname", "objectname"); ObjectRepleaceMetadata replaceMetadata = new ObjectRepleaceMetadata(); replaceMetadata.setContentType("image/jpeg"); request.setReplaceMetadata(replaceMetadata); ObsObject obsObject = obsClient.getObject(request); Log.i("GetObject", object.getMetadata().getContentType()); obsObject.getObjectContent().close();
父主题: 下载对象