腾讯云对象存储cos使用
一、新建密钥
腾讯云存储桶的javaSdk的API参考文档地址:链接: 官方文档.
1、登录腾讯云进入控制台
2、新建密钥
二、创建对象存储
1.进入对象存储,并创建存储桶
三、创建项目
pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!--腾讯云cos依赖包-->
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.28</version>
</dependency>
</dependencies>
yml文件
server:
port: 8000
spring:
application:
name: costest
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
创建一util层
public class CosTestUtil {
// 1 初始化用户身份信息(secretId, secretKey)*这里是第一步新建密钥的
public static COSCredentials cred =
new BasicCOSCredentials("secretId"
, "secretKey");
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
public static ClientConfig clientConfig = new ClientConfig(new Region("创建时选择的COS地域"));
// 3 生成cos客户端
public static COSClient cosClient = new COSClient(cred,clientConfig);
// bucket名需包含appid
public static String bucketName = "存储桶名称";
/**
* 上传文件
*/
public static void upFile(InputStream inputStream,String key){
//指定要上传到的存储桶
ObjectMetadata Metadata = new ObjectMetadata();
PutObjectRequest request = new PutObjectRequest(bucketName, key, inputStream, Metadata);
cosClient.putObject(request);
}
/**
* 下载文件
*/
public static void downFile(){
// 指定对象在 COS 上的对象键
String key = "";//存储桶下文件名
//指定要下载到的本地路径
File downFile = new File("D:/test/"+key);
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
cosClient.getObject(getObjectRequest,downFile);
}
/**
* 删除文件
*/
public static void delFile(){
String key="";//存储桶下文件名
cosClient.deleteObject(bucketName,key);
}
/**
* 改存储桶已经在前端界面进行创建 创建存储桶测试成功
*/
public static void createCosBucket() {
String bucket = "examplebucket-1305834615";
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucket);
// 设置 bucket 的权限为 PublicRead(公有读私有写), 其他可选有私有读写, 公有读写
createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
cosClient.createBucket(createBucketRequest);
}
/**
* 查询存储桶列表 已经测试、成功
*
* @return
*/
public static List<Bucket> queryBucketList() {
List<Bucket> buckets = null;
try {
buckets = cosClient.listBuckets();
System.out.println(buckets);
} catch (CosServiceException serverException) {
serverException.printStackTrace();
} catch (CosClientException clientException) {
clientException.printStackTrace();
}
return buckets;
}
/**
* 查询存储桶中内容(需要改1处位置)
* @param
*/
public static ObjectListing getContent() {
ObjectListing objectListing = null;
try {
// 桶容器的名称(需要改1)
String bucket = bucketName;
ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
// 设置 bucket 名称
listObjectsRequest.setBucketName(bucket);
// prefix 表示列出的 object 的 key 以 prefix 开始
listObjectsRequest.setPrefix("");
// 设置最大遍历出多少个对象, 一次 listobject 最大支持1000
listObjectsRequest.setMaxKeys(1000);
listObjectsRequest.setDelimiter("/");
objectListing =cosClient.listObjects(listObjectsRequest);
/*
* for (COSObjectSummary cosObjectSummary : objectListing.getObjectSummaries())
* { // 对象的路径 key String key = cosObjectSummary.getKey(); // 对象的 etag String
* etag = cosObjectSummary.getETag(); // 对象的长度 long fileSize =
* cosObjectSummary.getSize(); // 对象的存储类型 String storageClass =
* cosObjectSummary.getStorageClass(); System.out.println("key:" + key +
* "; etag:" + etag + "; fileSize:" + fileSize + "; storageClass:" +
* storageClass); }
*/
} catch (CosServiceException serverException) {
serverException.printStackTrace();
} catch (CosClientException clientException) {
clientException.printStackTrace();
}
return objectListing;
}
}
controller层
@RequestMapping("file")
@RestController
public class CosTestController {
@RequestMapping("/upload")
public String upFile(@RequestParam("file")MultipartFile file) {
try {
CosTestUtil.upFile(file.getInputStream(),file.getOriginalFilename());
} catch (IOException e) {
e.printStackTrace();
return "fail";
}
return "success";
}
/**
* 改存储桶已经在前端界面进行创建 创建存储桶测试成功
*/
@RequestMapping("/create")
public String createCosBucket(){
try {
CosTestUtil.createCosBucket();
} catch (Exception e) {
e.printStackTrace();
return "fail";
}
return "sucess";
}
@RequestMapping("/download")
public String downFile(){
try {
CosTestUtil.downFile();
} catch (Exception e) {
e.printStackTrace();
return "fail";
}
return "success";
}
@RequestMapping("/delFile")
public String delFile() {
try {
CosTestUtil.delFile();
} catch (Exception e) {
e.printStackTrace();
return "fail";
}
return "success";
}
/**
* 查询存储桶列表 已经测试、成功
*
* @return
*/
@RequestMapping("/query")
public List<Bucket> queryBucketList(){
List<Bucket> buckets = CosTestUtil.queryBucketList();
return buckets;
}
/**
* 查询存储桶中内容(需要改1处位置)
* @param
*/
@RequestMapping("/get")
public ObjectListing getContent() {
return CosTestUtil.getContent();
}
}
页面显示
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>文件上传</h1>
<form action="/file/upload" method="post" enctype="multipart/form-data" >
文件上传:<input type="file" name="file" ><br/>
<input type="submit" value="点击上传文件" ><br/>
</form>
<h1>文件下载</h1>
<a href="/file/download">下载</a>
</body>
</html>
四、测试
1、上传下载
浏览器输入:localhost:8000/index.html,点击上传文件响应
上传成功!!!
点击下载超链接
查看本地路径
下载成功!!!
2、删除文件
浏览器输入:localhost:8000/file/delFile
查看存储桶
删除成功!!!
3、查询存储桶,创建存储桶
浏览器输入:localhost:8000/file/create
注意: 这里的AppId是创建时的第一步创建密钥时的appid
创建成功!!!
浏览器输入:localhost:8000/file/query
查询成功!!!
4、查询存储桶中内容
浏览器输入:localhost:8000/file/get
查询成功!!!