-
Notifications
You must be signed in to change notification settings - Fork 142
/
GetObjectDemo.java
92 lines (80 loc) · 3.26 KB
/
GetObjectDemo.java
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
package com.qcloud.cos.demo;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.utils.IOUtils;
public class GetObjectDemo {
private static String secretId = System.getenv("SECRETID");
private static String secretKey = System.getenv("SECRETKEY");
private static String bucketName = System.getenv("BUCKET_NAME");
private static String region = System.getenv("REGION");
private static COSClient cosClient = createClient();
public static void main(String[] args) {
try {
getObjectToFileDemo();
//getObjectInputStreamDemo();
} catch (Exception e) {
e.printStackTrace();
} finally {
cosClient.shutdown();
}
}
private static COSClient createClient() {
// 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials(secretId,secretKey);
// 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region(region));
// 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
return cosclient;
}
private static void getObjectToFileDemo() {
String key = "test/my_test.json";
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
// 设置下载的单链接限速(如有需要),不需要可忽略
getObjectRequest.setTrafficLimit(8*1024*1024);
try {
File localFile = new File("my_test.json");
ObjectMetadata objectMetadata = cosClient.getObject(getObjectRequest, localFile);
System.out.println(objectMetadata.getContentLength());
} catch (CosServiceException cse) {
cse.printStackTrace();
} catch (CosClientException cce) {
cce.printStackTrace();
}
}
private static void getObjectInputStreamDemo() throws IOException {
String key = "test/my_test.json";
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
InputStream cosObjectInput = null;
try {
COSObject cosObject = cosClient.getObject(getObjectRequest);
cosObjectInput = cosObject.getObjectContent();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
}
// 处理下载到的流
// 这里是直接读取,按实际情况来处理
byte[] bytes = null;
try {
bytes = IOUtils.toByteArray(cosObjectInput);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 用完流之后一定要调用 close()
cosObjectInput.close();
}
}
}