集成框架 -- OSS

前言

接入oss必须有这两个文档基础

使用STS临时访问凭证访问OSS_对象存储(OSS)-阿里云帮助中心
前端上传跨域


正文

sts前后端通用,开通图示

AliyunSTSAssumeRoleAccess



后端实现代码

java 复制代码
    public static void main(String[] args) {
  
        String regionId = "cn-hangzhou";
        String endpoint = "sts.cn-hangzhou.aliyuncs.com";

        String accessKeyId = "accessKeyId"; //用户提供
        String accessKeySecret = "accessKeySecret";//用户提供

        String roleArn = "acs:ram::roleArn:role/ramoss";//角色提供
        String roleSessionName = "roleSessionName";// 自定义
        String bucket =  "bucket"; // 对应创建buket 前端上传记得开跨域

        String policy = "{\n" +
                "    \"Version\": \"1\", \n" +
                "    \"Statement\": [\n" +
                "        {\n" +
                "            \"Action\": [\n" +
                "                \"oss:PutObject\"\n" +
                "            ], \n" +
                "            \"Resource\": [\n" +
                "                \"acs:oss:*:*:sc-sale-servcie/test/*\" \n" +
                "            ], \n" +
                "            \"Effect\": \"Allow\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";


        Long durationSeconds = 3600L;
        try {
            DefaultProfile.addEndpoint("", regionId, "Sts", endpoint);
            IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
            // 构造client。
            DefaultAcsClient client = new DefaultAcsClient(profile);
            final AssumeRoleRequest request = new AssumeRoleRequest();

            request.setMethod(MethodType.POST);
            request.setRoleArn(roleArn);
            request.setRoleSessionName(roleSessionName);
            //request.setPolicy(policy);
            request.setDurationSeconds(durationSeconds);

			// 如果前端上传这个给前端 把跨域打开
            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());


            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build("https://oss-cn-hangzhou.aliyuncs.com", response.getCredentials().getAccessKeyId(), response.getCredentials().getAccessKeySecret(), response.getCredentials().getSecurityToken());
            PutObjectRequest putObjectRequest = new PutObjectRequest("reformer", "1/reformer.sql", new File("/Users/xushouchun/IdeaProjects/REformer-boot/fromer-start/src/main/resources/database/reformer.sql"));


            try {
                // 上传文件。
                PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
                System.out.println("putObjectResult = " + JSON.toJSONString(putObjectResult));

                // copy
                CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, "1/reformer.sql", bucket, "1/sql.reformer");
                ossClient.copyObject(copyObjectRequest);

                // 生成下载URL
                Date expirationDate = new Date(new Date().getTime() + durationSeconds); // URL 的有效期
                GeneratePresignedUrlRequest requests = new GeneratePresignedUrlRequest(bucket, "1/reformer.sql");
                requests.setExpiration(expirationDate);
                ossClient.generatePresignedUrl(requests);


            } catch (OSSException oe) {
                System.out.println("Caught an OSSException, which means your request made it to OSS, "
                        + "but was rejected with an error response for some reason.");
                System.out.println("Error Message:" + oe.getErrorMessage());
                System.out.println("Error Code:" + oe.getErrorCode());
                System.out.println("Request ID:" + oe.getRequestId());
                System.out.println("Host ID:" + oe.getHostId());
            } finally {
                if (ossClient != null) {
                    ossClient.shutdown();
                }
            }

        } catch (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());
        }

    }

总结

后端代码已经提供,前端就比较简单了,拿到钥匙直接upload就可以了。 一般脚手架都有,凭借url即可

前端文档: sts前端上传代码

相关推荐
Yvonne爱编码2 天前
前端工程化进阶:从搭建完整项目脚手架到性能优化【技术类】
前端·状态模式
Mr_sun.2 天前
Day02——基础数据开发-服务管理前端
前端·状态模式
xiaoxue..2 天前
Zustand 状态管理:轻量高效的 React 状态解决方案✨
前端·react.js·面试·状态模式·zustand
数据库知识分享者小北3 天前
从极速复制“死了么”APP,看AI编程时代的技术选型
数据库·阿里云·状态模式·ai编程·supabase
桃子叔叔3 天前
react-wavesurfer录音组件2:前端如何处理后端返回的仅Blob字段
前端·react.js·状态模式
牵牛老人3 天前
Qt后端开发遇到跨域问题终极解决方案 与 Nginx反向代理全解析
qt·nginx·状态模式
C_心欲无痕3 天前
有限状态机在前端中的应用
前端·状态模式
雨中散步撒哈拉4 天前
22、做中学 | 高一下期 | Golang反射
开发语言·golang·状态模式
小陈phd4 天前
langGraph从入门到精通(四)——基于LangGraph的State状态模式设计
python·microsoft·状态模式
stella·4 天前
后端二进制文件,现代前端如何下载
前端·ajax·状态模式·axios·request·buffer·download