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");
	}

}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\],在uniapp app中上腾讯COS文件可以使用upFile.js中的方法。在需要的页面中调用import upFile from '@/config/upFile.js'来引入upFile.js文件。然后可以使用upFileBtn(tag)方法来上视频或者封面。如果tag等于1,表示上视频,可以调用upFile.cosChoosevideo()方法来选择视频并上。如果tag等于2,表示上图片,可以调用upFile.cosUpLoadimg()方法来选择图片并上。上成功后,可以获取到相应的视频链接或者封面图片链接。 根据引用\[2\],为了避免SDK版本变更引发的问题,可以在node_modules\cos-wx-sdk-v5\文件夹下使用Linux命令grep -rn 'wx.'来全局搜索其他使用小程序API的地方,并进行相应的替换。这样可以确保在上腾讯COS文件时不会受到SDK版本变更的影响。 #### 引用[.reference_title] - *1* [uniapp----上图片和视频到腾讯云COS](https://blog.csdn.net/heavenz19/article/details/129708899)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [uniapp+PHP,APP端使用腾讯云cos SDK上文件](https://blog.csdn.net/weixin_35252964/article/details/116321982)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值