背景说明

最近做一个APP客户端图片直传阿里云OSS的服务,需要在后台开一个阿里云的OSSToken获取的接口。

阿里云官方文档地址:快速搭建移动应用直传服务

略过移动端说明,直接看服务端的。

不是移动端直传吗,为什么需要服务端呢?原因如下:

Android和iOS应用不能直接存储AccessKey,这样会存在数据泄露的风险。所以应用必须向用户的应用服务器申请一个Token。

这个Token是有时效性的,如果Token的过期时间是30分钟(由应用服务器指定),那么在这30分钟里,该Android/iOS应用可以使用此Token从OSS上传和下载数据, 30分钟后需要重新获取Token。

正确返回的参数 Expiration 为该Token失效的时间。Android SDK会自动判断Token是否失效,如果失效,则自动获取Token。

代码实现

这里有个Java代码示例,可以下载来参考,把功能集成到项目中,没必要为一个接口单独部署一个项目到服务器。

项目环境是 SpringBoot2.0,JDK8(虽然官方说的是适用java1.7,但是JDK8兼容可用。)

添加依赖

首先在pom中添加相关依赖

        <!--阿里云对象存储-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-sts</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.0.0</version>
</dependency>

配置属性文件

可以从下载的 AppTokenServerDemo 项目中找到 config.json 文件,将其中的五个参数都配置到我们自己的属性文件 application.properties 中。

#阿里云对象存储
ali.oss.AccessKeyID=xxxxxx
ali.oss.AccessKeySecret=xxxxxxxxxxxxxxx
ali.oss.RoleArn=xxxxxxxxxxxxxx
ali.oss.TokenExpireTime=900
ali.oss.PolicyFile=policy/all_policy.txt

