|
@@ -0,0 +1,249 @@
|
|
|
+package com.hssx.cloudmodel.util;
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+import com.qcloud.cos.COSClient;
|
|
|
+import com.qcloud.cos.ClientConfig;
|
|
|
+import com.qcloud.cos.auth.BasicCOSCredentials;
|
|
|
+import com.qcloud.cos.auth.BasicSessionCredentials;
|
|
|
+import com.qcloud.cos.auth.COSCredentials;
|
|
|
+import com.qcloud.cos.exception.CosClientException;
|
|
|
+import com.qcloud.cos.exception.CosServiceException;
|
|
|
+import com.qcloud.cos.model.*;
|
|
|
+import com.qcloud.cos.region.Region;
|
|
|
+import com.tencent.cloud.CosStsClient;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.TreeMap;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Author: 吴涛涛
|
|
|
+ * Date : 2019 - 12 - 20 14:31
|
|
|
+ * Description:<腾讯云文件上传的工具类>
|
|
|
+ * Version: 1.0
|
|
|
+ */
|
|
|
+public class QcloudUntil {
|
|
|
+
|
|
|
+ private static String secretId = "AKIDkfMmvJUndLwXruCUJq7EINmzvHOFqNZU";
|
|
|
+ private static String secretKey = "qPkqeCI9l6CrHTO31jBPNCk2nJabAO2R";
|
|
|
+ private static String tmpSecretId = "";
|
|
|
+ private static String tmpSecretKey = "";
|
|
|
+ private static String sessionToken = "";
|
|
|
+ private static COSClient cosClient;
|
|
|
+ private static String bucket = "yunsu-1300270157";
|
|
|
+ //静态块,用于初始化用到的配置
|
|
|
+ static {
|
|
|
+ try {
|
|
|
+ //获取临时密钥
|
|
|
+ getTemporaryKey();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1 初始化用户身份信息(secretId, secretKey)。
|
|
|
+ COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
|
|
|
+ // 1 传入获取到的临时密钥 (tmpSecretId, tmpSecretKey, sessionToken)
|
|
|
+ BasicSessionCredentials created = new BasicSessionCredentials(tmpSecretId, tmpSecretKey, sessionToken);
|
|
|
+ // 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
|
|
|
+ // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
|
|
|
+ Region region = new Region("ap-shanghai");
|
|
|
+ ClientConfig clientConfig = new ClientConfig(region);
|
|
|
+ // 3 生成 cos 客户端。
|
|
|
+ cosClient = new COSClient(cred, clientConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static PutObjectResult upload(String pathName,String onlyFileName) {
|
|
|
+ //存储桶名称,格式:BucketName-APPID
|
|
|
+ CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucket);
|
|
|
+// 设置 bucket 的权限为 PublicRead(公有读私有写), 其他可选有私有读写, 公有读写
|
|
|
+ createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
|
|
|
+ try {
|
|
|
+ //创建存储桶
|
|
|
+ Bucket bucketResult = cosClient.createBucket(createBucketRequest);
|
|
|
+ } catch (CosServiceException serverException) {
|
|
|
+ serverException.printStackTrace();
|
|
|
+ } catch (CosClientException clientException) {
|
|
|
+ clientException.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 指定要上传的文件
|
|
|
+ File localFile = new File(pathName);
|
|
|
+ // 指定要上传到的存储桶
|
|
|
+ String bucketName = bucket;
|
|
|
+ // 指定要上传到 COS 上对象键
|
|
|
+ String key = "/upload_files/"+onlyFileName;
|
|
|
+ PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
|
|
|
+ PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
|
|
|
+ return putObjectResult;
|
|
|
+ } catch (CosServiceException serverException) {
|
|
|
+ serverException.printStackTrace();
|
|
|
+ } catch (CosClientException clientException) {
|
|
|
+ clientException.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取临时密钥
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static Map getTemporaryKey() throws IOException {
|
|
|
+ Map<String,String> map = new HashMap<String, String> ();
|
|
|
+ TreeMap<String, Object> config = new TreeMap<String, Object>();
|
|
|
+// try {
|
|
|
+ // 替换为您的 SecretId
|
|
|
+ config.put("SecretId", secretId);
|
|
|
+ System.out.println(secretId);
|
|
|
+ // 替换为您的 SecretKey
|
|
|
+ config.put("SecretKey", secretKey);
|
|
|
+ System.out.println(secretKey);
|
|
|
+ // 临时密钥有效时长,单位是秒,默认1800秒,最长可设定有效期为7200秒
|
|
|
+ config.put("durationSeconds", 1800);
|
|
|
+
|
|
|
+ // 换成您的 bucket
|
|
|
+ config.put("bucket", "yunsu-1300270157");
|
|
|
+ // 换成 bucket 所在地区
|
|
|
+ config.put("region", "ap-shanghai");
|
|
|
+
|
|
|
+// config.put("expiredtime", 1800);
|
|
|
+
|
|
|
+ // 这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径,例子:a.jpg 或者 a/* 或者 * 。
|
|
|
+ // 如果填写了“*”,将允许用户访问所有资源;除非业务需要,否则请按照最小权限原则授予用户相应的访问权限范围。
|
|
|
+ config.put("allowPrefix", "a.jpg");
|
|
|
+
|
|
|
+ // 密钥的权限列表。简单上传、表单上传和分片上传需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
|
|
|
+ String[] allowActions = new String[]{
|
|
|
+ // 简单上传
|
|
|
+ "name/cos:PutObject",
|
|
|
+ // 表单上传、小程序上传
|
|
|
+ "name/cos:PostObject",
|
|
|
+ // 分片上传
|
|
|
+ "name/cos:InitiateMultipartUpload",
|
|
|
+ "name/cos:ListMultipartUploads",
|
|
|
+ "name/cos:ListParts",
|
|
|
+ "name/cos:UploadPart",
|
|
|
+ "name/cos:CompleteMultipartUpload"
|
|
|
+ };
|
|
|
+ config.put("allowActions", allowActions);
|
|
|
+
|
|
|
+ JSONObject credential = CosStsClient.getCredential(config);
|
|
|
+ //成功返回临时密钥信息,如下打印密钥信息
|
|
|
+ System.out.println(credential);
|
|
|
+ if (credential.toString().indexOf("credentials") != -1) {
|
|
|
+ tmpSecretId = credential.getJSONObject("credentials").getString("tmpSecretId");
|
|
|
+ System.out.println("tmpSecretId"+tmpSecretId);
|
|
|
+ map.put("tmpSecretId",tmpSecretId);
|
|
|
+ tmpSecretKey = credential.getJSONObject("credentials").getString("tmpSecretKey");
|
|
|
+ System.out.println("tmpSecretKey"+tmpSecretKey);
|
|
|
+ map.put("tmpSecretKey",tmpSecretKey);
|
|
|
+ sessionToken = credential.getJSONObject("credentials").getString("sessionToken");
|
|
|
+ System.out.println("sessionToken"+sessionToken);
|
|
|
+ map.put("sessionToken",sessionToken);
|
|
|
+ }
|
|
|
+// } catch (Exception e) {
|
|
|
+// //失败抛出异常
|
|
|
+// throw new IllegalArgumentException("no valid secret !");
|
|
|
+// }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取存储桶中的文件列表
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param prifix 文件的开始前缀(一般指文件夹名字)
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getBucketList(String prifix) {
|
|
|
+ try {
|
|
|
+ String bucket = "yunsu-1300270157";
|
|
|
+ ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
|
|
|
+ // 设置 bucket 名称
|
|
|
+ listObjectsRequest.setBucketName(bucket);
|
|
|
+ // prefix 表示列出的 object 的 key 以 prefix 开始
|
|
|
+ listObjectsRequest.setPrefix(prifix);
|
|
|
+ // 设置最大遍历出多少个对象, 一次 listobject 最大支持1000
|
|
|
+ listObjectsRequest.setMaxKeys(1000);
|
|
|
+ listObjectsRequest.setDelimiter("/");
|
|
|
+ ObjectListing 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);
|
|
|
+// return storageClass;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ } catch (CosServiceException serverException) {
|
|
|
+ serverException.printStackTrace();
|
|
|
+ } catch (CosClientException clientException) {
|
|
|
+ clientException.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除存储桶里的文件
|
|
|
+ * @param fileKey 指定对象在存储桶上的对象键(注:文件夹+文件名 具体参考腾讯云的对象键说明)
|
|
|
+ */
|
|
|
+ public static void delete(String fileKey){
|
|
|
+ try {
|
|
|
+ // 指定对象所在的存储桶
|
|
|
+ String bucketName = bucket;
|
|
|
+ // 指定对象在 COS 上的对象键
|
|
|
+ String key = fileKey;
|
|
|
+ cosClient.deleteObject(bucketName, key);
|
|
|
+ } catch (CosServiceException serverException) {
|
|
|
+ serverException.printStackTrace();
|
|
|
+ } catch (CosClientException clientException) {
|
|
|
+ clientException.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 下载存储桶里的文件
|
|
|
+ * @param fileKey 指定对象在存储桶上的对象键(注:文件夹+文件名 具体参考腾讯云的对象键说明)
|
|
|
+ * @param downloadPath 要下载到的路径和文件名(如:D:\34.txt 注:一定要是全路径和文件名一起,否则会导致文件无法识别)
|
|
|
+ */
|
|
|
+ public static void downLoadFile(String fileKey,String downloadPath){
|
|
|
+ try{
|
|
|
+ // 指定对象所在的存储桶
|
|
|
+ String bucketName = bucket;
|
|
|
+ // 指定对象在 COS 上的对象键
|
|
|
+ String key = fileKey;
|
|
|
+ // 指定要下载到的本地路径
|
|
|
+ File downFile = new File(downloadPath);
|
|
|
+ GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
|
|
|
+ ObjectMetadata downObjectMeta = cosClient.getObject(getObjectRequest, downFile);
|
|
|
+ } catch (CosServiceException serverException) {
|
|
|
+ serverException.printStackTrace();
|
|
|
+ } catch (CosClientException clientException) {
|
|
|
+ clientException.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 关闭客户端(关闭后台线程)
|
|
|
+ public static void close(){
|
|
|
+ cosClient.shutdown();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+// JSONObject tem = tem();
|
|
|
+// String a = tem.getJSONObject("credentials").getString("tmpSecretId");
|
|
|
+// System.out.println(a);
|
|
|
+
|
|
|
+// PutObjectResult upload = upload("D:\\1\\爱你.txt","ai.txt");
|
|
|
+// System.out.println(upload.ge);
|
|
|
+ downLoadFile("upload_files/ai.txt","D:\\34.txt");
|
|
|
+// System.out.println("存储桶列表" + getBucketList("/upload_files/"));
|
|
|
+ }
|
|
|
+}
|