Browse Source

@IPU_REQ_2021@weihf@添加ipu-s3示例

weihf 4 years ago
parent
commit
881635a2d0

+ 10 - 1
ipu-rest-demo/pom.xml

@ -105,7 +105,16 @@
105 105
			<groupId>com.ai.ipu.server</groupId>
106 106
			<artifactId>ipu-restful</artifactId>
107 107
		</dependency>
108
		
108
		<dependency>
109
		    <groupId>com.ai.ipu</groupId>
110
		    <artifactId>ipu-s3</artifactId>
111
		    <version>${ipu}</version>
112
		</dependency>
113
		<dependency>
114
		    <groupId>com.google.guava</groupId>
115
		    <artifactId>guava</artifactId>
116
		    <version>29.0-jre</version>
117
		</dependency>
109 118
        <!-- 这里引入mongo-java-driver的原因是springboot1.5.9缺省使用了mongo-java-driver低版本的驱动,
110 119
                                导致ipu-nosql里MongoCredential.createScramSha256Credential报“方法不存在”。
111 120
                               由于ipu-rest-libs里引入springboot采用了import方式;

+ 132 - 0
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/control/dfs/MinIOController.java

@ -0,0 +1,132 @@
1
package com.ai.ipu.server.demo.control.dfs;
2

3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.stereotype.Controller;
5
import org.springframework.web.bind.annotation.RequestMapping;
6
import org.springframework.web.bind.annotation.ResponseBody;
7

8
import com.ai.ipu.data.JMap;
9
import com.ai.ipu.data.impl.JsonMap;
10
import com.ai.ipu.server.demo.service.MinIOServiceImpl;
11

12
/**
13
 * 基于ipu-s3组件对MinIO操作范例
14
 * @author weihf@asiainfo.com
15
 * @team IPU
16
 * @date 2021年3月3日
17
 */