编写接口类

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*; /**
* @date 2019/4/7 20:18
* @auther wangbo
*/
@Api(tags = {"OSS接口类"})
@RestController
@RequestMapping("/api/oss")
public class ALiOSSController {
//只有 RAM用户(子账号)才能调用 AssumeRole 接口
//阿里云主账号的AccessKeys不能用于发起AssumeRole请求
//请首先在RAM控制台创建一个RAM用户,并为这个用户创建AccessKeys
@Value("${ali.oss.AccessKeyID}")
private String accessKeyId; @Value("${ali.oss.AccessKeySecret}")
private String accessKeySecret; //RoleArn 需要在 RAM 控制台上获取
@Value("${ali.oss.RoleArn}")
private String roleArn; //Token 过期时间,默认 900s
@Value("${ali.oss.TokenExpireTime}")
private Long tokenExpireTime; //阿里云对象存储例子的 token 权限文件
//all_policy.txt:指定了该token拥有对该账号下创建Bucket、删除Bucket、上传文件、下载文件、删除文件的权限 。
//bucket_read_policy.txt:指定了该token拥有该账号下对指定Bucket的读权限。
//bucket_read_write_policy.txt:指定了该token拥有该账号下对指定Bucket的读写权限。
@Value("${ali.oss.PolicyFile}")
private String policyFile; //目前只有"cn-hangzhou"这个region可用, 不要使用填写其他region的值
private static final String REGION_CN_HANGZHOU = "cn-hangzhou"; //这个是STS版本值
private static final String STS_API_VERSION = "2015-04-01"; @ApiOperation(value = "获取APPToken", notes = "获取阿里云对象存储的token")
@GetMapping("/getAppToken")
public void getAppToken(HttpServletRequest request, HttpServletResponse response) throws IOException {
//RoleSessionName 是临时Token的会话名称,自己指定用于标识你的用户,主要用于审计,或者用于区分Token颁发给谁
//但是注意RoleSessionName的长度和规则,不要有空格,只能有'-' '_' 字母和数字等字符
//具体规则请参考API文档中的格式要求
String roleSessionName = "alice-001";
//此处必须为 HTTPS
ProtocolType protocolType = ProtocolType.HTTPS;
//获取文件的字符串
String policy = ReadJson(policyFile);
//创建json对象
JSONObject respMap = new JSONObject();
try {
final AssumeRoleResponse stsResponse = assumeRole(accessKeyId, accessKeySecret, roleArn, roleSessionName,
policy, protocolType, tokenExpireTime);
//设置获取阿里云对象存储成功的值
respMap.put("StatusCode", "200");
respMap.put("AccessKeyId", stsResponse.getCredentials().getAccessKeyId());
respMap.put("AccessKeySecret", stsResponse.getCredentials().getAccessKeySecret());
respMap.put("SecurityToken", stsResponse.getCredentials().getSecurityToken());
respMap.put("Expiration", stsResponse.getCredentials().getExpiration());
} catch (ClientException e) {
//设置获取阿里云对象存储失败的值
respMap.put("StatusCode", "500");
respMap.put("ErrorCode", e.getErrCode());
respMap.put("ErrorMessage", e.getErrMsg());
}
//response写回json数据
response(request, response,respMap.toJSONString());
} protected AssumeRoleResponse assumeRole(String accessKeyId, String accessKeySecret, String roleArn,
String roleSessionName, String policy, ProtocolType protocolType, long durationSeconds) throws ClientException{
try {
//创建一个 Aliyun Acs Client, 用于发起 OpenAPI 请求
IClientProfile profile = DefaultProfile.getProfile(REGION_CN_HANGZHOU, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile); //创建一个 AssumeRoleRequest 并设置请求参数
final AssumeRoleRequest request = new AssumeRoleRequest();
request.setVersion(STS_API_VERSION);
request.setMethod(MethodType.POST);
request.setProtocol(protocolType); request.setRoleArn(roleArn);
request.setRoleSessionName(roleSessionName);
request.setPolicy(policy);
request.setDurationSeconds(durationSeconds); //发起请求,并得到response
final AssumeRoleResponse response = client.getAcsResponse(request); return response;
}catch (ClientException e){
throw e;
}
} /**
* 读取配置文件
* @param path
* @return
*/
public static String ReadJson(String path){
BufferedReader reader = null;
//返回值,使用StringBuffer
StringBuffer data = new StringBuffer();
try {
//从给定位置获取文件
ClassPathResource resource = new ClassPathResource(path);
InputStream inputStream = resource.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream));
//每次读取文件的缓存
String temp = null;
while((temp = reader.readLine()) != null){
data.append(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭文件流
if (reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return data.toString();
} /**
* 服务器响应结果
* @param request
* @param response
* @param results
* @throws IOException
*/
private void response(HttpServletRequest request, HttpServletResponse response, String results) throws IOException {
String callbackFunName = request.getParameter("callback");
if (callbackFunName==null || callbackFunName.equalsIgnoreCase(""))
response.getWriter().println(results);
else
response.getWriter().println(callbackFunName + "( "+results+" )");
response.setStatus(HttpServletResponse.SC_OK);
response.flushBuffer();
}
}

此外需要把相关的资源文件(可以在下载的demo项目中获取)引入到resource目录下:

踩坑记录

代码中的文件读取部分在我本地 idea 中运行服务时,可以正常执行。但是将服务打包成可执行jar的包,以jar服务运行服务时报错。

java.io.FileNotFoundException: class path resource [policay/all_policay.txt] cannot be resolved to absolute file path because it does not reside in the file system

原因:打包后Spring无法使用resource.getFile()访问JAR中的路径的文件,必须使用resource.getInputStream()。

我尝试了好几种:

(1)使用demo项目中的方式,本地线上都找不到文件

File file = new File("policay/all_policay.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));

(2)使用ResouceUtils类,线上版本找不到文件

File file = ResourceUtils.getFile("classpath:policay/all_policay.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));

(3)最后改为使用InputStream才解决问题。

ClassPathResource resource = new ClassPathResource("policay/all_policay.txt");
InputStream inputStream = resource.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

另外注意,该接口需要定义为GET接口,并且响应值不能自定义的,需要按照demo中的形式进行响应,因为该接口是给阿里云OSS调用的,前端会在相应位置配置该接口地址。

参考博客:

快速上手阿里云对象存储(一)

快速上手阿里云对象存储(二)

快速上手阿里云对象存储(三)

阿里云对象存储 OSS 应用服务器搭建代码的更多相关文章

  1. 阿里云对象存储OSS访问控制

    阿里云对象存储OSS的Android SDK提供了STS鉴权模式和自签名模式来保障移动终端的安全性. OSS可以通过阿里云STS (Security Token Service) 进行临时授权访问.交 ...

  2. 阿里云对象存储OSS与文件存储NAS的区别

    一.简介 应用场景:选择一款存储产品,面向文档数据的存取,不会涉及到数据处理. 产品选型主要从OSS和NAS中选择一款,满足文档存储的需求. 二.NAS优缺点 NAS 是一种采用直接与网络介质相连的特 ...

  3. 阿里云对象存储OSS使用 HTTPS

    一.前言 阿里云对象存储oss本身也是可以用HTTPS直接访问的,但是它本身的地址是http://***.oss-cn-hangzhou.aliyuncs.com这样的,那么如果我们想使用自己的域名, ...

  4. Delphi阿里云对象存储OSS【支持上传文件、下载文件、删除文件、创建目录、删除目录、Bucket操作等】

    作者QQ:(648437169) 点击下载➨Delphi阿里云对象存储OSS             阿里云api文档 [Delphi阿里云对象存储OSS]支持 获取Bucket列表.设置Bucket ...

  5. 阿里云对象存储OSS

    阿里云的产品种类繁多,今天让我们一起来了解下对象存储(Object Storage Service,简称OSS)吧! 什么是对象存储呢? 简单来说,对象存储OSS是阿里云提供的海量.安全和高可靠的云存 ...

  6. 阿里云对象存储OSS支持版本管理特性

    阿里云对象存储OSS现已经全面支持“对象版本管理”特性.该功能适用于所有的存储类型以及区域.当Bucket启用该特性后,“对象版本管理”功能可以保护和恢复误删除.误覆盖的数据. 对象存储OSS“版本管 ...

  7. 阿里云对象存储OSS及CDN加速配置

    目录 十大云存储服务商 1. 登陆阿里云官网,开通对象存储服务 OSS 2. 创建存储空间 3. 绑定自定义域名 4. 配置阿里云CDN加速 5. 购买阿里云免费SSL证书 6. 阿里云CDN配置HT ...

  8. java开发之阿里云对象存储OSS和云数据库Memcache的使用

    web开发中标配:aliyun ECS(阿里云服务器),aliyun RDS(阿里云数据库),aliyun OSS(阿里云对象存储),aliyun Memcache(阿里云缓存数据库). 今天就介绍下 ...

  9. 阿里云对象存储OSS————跨域资源共享(CORS)(m3u8 无法加载m3u8:跨域访问被拒绝)

    今天在做视频直播录像的时候,添加一个录制APP的.M3U8文件到OSS的一个test文件中存储,结果是访问不到了: 提示:无法加载m3u8:跨域访问被拒绝!!!!! 项目代码测试地址:https:// ...

随机推荐

  1. SpringMVC:处理静态资源

    方法1.采用<mvc:default-servlet-handler/> 若将 DispatcherServlet 请求映射配置为 /,则 Spring MVC 将捕获WEB 容器的所有请 ...

  2. mysqldump命令使用

    1.执行mysqldump命令前,先给配制添加帐号和密码: # vi /etc/my.cnf user=root password=XXXXXX 2. # mysqldump -uroot -pXXX ...

  3. 编译php-5.3.28

    1. 下载php-5.3.28 2. 编译/安装 ./configure --prefix=/usr/local/php --enable-fpm --enable-maintainer-zts -- ...

  4. pytest(1)

    安装就讲这么多,别的不多比比 第1个例子(基本法要了解): 这个例子中,还有一些需要注意的知识点: 1 pytest将在当前目录及其子目录中运行test _ * .py或* _test.py形式的所有 ...

  5. BootstrapTable,选中某几行,获取其数据并进行后台处理。以及其他的属性使用。

    参考链接:bootstrap Table API 中文版 Bootstrap Table 选中某几行,获取其数据 Ajax传递数组,struts2接收数组 1.首先将复选框搞出来,<table ...

  6. Selenium 常用API

    浏览器相关操作 启动浏览器 driver = new ChromeDriver(); 控制浏览器窗口大小 maximize() 设置浏览器最大化 setSize() 设置浏览器窗口的宽与高 访问网页 ...

  7. Anki 2.0 用户手册

    Anki 2.0 用户手册 目录 Anki 2.0 用户手册 视频介绍 卡片和模板 Javascript Anki 2.0 用户手册 视频介绍 共享牌组和基础复习技巧 调换卡片正反面 卡片样式设计 亲 ...

  8. 今日报错Cannot access java.lang.String

    public java.long.Long getId() { return id; } public void setId(java.lang.Long id) { this.id = id;} 手 ...

  9. json劫持payload

    <html> <head>jsonp hijacking</head> <body> <script> function jj(json){ ...

  10. Array库

    /** * 查找元素在数组中出现的所有位置 * @param {要查找的数组} array * @param {要查找的元素} ele * @param {回调函数} callback */ func ...