BlueXIII's Blog

热爱技术,持续学习

0%

阿里云OSS对象存储示例

DEMO GitLab

更多源码请移步GitHub
https://github.com/xiiiblue/demo-oss

OSS SDK下载

https://promotion.aliyun.com/ntms/act/ossdoclist.html

OSS SDK源码

https://github.com/aliyun/aliyun-oss-java-sdk

OSS Maven依赖

1
2
3
4
5
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.1</version>
</dependency>

OSS官方文档

https://help.aliyun.com/document_detail/32009.html

申请及配置OSS流程

  1. 登录aliyun后,进入OSS控制台:
    https://oss.console.aliyun.com/overview

  2. 右侧点击”购买资源包”,选择合适的档位,付费购买。

  3. 右侧点击”新建Bucket”,输入一个全局唯一的bucket名,并选择所属地域,新建Bucket。

  4. 左侧列表中,进入新建的Bucket,可以查询到”Endpoint”

  5. 获取AK,可以直接访问链接:
    https://ak-console.aliyun.com/#/accesskey

常用操作示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
private Logger logger = LoggerFactory.getLogger(OssComponent.class);
@Value("${aliyun.oss.endpoint}")
private String endpoint;
@Autowired
private OSSClient ossClient;


/**
* 通过bucket与fileKey获取URL
*
* @param bucket
* @param fileKey
* @return
*/
public String urlFromFileKey(String bucket, String fileKey) {
return "http://" + bucket + "." + this.endpoint + "/" + fileKey;
}

/**
* 上传本地文件
*
* @param bucket Bucket
* @param filePath 本地文件路径
* @param fileKey 指定OSS文件名,传空时自动生成UUID
* @return
* @throws FileNotFoundException
*/
public String putObject(String bucket, String filePath, String fileKey) throws FileNotFoundException {
if (fileKey == null) {
fileKey = UUID.randomUUID().toString().replaceAll("-", "") + filePath.substring(filePath.lastIndexOf("."));
}

ossClient.putObject(bucket, fileKey, new File(filePath));

logger.info("OSS putObject success! fileKey={}", fileKey);
return fileKey;
}

/**
* 下载文件到本地目录
*
* @param bucket Bucket
* @param fileKey OSS文件名
* @param localPath 本地文件目录
* @return
*/
public String getObject(String bucket, String fileKey, String localPath) {
String localFileKey = localPath + fileKey;
ossClient.getObject(new GetObjectRequest(bucket, fileKey), new File(localFileKey));

logger.info("OSS getObject success! localFileKey={}", localFileKey);
return localFileKey;
}


/**
* 检查文件是否存在
*
* @param bucket Bucket
* @param fileKey OSS文件名
* @return
*/
public boolean checkObject(String bucket, String fileKey) {
return ossClient.doesObjectExist(bucket, fileKey);
}


/**
* 列出所有文件
*
* @param bucket Bucket
* @param keyPrifix 文件名前缀
*/
public List<String> listObjects(String bucket, String keyPrifix) {
List<String> fileList = new ArrayList<>();

ObjectListing objectListing = ossClient.listObjects(bucket, keyPrifix);
List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
fileList.add(bucket);
}

return fileList;
}

/**
* 删除文件
*
* @param bucket
* @param fileKey
*/
public void deleteObject(String bucket, String fileKey) {
ossClient.deleteObject(bucket, fileKey);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
private Logger logger = LoggerFactory.getLogger(OssComponentTest.class);
private String localPath;
@Autowired
OssComponent ossComponent;

public OssComponentTest() {
localPath = System.getProperty("user.dir") + "/" + "upload/";
}


@Test
public void urlFromFileKey() throws Exception {
String bucket = "foobar";
String fileKey = "0c2eb1357a454c71b71aa20462951ac4.jpg";
String url = ossComponent.urlFromFileKey(bucket, fileKey);

logger.debug(url);
assertNotNull(url);
}

@Test
public void putObject() throws Exception {
String bucket = "foobar";
String filePath = localPath + "sonic.jpg";

String fileKey = ossComponent.putObject(bucket, filePath, null);
logger.debug(fileKey);
assertNotNull(fileKey);

fileKey = ossComponent.putObject(bucket, filePath, "sonic.jpg");
logger.debug(fileKey);
assertNotNull(fileKey);
}

@Test
public void getObject() throws Exception {
String bucket = "foobar";
String fileKey = "0c2eb1357a454c71b71aa20462951ac4.jpg";
String localPath = this.localPath;

String localFileKey = ossComponent.getObject(bucket, fileKey, localPath);

logger.debug(localFileKey);
assertNotNull(localFileKey);
}

@Test
public void checkObject() throws Exception {
String bucket = "foobar";
String fileKey = "0c2eb1357a454c71b71aa20462951ac4.jpg";

boolean flag = ossComponent.checkObject(bucket, fileKey);

assertTrue(flag);
}

@Test
public void listObjects() throws Exception {
String bucket = "foobar";
String keyPrifix = "";

List<String> list = ossComponent.listObjects(bucket, keyPrifix);

logger.debug(list.toString());
assertTrue(list.size() > 0);
}

@Test
public void deleteObject() throws Exception {
String bucket = "foobar";
String fileKey = "sonic.jpg";

ossComponent.deleteObject(bucket, fileKey);
}