1. 程式人生 > >Java-青雲物件儲存SDK詳解

Java-青雲物件儲存SDK詳解

背景

主要是專案中用到青雲物件儲存儲存操作檔案,所以把該功能簡單梳理一下,以作記錄。

功能實現步驟

一)建立springboot專案,專案名為oysept-qingstor,專案結構圖如下

二)pom.xml配置檔案中內容如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.oysept.qingstor</groupId>
  <artifactId>oysept-qingstor</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>1.5.9.RELEASE</version>
       <relativePath/>
  </parent>
  
   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- json處理jar -->
        <dependency>
		    <groupId>com.alibaba</groupId>
		    <artifactId>fastjson</artifactId>
		    <version>1.2.49</version>
		</dependency>
		
		<dependency>
            <groupId>com.yunify</groupId>
            <artifactId>qingstor.sdk.java</artifactId>
            <version>2.2.6</version>
            <exclusions>
                <exclusion>
                    <groupId>com.squareup.okhttp3</groupId>
                    <artifactId>okhttp</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.9.0</version>
        </dependency>
        
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.oysept.qingstor.QingStorApplication</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
</project>

三)logback.xml配置檔案內容如下

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>

    <property name="rootPath" value="/app/oysept-qingstor/logs" />
    <property name="baseFile" value="oysept-qingstor"></property>
    <property name="log.root.level" value="INFO"></property>
    
    <!-- 控制檯輸出日誌 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%X{logthreadId} [%date{yyyy-MM-dd HH:mm:ss.SSS}] %level %logger{36} %line - %msg%n
            </pattern>
        </encoder>
    </appender>

    <!-- 檔案輸出日誌 -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${rootPath}/${baseFile}.log</File>
        <!-- 日誌檔案rolling策略 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${rootPath}/${baseFile}_%d{yyyy-MM-dd}.log.gz
            </FileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <!-- 日誌輸出格式 -->
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%X{logthreadId} [%date{yyyy-MM-dd HH:mm:ss.SSS}] %level %logger{36} %line - %msg%n
            </Pattern>
        </encoder>
    </appender>

    <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
        <discardingThreshold>0</discardingThreshold>
        <queueSize>10000</queueSize>
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE" />
    </appender>

    <root level="INFO">
        <appender-ref ref="ASYNC" />
    </root>

</configuration>

四)application.properties配置內容如下

server.port=8080

五)QingStorApplication啟動類內容如下

package com.oysept.qingstor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.oysept.qingstor.QingStorApplication;

/**
 * oysept-qingstor專案啟動類
 * @author ouyangjun
 */
@SpringBootApplication
public class QingStorApplication {

	public static void main(String[] args) {
		
		SpringApplication.run(QingStorApplication.class, args);
	}

}

六)QingStorUtils工具類內容如下

package com.oysept.qingstor.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qingstor.sdk.config.EvnContext;
import com.qingstor.sdk.service.QingStor;

/**
 * 
 * @author ouyangjun
 */
public class QingStorUtils {
	
	private final static Logger LOGGER = LoggerFactory.getLogger(QingStorUtils.class);
	
	// 北京3區
	public final static String zone = "pek3b";
	// qy_access_key_id
	private final static String accessKeyId = "";
	// qy_secret_access_key
	private final static String secretAccessKey = "";
	
	private static class QingStorService {
		// 獲取物件
		private static QingStor qingStor = getQingStor();
		
		private static QingStor getQingStor() {
			EvnContext evn = new EvnContext(accessKeyId, secretAccessKey);
			QingStor storService = new QingStor(evn, zone);
			
			LOGGER.info("QingStorSingleton,獲取QingStor物件!");
			// 返回
			return storService;
		}
	}
	
	/**
	 * 構建QingStor物件
	 * @return
	 */
	public static QingStor getQingStorSingleton() {
		return QingStorService.qingStor;
	}
	
}

七)BucketController介面類內容如下

package com.oysept.qingstor.controller;

import java.io.File;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.oysept.qingstor.utils.QingStorUtils;
import com.qingstor.sdk.exception.QSException;
import com.qingstor.sdk.service.Bucket;
import com.qingstor.sdk.service.QingStor;
import com.qingstor.sdk.service.QingStor.ListBucketsOutput;

/**
 * Bucket Controller
 * @author ouyangjun
 */
@RestController
@RequestMapping(value = "/qingstor")
public class BucketController {
	
	private final static Logger LOGGER = LoggerFactory.getLogger(BucketController.class);
	
	/**
	 * 該系統首頁測試介面
	 * @return
	 */
	@RequestMapping(value = "/home", method = RequestMethod.GET)
	public String home(){
		return "oysept qingstor bucket home!";
	}
	
