阿里云OSS存储快速开发

阿里云云存储的官方文档相对微软对象存储来讲要容易很多,这里就是记录一下自己曾经做过,写的不好勿喷;

SDK源码和API文档

SDK源码请参见GitHub。更多信息请参见OSS Java SDK API文档

增删改查参照文档就可以搞定,这里就不多说了,直接看后面连接就可以了;

下面介绍一下Sts的获取,直接上代码

 String roleSessionName = "alice-001";         //这个可以为空,不好写,格式要对,无要求建议为空
 String policy = null;
 ProtocolType protocolType = ProtocolType.HTTPS;

 String accessKeyId = "LTAIUBoCI9E4nicI";
 String accessKeySecret = "epnz5rm1E7HHMO4EVP7B0NlWhYr3rV";

        // 发起请求,并得到response
        AssumeRoleResponse response = null;
        StsResponse stsResponse = null;
        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);
            if (request1.getStsType() == 1){
                request.setRoleArn(roleArnUpload);
            }else if (request1.getStsType() == 2){
                request.setRoleArn(roleArnDownload);
            }
            request.setRoleSessionName(roleSessionName);
            request.setPolicy(policy);
            try {
                response = client.getAcsResponse(request);
                logger.info("获取aliyun的sts的返回值是:"+response);
            } catch (com.aliyuncs.exceptions.ClientException e) {
                e.printStackTrace();
            }

        } catch (com.aliyun.oss.ClientException e) {
            throw new RuntimeException("获取sts失败");
        }

以上涉及到的变量都可以在门户后台里面创建后找到;有什么问题加1220145227;

参考文件:

https://help.aliyun.com/document_detail/32008.html?spm=a2c4g.11174283.3.3.45eb7da20L0TXc

猜你喜欢

转载自blog.csdn.net/studentxyh/article/details/86492791