package com.yeshi.buwan.util;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.util.List;
|
|
import com.qcloud.cos.model.COSObjectSummary;
|
import com.qcloud.cos.model.ObjectListing;
|
import org.apache.commons.codec.digest.HmacUtils;
|
import org.apache.commons.lang.ArrayUtils;
|
|
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.model.PutObjectRequest;
|
import com.qcloud.cos.region.Region;
|
|
public class COSManager {
|
static long appId = 1255749512L;
|
static String secretId = "AKIDTlpgJhLjOozvd6QI2XnpfGbgV4NQJk25";
|
static String secretKey = "xhCSUHo55oHUQ6XicFcmfIgspX0EEzWo";
|
// 设置要操作的bucket
|
static String bucketName = "buwan";
|
static COSClient cosClient;
|
static String totalBucketName = "";
|
|
static {
|
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
|
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
|
cosClient = new COSClient(cred, clientConfig);
|
// bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
|
totalBucketName = bucketName + "-" + appId;
|
|
|
}
|
|
static COSManager instance;
|
|
public static COSManager getInstance() {
|
if (instance == null)
|
instance = new COSManager();
|
return instance;
|
}
|
|
public String uploadFile(File f, String key) {
|
if (key != null && !key.startsWith("/"))
|
key = "/" + key;
|
PutObjectRequest putObjectRequest = new PutObjectRequest(totalBucketName, key, f);
|
try {
|
cosClient.putObject(putObjectRequest);
|
return String.format("http://%s.file.myqcloud.com%s", totalBucketName, key);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
public String uploadFile(InputStream inputStream, String key) {
|
if (key != null && !key.startsWith("/"))
|
key = "/" + key;
|
String dir = FileUtil.getCacheDir();
|
String tempPath = dir + "/temp_" + System.currentTimeMillis() + "_" + (long) (Math.random() * 100000000);
|
try {
|
|
try {
|
FileUtil.saveAsFile(inputStream, tempPath);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
if (new File(tempPath).exists()) {
|
return uploadFile(new File(tempPath), key);
|
}
|
return null;
|
} finally {
|
if (new File(tempPath).exists())
|
new File(tempPath).delete();
|
}
|
}
|
|
// 文件是否存在
|
public boolean isFileExist(String url) {
|
String key = url.replace(String.format("https://%s.file.myqcloud.com", totalBucketName), "");
|
try {
|
cosClient.getObjectMetadata(totalBucketName, key);
|
return true;
|
} catch (Exception e) {
|
}
|
return false;
|
}
|
|
public boolean deleteFile(String url) {
|
String key = url.replace(String.format("https://%s.file.myqcloud.com", totalBucketName), "");
|
try {
|
cosClient.deleteObject(totalBucketName, key);
|
return true;
|
} catch (Exception e) {
|
}
|
return false;
|
}
|
|
public static String getUploadVideoUrl(String name) {
|
if (name != null && !name.startsWith("/"))
|
name = "/" + name;
|
return "https://gz.file.myqcloud.com/files/v2/" + appId + "/" + bucketName + name;
|
}
|
|
public static String getUploadVideoFileId(String name) {
|
if (name != null && !name.startsWith("/"))
|
name = "/" + name;
|
return "/" + appId + "/" + bucketName + name;
|
}
|
|
public static String getUploadVideoAuthorization() {
|
long time = System.currentTimeMillis() / 1000;
|
long expireTime = time + 60 * 20;// 20分钟的上传时间
|
long random = (long) (1000000L * Math.random());
|
String original = String.format("a=%s&b=%s&k=%s&e=%s&t=%s&r=%s&f=", appId + "", bucketName, secretId,
|
(expireTime) + "", time + "", random + "");
|
byte[] so = HmacUtils.hmacSha1(secretKey, original);
|
byte[] or = original.getBytes();
|
byte[] bts = ArrayUtils.addAll(so, or);
|
String sign = StringUtil.getBase64FromByte(bts).replace("\n", "").replace("\r", "").trim();
|
return sign;
|
}
|
|
public static List<COSObjectSummary> listObject(String bucketName, String prefix){
|
ObjectListing result = cosClient.listObjects(bucketName,prefix);
|
return result.getObjectSummaries();
|
}
|
|
}
|