18
@Controller
19
@RequestMapping("/minio/")
20
public class MinIOController {
21
	private static final String DEFAULT_DFS_NAME = "test";
22
	private static final String DEFAULT_BUCKET_NAME = "test";
23
	private static final String LOCAL_FILE_PATH = "/data";
24
	private static final int DEFAULT_EXPIRE = 300;
25
	
26
	@Autowired
27
	private MinIOServiceImpl service;
28
	
29
	@ResponseBody
30
    @RequestMapping("/createBucket")
31
    public JMap createBucket(JMap param) throws Exception {
32
		String bucketName = param.getString("bucketName", DEFAULT_BUCKET_NAME);
33
		JMap result = new JsonMap();
34
		result.put("result", service.createBucket(DEFAULT_DFS_NAME, bucketName));
35
		return result;
36
	}
37
	
38
	@ResponseBody
39
    @RequestMapping("/deleteBucketWithoutToken")
40
    public JMap deleteBucketWithoutToken(JMap param) throws Exception {
41
		String bucketName = param.getString("bucketName", DEFAULT_BUCKET_NAME);
42
		JMap result = new JsonMap();
43
		result.put("result", service.deleteBucketWithoutToken(DEFAULT_DFS_NAME, bucketName));
44
		return result;
45
	}
46
	
47
	@ResponseBody
48
    @RequestMapping("/deleteBucketWithToken")
49
    public JMap deleteBucketWithToken(JMap param) throws Exception {
50
		String bucketName = param.getString("bucketName", DEFAULT_BUCKET_NAME);
51
		JMap result = new JsonMap();
52
		result.put("result", service.deleteBucketWithToken(DEFAULT_DFS_NAME, bucketName));
53
		return result;
54
	}
55

56
	@ResponseBody
57
    @RequestMapping("/deleteFileWithoutToken")
58
    public JMap deleteFileWithoutToken(JMap param) throws Exception {
59
		String bucketName = param.getString("bucketName", DEFAULT_BUCKET_NAME);
60
		String minioLabel = param.getString("minioLabel");
61
		JMap result = new JsonMap();
62
		result.put("result", service.deleteFileWithoutToken(DEFAULT_DFS_NAME, bucketName, minioLabel));
63
		return result;
64
	}
65
	
66
	@ResponseBody
67
    @RequestMapping("/deleteFileWithToken")
68
    public JMap deleteFileWithToken(JMap param) throws Exception {
69
		String bucketName = param.getString("bucketName", DEFAULT_BUCKET_NAME);
70
		String minioLabel = param.getString("minioLabel");
71
		JMap result = new JsonMap();
72
		result.put("result", service.deleteFileWithToken(DEFAULT_DFS_NAME, bucketName, minioLabel));
73
		return result;
74
	}
75
	
76
	@ResponseBody
77
    @RequestMapping("/downloadFile")
78
    public JMap downloadFile(JMap param) throws Exception {
79
		String bucketName = param.getString("bucketName", DEFAULT_BUCKET_NAME);
80
		String minioLabel = param.getString("minioLabel");
81
		JMap result = new JsonMap();
82
		result.put("result", service.downloadFile(DEFAULT_DFS_NAME, bucketName, minioLabel, LOCAL_FILE_PATH + "/" + minioLabel));
83
		return result;
84
	}
85
	
86
	@ResponseBody
87
    @RequestMapping("/getAllBuckets")
88
    public JMap getAllBuckets(JMap param) throws Exception {
89
		JMap result = new JsonMap();
90
		result.put("result", service.getAllBuckets(DEFAULT_DFS_NAME));
91
		return result;
92
	}
93
	
94
	@ResponseBody
95
    @RequestMapping("/getAllFileByPrefix")
96
    public JMap getAllFileByPrefix(JMap param) throws Exception {
97
		String bucketName = param.getString("bucketName", DEFAULT_BUCKET_NAME);
98
		JMap result = new JsonMap();
99
		result.put("result", service.getAllByPrefix(DEFAULT_DFS_NAME, bucketName));
100
		return result;
101
	}
102
	
103
	@ResponseBody
104
    @RequestMapping("/getDownloadUrl")
105
    public JMap getDownloadUrl(JMap param) throws Exception {
106
		String bucketName = param.getString("bucketName", DEFAULT_BUCKET_NAME);
107
		String minioLabel = param.getString("minioLabel");
108
		int expire = param.getInt("expire", DEFAULT_EXPIRE);
109
		JMap result = new JsonMap();
110
		result.put("result", service.getDownloadUrl(DEFAULT_DFS_NAME, bucketName, minioLabel, expire));
111
		return result;
112
	}
113
	
114
	@ResponseBody
115
    @RequestMapping("/isBucketExist")
116
    public JMap isBucketExist(JMap param) throws Exception {
117
		String bucketName = param.getString("bucketName", DEFAULT_BUCKET_NAME);
118
		JMap result = new JsonMap();
119
		result.put("result", service.isBucketExist(DEFAULT_DFS_NAME, bucketName));
120
		return result;
121
	}
122
	
123
	@ResponseBody
124
    @RequestMapping("/uploadFile")
125
    public JMap uploadFile(JMap param) throws Exception {
126
		String bucketName = param.getString("bucketName", DEFAULT_BUCKET_NAME);
127
		String localFile = LOCAL_FILE_PATH + "/2.txt";
128
		JMap result = new JsonMap();
129
		result.put("result", service.uploadFile(DEFAULT_DFS_NAME, bucketName, localFile, "text/plain"));
130
		return result;
131
	}
132
}

+ 165 - 0
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/service/MinIOServiceImpl.java

@ -0,0 +1,165 @@
1
package com.ai.ipu.server.demo.service;
2

3
import java.io.File;
4
import java.util.ArrayList;
5
import java.util.List;
6

7
import org.springframework.stereotype.Service;
8

9
import com.ai.ipu.basic.log.ILogger;
10
import com.ai.ipu.basic.log.IpuLoggerFactory;
11
import com.ai.ipu.dfs.s3.IFs;
12
import com.ai.ipu.dfs.s3.IpuDfsFactory;
13
import com.ai.ipu.dfs.s3.entity.BucketEntity;
14
import com.ai.ipu.dfs.s3.entity.DfsEntity;
15

16
import io.minio.http.Method;
17
import io.minio.messages.Bucket;
18
import io.minio.messages.Item;
19

