Browse Source

aliyunoss使用spring管理client,分离sdk和配置文件

shuzheng 7 years ago
parent
commit
46428eeda9

+ 12 - 2
zheng-oss/zheng-oss-sdk/src/main/resources/applicationContext-oss.xml

@@ -1,8 +1,18 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
 	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
+	   xmlns:context="http://www.springframework.org/schema/context"
+	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 
-	<bean id="zhengAdminUtil" class="com.zheng.common.util.ZhengAdminUtil"></bean>
+	<description>zheng-oss-sdk</description>
+
+	<context:property-placeholder location="classpath*:zheng-oss-client.properties"/>
+
+	<!-- 阿里云OSS客户端 -->
+	<bean id="aliyunOssClient" class="com.aliyun.oss.OSSClient">
+		<constructor-arg value="${alipay.oss.endpoint}"/>
+		<constructor-arg value="${alipay.oss.accessKeyId}"/>
+		<constructor-arg value="${alipay.oss.accessKeySecret}"/>
+	</bean>
 
 </beans>

+ 11 - 22
zheng-oss/zheng-oss-web/src/main/java/com/zheng/oss/web/controller/DemoController.java

@@ -1,41 +1,30 @@
 package com.zheng.oss.web.controller;
 
 import com.aliyun.oss.OSSClient;
+import com.aliyun.oss.model.PutObjectResult;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.ModelMap;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.*;
 
 import java.io.ByteArrayInputStream;
 
 /**
  * Created by shuzheng on 2017/4/18.
  */
-@Controller
+@RestController
 @RequestMapping("/demo")
 public class DemoController {
 
-    @RequestMapping(value = "/index", method = RequestMethod.GET)
-    @ResponseBody
-    public String index(ModelMap modelMap) {
-        // endpoint以杭州为例,其它region请按实际情况填写
-        String endpoint = "oss-cn-shanghai.aliyuncs.com";
-        // accessKey请登录https://ak-console.aliyun.com/#/查看
-        String accessKeyId = "LTAIf5dDIthJN3h0";
-        String accessKeySecret = "2IZVHc1Qzxul8rC0ZQGKCEjjnpvm5d";
-
-        // 创建OSSClient实例
-        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
-
-        // 使用访问OSS
-        String content = "Hello OSS";
-        ossClient.putObject("shuzheng", "test", new ByteArrayInputStream(content.getBytes()));
-
-        // 关闭ossClient
-        ossClient.shutdown();
+    @Autowired
+    private OSSClient aliyunOssClient;
 
+    @GetMapping("/aliyun/put")
+    public String index() {
+        for (int i = 0; i < 10; i++) {
+            PutObjectResult putObjectResult = aliyunOssClient.putObject("shuzheng", "test" + i, new ByteArrayInputStream(("Hello OSS" + i).getBytes()));
+            System.out.println(putObjectResult);
+        }
         return "success";
     }
 

+ 4 - 0
zheng-oss/zheng-oss-web/src/main/resources/zheng-oss-client.properties

@@ -0,0 +1,4 @@
+### aliyun oss ###
+alipay.oss.endpoint=oss-cn-shanghai.aliyuncs.com
+alipay.oss.accessKeyId=LTAIf5dDIthJN3h0
+alipay.oss.accessKeySecret=2IZVHc1Qzxul8rC0ZQGKCEjjnpvm5d