TencentCOSUtils腾讯cos上传下载删除文件

package com.hengtong.boxcode.utils.tool;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;

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.COSObject;
import com.qcloud.cos.model.COSObjectInputStream;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;

@SuppressWarnings("unused")
public class TencentCOSUtils {
	/**
	 * COS APPID
	 */
	private static Integer APPID = 1251800125;//假的id,只参考长度和类型
	/**
	 * COS SECRETID
	 */
	private static String SECRETID = "AKIDhCgwwgsXbQzZuzKX1JK6rNefS0JVqUEj";//假的id,只参考长度和类型
	/**
	 * COS SECRETKEY
	 */
	private static String SECRETKEY = "bx4wYv98qJXWFjy82hqlSt21uXviGZcP";//假的id,只参考长度和类型
	/**
	 * COS BUCKETNAME bucket的命名规则为{name}-{appid} ,存储桶名称必须为此格式
	 */
	private static String BUCKETNAME = "nike-obp-manager-file-1251800625";//假的id,只参考长度和类型

	// 上传文件
	public static String upload(InputStream input, String filename) {
		COSClient cosClient = null;
		try {
			// 1 初始化用户身份信息(secretId, secretKey)。
			COSCredentials cred = new BasicCOSCredentials(SECRETID, SECRETKEY);
			// 2 设置bucket的区域, COS地域的简称请参照
			// https://cloud.tencent.com/document/product/436/6224
			// clientConfig中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者接口文档 FAQ
			// 中说明。
			ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing"));
			// 3 生成 cos 客户端。
			cosClient = new COSClient(cred, clientConfig);
			// 指定要上传到的存储桶,指定要上传到 COS 上对象键
			ObjectMetadata objectMetadata = new ObjectMetadata();
			PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKETNAME, filename, input, objectMetadata);
			PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
			Date expiration = new Date(new Date().getTime() + 5 * 60 * 10000);
			URL url = cosClient.generatePresignedUrl(BUCKETNAME, filename, expiration);
			return url.toString().substring(0,url.toString().indexOf("?"));
		} finally {
			if (cosClient != null) {
				// 关闭客户端(关闭后台线程)
				cosClient.shutdown();
			}
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	// 下载文件
	public static COSObjectInputStream download(String filename) {
		COSClient cosClient = null;
		try {
			// 1 初始化用户身份信息(secretId, secretKey)。
			COSCredentials cred = new BasicCOSCredentials(SECRETID, SECRETKEY);
			// 2 设置bucket的区域, COS地域的简称请参照
			// https://cloud.tencent.com/document/product/436/6224
			// clientConfig中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者接口文档 FAQ
			// 中说明。
			ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing"));
			// 3 生成 cos 客户端。
			cosClient = new COSClient(cred, clientConfig);
			// 指定文件所在的存储桶,指定文件在 COS 上的对象键
			GetObjectRequest getObjectRequest = new GetObjectRequest(BUCKETNAME, filename);
			COSObject cosObject = cosClient.getObject(getObjectRequest);
			return cosObject.getObjectContent();
		} finally {
			if (cosClient != null) {
				// 关闭客户端(关闭后台线程)
				cosClient.shutdown();
			}
		}
	}

	/**
	 * 下载文件
	 * 
	 * @param filename
	 * @return
	 */
	public static String download_url(String filename) {
		COSClient cosClient = null;
		try {
			// 1 初始化用户身份信息(secretId, secretKey)。
			COSCredentials cred = new BasicCOSCredentials(SECRETID, SECRETKEY);
			// 2 设置bucket的区域, COS地域的简称请参照
			// https://cloud.tencent.com/document/product/436/6224
			// clientConfig中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者接口文档 FAQ
			// 中说明。
			ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing"));
			// 3 生成 cos 客户端。
			cosClient = new COSClient(cred, clientConfig);
			// 指定文件所在的存储桶,指定文件在 COS 上的对象键
			GetObjectRequest getObjectRequest = new GetObjectRequest(BUCKETNAME, filename);
			COSObject cosObject = cosClient.getObject(getObjectRequest);

			Date expiration = new Date(new Date().getTime() + 5 * 60 * 10000);
			URL url = cosClient.generatePresignedUrl(BUCKETNAME, filename, expiration);
			return url.toString().substring(0,url.toString().indexOf("?"));
		} finally {
			if (cosClient != null) {
				// 关闭客户端(关闭后台线程)
				cosClient.shutdown();
			}
		}
	}

	// 删除文件
	public static void delete(String filename) {
		COSClient cosClient = null;
		try {
			// 1 初始化用户身份信息(secretId, secretKey)。
			COSCredentials cred = new BasicCOSCredentials(SECRETID, SECRETKEY);
			// 2 设置bucket的区域, COS地域的简称请参照
			// https://cloud.tencent.com/document/product/436/6224
			// clientConfig中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者接口文档 FAQ
			// 中说明。
			ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing"));
			// 3 生成 cos 客户端。
			cosClient = new COSClient(cred, clientConfig);
			cosClient.deleteObject(BUCKETNAME, filename);
		} finally {
			if (cosClient != null) {
				// 关闭客户端(关闭后台线程)
				cosClient.shutdown();
			}
		}

	}

	public static void main(String[] args) throws FileNotFoundException {
		File file = new File("/Users/wenjianjia1/wenjianjia2/5b20ba638ac4e.jpg");
		System.out.println(upload(new FileInputStream(file), "/act_images/A2687D13-FED4-4889-B0D2-B79431403F26.jpg"));
		// download("/avatar/0f6d9fe9422f4a9aa94b86506f47fcff.jpg");
	}

}

 

你可能感兴趣的