	/**
	 * 青雲-獲取Bucket列表
	 * 請求地址: http://localhost:8080/qingstor/listBuckets
	 * @return
	 */
	@RequestMapping(value = "/listBuckets", method = RequestMethod.GET)
	public String listBuckets() {
		try {
			// 獲取QingStor物件
			QingStor qingstor = QingStorUtils.getQingStorSingleton();
			
			// 獲取Bucket列表
			ListBucketsOutput listOutput = qingstor.listBuckets(null);
			
			// 轉換成json字串
			String buckets = JSON.toJSONString(listOutput);
			LOGGER.info("Bucket列表: {}", buckets);
			
			// 返回值
			return buckets;
		} catch (QSException e) {
			LOGGER.info("/listBuckets,獲取Bucket列表失敗!", e);
		}
		return "List Bucket Error!";
	}
	
	/**
	 * 青雲-建立Bucket
	 * 請求地址: http://localhost:8080/qingstor/createBucket
	        引數: {
				"bucketName":"ouyangjun"
			}
	 * @return
	 */
	@RequestMapping(value = "/createBucket", method = RequestMethod.POST)
	public String createBucket(@RequestBody String objJson) {
		LOGGER.info("/createBucket,objJson: {}", objJson);
		if (StringUtils.isEmpty(objJson)) {
			return "objJson is null!";
		}
		try {
			LOGGER.info("/createBucket,建立Bucket步驟-----begin-----");
			// 解析物件
			JSONObject obj = JSONObject.parseObject(objJson);
			if (obj != null && !StringUtils.isEmpty(obj.get("bucketName"))) {
				// 獲取物件
				QingStor qingstor = QingStorUtils.getQingStorSingleton();
				
				// 構建物件
				Bucket bucket = qingstor.getBucket(String.valueOf(obj.get("bucketName")), QingStorUtils.zone);
				Bucket.PutBucketOutput putBucketOutput = bucket.put();
				
				LOGGER.info("/createBucket,建立Bucket步驟-----end-----");
				return JSONObject.toJSONString(putBucketOutput);
			}
		} catch (QSException e) {
			LOGGER.info("/createBucket,建立Bucket失敗!", e);
		}
		return "Create Bucket Error!";
	}
	
	/**
	 * 獲取Bucket中儲存的Object列表
	 * 請求地址: http://localhost:8080/qingstor/listObjects
	        引數: {
				"bucketName":"ouyangjun"
			}
	 */
	@RequestMapping(value = "/listObjects", method = RequestMethod.POST)
	public String listObjects(@RequestBody String objJson) {
		LOGGER.info("/listObjects,objJson: {}", objJson);
		if (StringUtils.isEmpty(objJson)) {
			return "objJson is null!";
		}
		try {
			LOGGER.info("/listObjects,獲取Bucket Object-----begin-----");
			// 解析物件
			JSONObject obj = JSONObject.parseObject(objJson);
			if (obj != null && !StringUtils.isEmpty(obj.get("bucketName"))) {
				// 獲取QingStor物件
				QingStor qingstor = QingStorUtils.getQingStorSingleton();
				
				// 獲取Bucket物件
				Bucket bucket = qingstor.getBucket(String.valueOf(obj.get("bucketName")), QingStorUtils.zone);
				// 獲取Object物件
				Bucket.ListObjectsOutput listOutput = bucket.listObjects(null);
				
				LOGGER.info("/listObjects,獲取Bucket Object-----end-----");
				return JSON.toJSONString(listOutput);
			}
		} catch (QSException e) {
			LOGGER.info("/listObjects,獲取Bucket Object列表失敗!", e);
		}
		return "List Object Error!";
	}
	
	/**
	 * 刪除Bucket中儲存的Object列表
	 * 請求地址: http://localhost:8080/qingstor/deleteObject
	        引數: {
				"bucketName":"ouyangjun",
				"objectName":""
			}
	 */
	@RequestMapping(value = "/deleteObject", method = RequestMethod.POST)
	public String deleteObject(@RequestBody String objJson) {
		LOGGER.info("/deleteObject,objJson: {}", objJson);
		if (StringUtils.isEmpty(objJson)) {
			return "objJson is null!";
		}
		try {
			LOGGER.info("/deleteObject,刪除Bucket Object-----begin-----");
			// 解析物件
			JSONObject obj = JSONObject.parseObject(objJson);
			if (obj != null && !StringUtils.isEmpty(obj.get("bucketName"))) {
				// 獲取QingStor物件
				QingStor qingstor = QingStorUtils.getQingStorSingleton();
				
				// 獲取Bucket物件
				Bucket bucket = qingstor.getBucket(String.valueOf(obj.get("bucketName")), QingStorUtils.zone);
				
				// 判斷檔名稱是否為空
				if (!StringUtils.isEmpty(obj.get("objectName"))) {
					// 獲取Object物件(名稱是唯一的,不能重複,如果檔案不存在,刪除就沒效果)
					Bucket.DeleteObjectOutput deleteOutput = bucket.deleteObject(String.valueOf(obj.get("objectName")));
					
					LOGGER.info("/deleteObject,刪除Bucket Object-----end-----");
					return JSON.toJSONString(deleteOutput);
				}
			}
		} catch (QSException e) {
			LOGGER.info("/deleteObject,刪除Bucket Object失敗!", e);
		}
		return "Delete Object Error!";
	}
	
