前言阿里云oss上传文件蕴含有好多SDK,个别像客户端的百度云盘这样的C端会应用如java相干的SDK,而浏览器端,也就是web端最好应用browser.js相干的SDK,上面就来解说下如何应用,心愿可能帮忙到大家。
配置ram权限获取sts token和跨域反对在应用阿里云OSS上传文件之前,要提前配置好ram相干权限,官网文档有阐明,具体请看应用STS长期拜访凭证拜访OSS,配置时,要留神两点:1、因为会应用上传和下载两种权限,所以应该应用上面权限策略(xxx为你的门路):
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "oss:PutObject", "oss:GetObject" ], "Resource": [ "acs:oss:*:*:xxx", "acs:oss:*:*:xxx/*" ] } ]}配置好之后,咱们再来配置后盾,返回sts token给前端
@Override public AssumeRoleResponse getAcsForUploadFile(String userName) { // STS接入地址,例如sts.cn-hangzhou.aliyuncs.com。 String endpoint = aliyunOssConfig.getRamStsEndpoint(); // 填写步骤1生成的拜访密钥AccessKey ID和AccessKey Secret。 String AccessKeyId = aliyunOssConfig.getRamAccessKeyId(); String accessKeySecret = aliyunOssConfig.getRamAccessKeySecret(); // 填写步骤3获取的角色ARN。 String roleArn = aliyunOssConfig.getRamRoleArn(); // 自定义角色会话名称,用来辨别不同的令牌,例如可填写为SessionTest。 String roleSessionName = userName; // 以下Policy用于限度仅容许应用长期拜访凭证向指标存储空间examplebucket上传文件。 // 长期拜访凭证最初取得的权限是步骤4设置的角色权限和该Policy设置权限的交加,即仅容许将文件上传至指标存储空间examplebucket下的exampledir目录。// String policy = "{" +// " \"Version\": \"1\", " +// " \"Statement\": [" +// " {\n" +// " \"Action\": [" +// " \"oss:PutObject\"" +// " ]," +// " \"Resource\": [" +// " \"acs:oss:*:*:com-seaurl-cdn/space\"," +// " \"acs:oss:*:*:com-seaurl-cdn/space/*\"," +// " ]," +// " \"Effect\": \"Allow\"" +// " }" +// " ]" +// "}"; try { // regionId示意RAM的地区ID。以华东1(杭州)地区为例,regionID填写为cn-hangzhou。也能够保留默认值,默认值为空字符串("")。 String regionId = aliyunOssConfig.getRegion(); // 增加endpoint。实用于Java SDK 3.12.0及以上版本。 DefaultProfile.addEndpoint(regionId, "Sts", endpoint); // 增加endpoint。实用于Java SDK 3.12.0以下版本。 // DefaultProfile.addEndpoint("",regionId, "Sts", endpoint); // 结构default profile。 IClientProfile profile = DefaultProfile.getProfile(regionId, AccessKeyId, accessKeySecret); // 结构client。 DefaultAcsClient client = new DefaultAcsClient(profile); final AssumeRoleRequest request = new AssumeRoleRequest(); // 实用于Java SDK 3.12.0及以上版本。 request.setSysMethod(MethodType.POST); // 实用于Java SDK 3.12.0以下版本。 //request.setMethod(MethodType.POST); request.setRoleArn(roleArn); request.setRoleSessionName(roleSessionName); request.setPolicy(null); // 如果policy为空,则用户将取得该角色下所有权限。 request.setDurationSeconds(3600L); // 设置长期拜访凭证的无效工夫为3600秒。 final AssumeRoleResponse response = client.getAcsResponse(request); System.out.println("Expiration: " + response.getCredentials().getExpiration()); System.out.println("Access Key Id: " + response.getCredentials().getAccessKeyId()); System.out.println("Access Key Secret: " + response.getCredentials().getAccessKeySecret()); System.out.println("Security Token: " + response.getCredentials().getSecurityToken()); System.out.println("RequestId: " + response.getRequestId()); return response; } catch (com.aliyuncs.exceptions.ClientException e) { System.out.println("Failed:"); System.out.println("Error code: " + e.getErrCode()); System.out.println("Error message: " + e.getErrMsg()); System.out.println("RequestId: " + e.getRequestId()); return null; } }2、设置跨域反对如果设置了跨域规定还是不起作用,能够应用上面形式进行批改:设置跨域规定后调用OSS时依然报“No 'Access-Control-Allow-Origin'”的谬误
...