20
@Service
21
public class MinIOServiceImpl {
22
	private ILogger log = IpuLoggerFactory.createLogger(MinIOServiceImpl.class);
23
	private static final String DELETE_TOKEN = "minio";
24
	
25
	public boolean isBucketExist(String dfsName, String bucketName) throws Exception {
26
		DfsEntity dfsEntity = IpuDfsFactory.getDfsEntity(dfsName);
27
		BucketEntity bucket = new BucketEntity();
28
		bucket.setDfsName(dfsEntity.getDfsName());
29
		bucket.setBucketName(bucketName);
30
		bucket.setRegionName(dfsEntity.getRegionName());
31
		IFs client = IpuDfsFactory.getFsClient(dfsEntity.getDfsName());
32
		return client.isBucketExist(bucket);
33
	}
34
	
35

36
	public boolean createBucket(String dfsName, String bucketName) throws Exception {
37
		DfsEntity dfsEntity = IpuDfsFactory.getDfsEntity(dfsName);
38
		BucketEntity bucket = new BucketEntity();
39
		bucket.setDfsName(dfsEntity.getDfsName());
40
		bucket.setBucketName(bucketName);
41
		bucket.setRegionName(dfsEntity.getRegionName());
42
		IFs client = IpuDfsFactory.getFsClient(dfsEntity.getDfsName());
43
		return client.createBucket(bucket);
44
	}
45
	
46

47
	public String getDownloadUrl(String dfsName, String bucketName, String minioLabel, int expire) throws Exception {
48
		DfsEntity dfsEntity = IpuDfsFactory.getDfsEntity(dfsName);
49
		BucketEntity bucket = new BucketEntity();
50
		bucket.setDfsName(dfsEntity.getDfsName());
51
		bucket.setBucketName(bucketName);
52
		bucket.setRegionName(dfsEntity.getRegionName());
53
		IFs client = IpuDfsFactory.getFsClient(dfsEntity.getDfsName());
54
		
55
		/* 生成临时下载链接。 */
56
		return client.getDownloadUrl(bucket, Method.GET, minioLabel, expire);
57
	}
58
		
59

60
	public boolean downloadFile(String dfsName, String bucketName, String minioLabel, String localFile) throws Exception {
61
		DfsEntity dfsEntity = IpuDfsFactory.getDfsEntity(dfsName);
62
		BucketEntity bucket = new BucketEntity();
63
		bucket.setDfsName(dfsEntity.getDfsName());
64
		bucket.setBucketName(bucketName);
65
		bucket.setRegionName(dfsEntity.getRegionName());
66
		IFs client = IpuDfsFactory.getFsClient(dfsEntity.getDfsName());
67
		return client.downloadFile(bucket, minioLabel, localFile);
68
	}
69
	
70

71
	public boolean deleteFileWithoutToken(String dfsName, String bucketName, String minioLabel) throws Exception {
72
		DfsEntity dfsEntity = IpuDfsFactory.getDfsEntity(dfsName);
73
		BucketEntity bucket = new BucketEntity();
74
		bucket.setDfsName(dfsEntity.getDfsName());
75
		bucket.setBucketName(bucketName);
76
		bucket.setRegionName(dfsEntity.getRegionName());
77
		IFs client = IpuDfsFactory.getFsClient(dfsEntity.getDfsName());
78
		//由于deletetoken错误,删除会抛异常
79
		return client.deleteFile(bucket, minioLabel, null);
80
	}
81
	
82

83
	public boolean deleteFileWithToken(String dfsName, String bucketName, String minioLabel) throws Exception {
84
		DfsEntity dfsEntity = IpuDfsFactory.getDfsEntity(dfsName);
85
		BucketEntity bucket = new BucketEntity();
86
		bucket.setDfsName(dfsEntity.getDfsName());
87
		bucket.setBucketName(bucketName);
88
		bucket.setRegionName(dfsEntity.getRegionName());
89
		IFs client = IpuDfsFactory.getFsClient(dfsEntity.getDfsName());
90
		return client.deleteFile(bucket, minioLabel, DELETE_TOKEN);
91
	}
92
	
93

94
	public boolean deleteBucketWithoutToken(String dfsName, String bucketName) throws Exception {
95
		DfsEntity dfsEntity = IpuDfsFactory.getDfsEntity(dfsName);
96
		BucketEntity bucket = new BucketEntity();
97
		bucket.setDfsName(dfsEntity.getDfsName());
98
		bucket.setBucketName(bucketName);
99
		bucket.setRegionName(dfsEntity.getRegionName());
100
		IFs client = IpuDfsFactory.getFsClient(dfsEntity.getDfsName());
101
		return client.deleteBucket(bucket, null);
102
	}
103
	
104

105
	public boolean deleteBucketWithToken(String dfsName, String bucketName) throws Exception {
106
		DfsEntity dfsEntity = IpuDfsFactory.getDfsEntity(dfsName);
107
		BucketEntity bucket = new BucketEntity();
108
		bucket.setDfsName(dfsEntity.getDfsName());
109
		bucket.setBucketName(bucketName);
110
		bucket.setRegionName(dfsEntity.getRegionName());
111
		IFs client = IpuDfsFactory.getFsClient(dfsEntity.getDfsName());
112
		return client.deleteBucket(bucket, DELETE_TOKEN);
113
	}
114
	
115

116
	public boolean uploadFile(String dfsName, String bucketName, String localFile, String contentType) throws Exception {
117
		DfsEntity dfsEntity = IpuDfsFactory.getDfsEntity(dfsName);
118
		BucketEntity bucket = new BucketEntity();
119
		bucket.setDfsName(dfsEntity.getDfsName());
120
		bucket.setBucketName(bucketName);
121
		bucket.setRegionName(dfsEntity.getRegionName());
122
		
123
		File tmpFile=new File(localFile);
124
        String minioLabel=tmpFile.getName();
125
		IFs client = IpuDfsFactory.getFsClient(dfsEntity.getDfsName());
126
		return client.uploadFile(bucket, localFile, minioLabel, contentType);
127
	}
128
	
129

130
	public List<String> getAllByPrefix(String dfsName, String bucketName) throws Exception {
131
		DfsEntity dfsEntity = IpuDfsFactory.getDfsEntity(dfsName);
132
		BucketEntity bucket = new BucketEntity();
133
		bucket.setDfsName(dfsEntity.getDfsName());
134
		bucket.setBucketName(bucketName);
135
		bucket.setRegionName(dfsEntity.getRegionName());
136
		IFs client = IpuDfsFactory.getFsClient(dfsEntity.getDfsName());
137
		List<Item> files = client.getAllByPrefix(bucket, null, 10, false);
138
		int i=0;
139
		List<String> results = new ArrayList<String>();
140
		log.debug("filelist results:");
141
		for (;i<files.size();i++) {
142
			log.debug(files.get(i).objectName());
143
			results.add(files.get(i).objectName());
144
		}
145
		return results;
146
	}
147
	
148

149
	public List<String> getAllBuckets(String dfsName) throws Exception {
150
		DfsEntity dfsEntity = IpuDfsFactory.getDfsEntity(dfsName);
151
		BucketEntity bucket = new BucketEntity();
152
		bucket.setDfsName(dfsEntity.getDfsName());
153
		IFs client = IpuDfsFactory.getFsClient(dfsEntity.getDfsName());
154
		List<Bucket> buckets = client.getAllBuckets(bucket);
155
		int i=0;
156
		List<String> results = new ArrayList<String>();
157
		log.debug("buckets results:");
158
		for (;i<buckets.size();i++) {
159
			log.debug(buckets.get(i).name());
160
			results.add(buckets.get(i).name());
161
		}
162
		return results;
163
	}
164
	
165
}

+ 14 - 0
ipu-rest-demo/src/main/resources/dev/ipu-s3.xml

@ -0,0 +1,14 @@
1
<?xml version = '1.0' encoding = 'UTF-8'?>
2
<dfs>
3
	<fs name="test" type="MinIO">
4
        <config name="DfsUrl" value="http://47.105.160.21:9000"/>
5
        <config name="AccessKey" value="@DES@RSzkZ6hmDVI="/>
6
        <config name="SecretKey" value="@DES@ObffDdlirVFZLdcs2DV3cLnXvnxZVc1y"/>
7
        <config name="RegionName" value="china-beijing"/>  
8
        <config name="Version" value=""/>
9
        <config name="DeleteToken" value="@DES@/T/mbyc9wik="/>
10
        <!-- 是否忽略ssl证书校验,缺省为false,表示需要 校验。此时非CA颁发证书需要通过jvm参数-Dipu.s3.cert=/证书目录/证书设置证书
11
        true表示不需要校验证书。 -->
12
        <config name="Insecure" value="true"/>
13
	</fs>
14
</dfs>