	/**
	 * 在指定Bucket下建立一個Object物件
	 * 請求地址: http://localhost:8080/qingstor/createObject
	        引數: {
				"bucketName":"ouyangjun"
			}
	 */
	@RequestMapping(value = "/createObject", method = RequestMethod.POST)
	public String createObject(@RequestBody String objJson) {
		LOGGER.info("/createObject,objJson: {}", objJson);
		if (StringUtils.isEmpty(objJson)) {
			return "objJson is null!";
		}
		try {
			LOGGER.info("/createObject,建立Bucket Object-----begin-----");
			// 解析物件
			JSONObject obj = JSONObject.parseObject(objJson);
			if (obj != null && !StringUtils.isEmpty(obj.get("bucketName"))) {
				// 檔案路徑
				String fileUrl = "D:\\test.txt";
				File file = new File(fileUrl);
				if(!file.exists()) {
					return fileUrl + " is not exists!";
				}
				
				// 獲取QingStor物件
				QingStor qingstor = QingStorUtils.getQingStorSingleton();
				
				// 獲取Bucket物件
				Bucket bucket = qingstor.getBucket(String.valueOf(obj.get("bucketName")), QingStorUtils.zone);
				
				// 建立物件
				Bucket.PutObjectInput input = new Bucket.PutObjectInput();
				input.setBodyInputFile(file);
				input.setContentType("text/plain");
				input.setContentLength((Long)file.length());
				Bucket.PutObjectOutput putObjectOutput = bucket.putObject(file.getName(), input);
				
				LOGGER.info("/createObject,建立Bucket Object-----end-----");
				return JSON.toJSONString(putObjectOutput);
			}
		} catch (QSException e) {
			LOGGER.info("/createObject,建立Bucket Object失敗!", e);
		}
		return "Create Object Error!";
	}
	
	/**
     * 查詢青雲QingStor檔案,可以轉換成流
     * @param objJson
     * 請求地址: http://localhost:8080/qingstor/getObjectByFileName
                  引數: {
				"bucketName":"ouyangjun",
				"fileName":"oyj.txt"
			}
     */
	@RequestMapping(value = "/getObjectByFileName", method = RequestMethod.POST)
    public String getFileByte(@RequestBody String objJson) {
		LOGGER.info("/getObjectByFileName,objJson: {}", objJson);
		if (StringUtils.isEmpty(objJson)) {
			return "objJson is null!";
		}
		
        try {
        	// 解析物件
    		JSONObject obj = JSONObject.parseObject(objJson);
    		if (obj != null && !StringUtils.isEmpty(obj.get("bucketName"))) {
    			// 獲取QingStor物件
        		QingStor qingstor = QingStorUtils.getQingStorSingleton();
        		
                Bucket bucket = qingstor.getBucket(String.valueOf(obj.get("bucketName")), QingStorUtils.zone);
                
                Bucket.GetObjectInput headObjectInput = new Bucket.GetObjectInput();
                Bucket.GetObjectOutput out = bucket.getObject(String.valueOf(obj.get("fileName")), headObjectInput);
                
                if (HttpStatus.OK.value() == out.getStatueCode()) {
                    if (Objects.nonNull(out.getBodyInputStream())) {
                    	// org.apache.commons.io.IOUtils.toByteArray(out.getBodyInputStream());
                    	LOGGER.info("BodyInputStream: {}", out.getBodyInputStream());
                    	return "true";
                    }
                }
    		}
            
        } catch (QSException e) {
            LOGGER.error("[青雲Qingstor檔案獲取失敗]", e);
        }
        return "false";
    }
	
	
}

本章完結,待續!

原始碼下載地址: https://gitee.com/ouyangjun_xm/java/attach_files下oysept-qingstor.zip壓縮包

                      碼雲賬戶: [email protected]     密碼: [email protected]

                      請勿惡意操作,謝謝!

本文說明:該文章屬於原創,如需轉載,請標明文章轉載來源