引入相关依赖
1 2 3 4 5 6 7 8 9 10 11 12
| <dependency> <groupId>com.qcloud</groupId> <artifactId>cos_api</artifactId> <version>5.6.89</version> </dependency>
<dependency> <groupId>com.tencent.cloud</groupId> <artifactId>cos-sts-java</artifactId> <version>3.0.5</version> </dependency>
|
代码测试
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
| @Test public void cosTest() { String secretId = "*************************"; String secretKey = "************************"; COSCredentials cred = new BasicCOSCredentials(secretId, secretKey); Region region = new Region("COS_REGION"); ClientConfig clientConfig = new ClientConfig(region); clientConfig.setHttpProtocol(HttpProtocol.https); COSClient cosClient = new COSClient(cred, clientConfig);
List<Bucket> buckets = cosClient.listBuckets(); for (Bucket bucketElement : buckets) { String bucketName = bucketElement.getName(); String bucketLocation = bucketElement.getLocation(); System.out.println("bucketName : "+bucketName); System.out.println("bucketLocation : "+bucketLocation); } }
|
ClientConfig 类为配置信息类,主要的成员如下:
成员名 |
设置方法 |
描述 |
类型 |
region |
构造函数或 set 方法 |
存储桶所在的地域,COS 地域的简称请参见 地域和访问域名 文档 |
Region |
httpProtocol |
set 方法 |
请求所使用的协议,默认使用 HTTPS 协议与 COS 交互 |
HttpProtocol |
signExpired |
set 方法 |
请求签名的有效时间,单位:秒,默认为3600s |
long |
connectionTimeout |
set 方法 |
连接 COS 服务的超时时间,单位:毫秒,默认为30000ms |
int |
socketTimeout |
set 方法 |
客户端读取数据的超时时间,单位:毫秒,默认为30000ms |
int |
httpProxyIp |
set 方法 |
代理服务器的 IP |
String |
httpProxyPort |
set 方法 |
代理服务器的端口 |
int |
启动测试,如果成功打印出相关信息, 说明配置无误。
创建存储桶
1 2 3 4 5 6 7 8 9 10 11
| String bucket = "****************"; CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucket);
createBucketRequest.setCannedAcl(CannedAccessControlList.Private); try{ Bucket bucketResult = cosClient.createBucket(createBucketRequest); } catch (CosServiceException serverException) { serverException.printStackTrace(); } catch (CosClientException clientException) { clientException.printStackTrace(); }
|
上传文件测试
将本地文件或者已知长度的输入流内容上传到 COS,适用于20M以下图片类小文件上传,最大支持上传不超过5GB文件。5GB以上的文件必须使用分块上传或高级 API 接口上传。
- 注意前面地区(Region)初始化的时候名称填入存储桶的地区
- 下面上传文件的key 需要包含完整的文件名字(注意后缀)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @Test public void cosTest() { String localFilePath="C:\\Users\\lenovo\\Pictures\\v2-2eb235a75af128a0374984559350242e_r.jpg"; File localFile = new File(localFilePath); String fileName = localFile.getName(); String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
String bucketName = "typora-1308522872"; String key = "test/"+ UUID.randomUUID().toString()+"."+suffix; PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile); PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest); }
|
获取文件后缀的代码
1 2
| String fileName = localFile.getName(); String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
|
查询对象列表
查询存储桶中对象列表,参考示例如下:
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
| String bucketName = "examplebucket-1250000000"; ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
listObjectsRequest.setBucketName(bucketName);
listObjectsRequest.setPrefix("images/");
listObjectsRequest.setDelimiter("/");
listObjectsRequest.setMaxKeys(1000); ObjectListing objectListing = null; do { try { objectListing = cosClient.listObjects(listObjectsRequest); } catch (CosServiceException e) { e.printStackTrace(); return; } catch (CosClientException e) { e.printStackTrace(); return; } List<String> commonPrefixs = objectListing.getCommonPrefixes();
List<COSObjectSummary> cosObjectSummaries = objectListing.getObjectSummaries(); for (COSObjectSummary cosObjectSummary : cosObjectSummaries) { String key = cosObjectSummary.getKey(); String etag = cosObjectSummary.getETag(); long fileSize = cosObjectSummary.getSize(); String storageClasses = cosObjectSummary.getStorageClass(); }
String nextMarker = objectListing.getNextMarker(); listObjectsRequest.setMarker(nextMarker); } while (objectListing.isTruncated());
|
下载对象
可以用同样的 key,调用 GetObject 接口将对象下载到本地,也可以生成预签名链接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| String bucketName = "examplebucket-1250000000";
String key = "exampleobject";
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key); COSObject cosObject = cosClient.getObject(getObjectRequest); COSObjectInputStream cosObjectInput = cosObject.getObjectContent();
String crc64Ecma = cosObject.getObjectMetadata().getCrc64Ecma();
cosObjectInput.close();
String outputFilePath = "exampleobject"; File downFile = new File(outputFilePath); getObjectRequest = new GetObjectRequest(bucketName, key); ObjectMetadata downObjectMeta = cosClient.getObject(getObjectRequest, downFile);
|
关闭客户端
关闭 cosClient,并释放 HTTP 连接的后台管理线程,代码如下:
示例代码
CosClientUtil
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
| @Component public class CosClientUtil { @Value("${tencent.secretId:#{null}}") private String secretId; @Value("${tencent.secretKey:#{null}}") private String secretKey; @Value("${tencent.region:#{null}}") private String regionName; @Value("${tencent.bucket:#{null}}") private String bucketName; @Value("${tencent.baseUrl:#{null}}") private String baseUrl;
public static final String ANIMAL_FILE="animal/"; public static final String AVATAR_FILE="avatar/"; public static final String TOPIC_FILE="topic/";
public void testValue(){ System.out.println(testValue); }
public COSClient getInstance(){
System.out.println(secretId); System.out.println(secretKey); COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
Region region = new Region(regionName); ClientConfig clientConfig = new ClientConfig(region);
clientConfig.setHttpProtocol(HttpProtocol.https);
COSClient cosClient = new COSClient(cred, clientConfig); return cosClient; }
public String uploadFile(File file,String fileType){ String fileName = file.getName(); String suffix = fileName.substring(fileName.lastIndexOf(".") + 1); COSClient cosClient = getInstance();
String key = fileType+ UUID.randomUUID().toString()+"."+suffix; PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file); cosClient.putObject(putObjectRequest); String url= baseUrl+ key; return url; } }
|
小插曲
@Value用法
为了避免信息泄露使用配置文件赋值时, 总是赋值失败
1 2 3 4 5 6 7 8 9 10
| @Value("${tencent.secretId:#{null}}") private String secretId; @Value("${tencent.secretKey:#{null}}") private String secretKey; @Value("${tencent.region:#{null}}") private String regionName; @Value("${tencent.bucket:#{null}}") private String bucketName; @Value("${tencent.baseUrl:#{null}}") private String baseUrl;
|
后来百度才知道@Value
的一些使用细节
- 不能作用于静态变量(static);
- 不能作用于常量(final);
- 不能在非注册的类中使用
- 类需要被注册在spring上下文中,如用@Service,@RestController,@Component等
- 使用这个类时,只能通过依赖注入的方式,用new的方式是不会自动注入这些配置的。
如果yaml文件报错, 不用在意 , 实际执行的时候还是可以读取到的