package com.jc.chile.booksgrab.utils;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.*;
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.region.Region;
import java.io.*;
/**
* @Description 腾讯云存储
* @Author liangzhiqing
* @Date 2019/10/9 9:56
*/
public class CosConstants {
//用户身份信息
private final static String SECRET_ID = "";
private final static String SECRET_KEY = "";
//bucket 区域
private final static String BUCKET_REGION = "";
//存储桶
private final static String BUCKET_NAME = "";
private COSClient cosClient;
public CosConstants(){
COSCredentials credentials = new BasicCOSCredentials(SECRET_ID, SECRET_KEY);
//设置区域
Region region = new Region(BUCKET_REGION);
ClientConfig clientConfig = new ClientConfig(region);
//生成COS客户端
cosClient = new COSClient(credentials, clientConfig);
}
//上传不超过5G的文件
//key:上传到COS的键,例如:key = images/picture.jpg 上传后访问地址是:xxxxx/images/picture.jpg
public void uploadSmallFile(String key, File file){
try {
PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, key, file);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
} catch (CosServiceException serverException){
serverException.printStackTrace();
} catch (CosClientException clientException) {
clientException.printStackTrace();
} finally {
closeCosClient();
}
}
//输入流上传
public void uploadSmallFile(String key, InputStream inputStream){
try {
ObjectMetadata objectMetadata = new ObjectMetadata();
//设置输入流长度为500
objectMetadata.setContentLength(inputStream.available());
// 设置 Content type, 默认是 application/octet-stream
//objectMetadata.setContentType("application/pdf");
PutObjectResult putObjectResult = cosClient.putObject(BUCKET_NAME, key, inputStream, objectMetadata);
String etag = putObjectResult.getETag();
} catch (CosServiceException serverException){
serverException.printStackTrace();
} catch (CosClientException clientException) {
clientException.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
closeCosClient();
}
}
//读取COS文件内容
public String readerFileContent(String key){
String content = null;
BufferedReader reader = null;
try {
//获取下载输入流
GetObjectRequest getObjectRequest = new GetObjectRequest(BUCKET_NAME, key);
COSObject cosObject = cosClient.getObject(getObjectRequest);
COSObjectInputStream cosObjectInputStream = cosObject.getObjectContent();
reader = new BufferedReader(new InputStreamReader(cosObjectInputStream));
byte[] buf = new byte[1024];
String line = null;
while ((line = reader.readLine()) != null){
content += line;
}
} catch (Exception e){
e.printStackTrace();
} finally {
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//关闭cosClient
closeCosClient();
}
return content;
}
public static void main(String[] arts){
CosConstants cosConstants = new CosConstants();
String content = cosConstants.readerFileContent("novel/万古术皇雕龙画凤/第七百零四章暂时结盟 巧骗蝼蚁.txt");
System.out.println(content);
}
//关闭cosClient
private void closeCosClient(){
this.cosClient.shutdown();
}
}