腾讯云 对象存储java实现COS文件上传与返回的URL

本人已尝试上传成功

pom代码    

                <dependency>
			<groupId>com.qcloud</groupId>
			<artifactId>cos_api</artifactId>
			<version>5.2.4</version>
		</dependency>
		<dependency>
			<groupId>com.qcloud</groupId>
			<artifactId>qcloud-java-sdk</artifactId>
			<version>2.0.1</version>
		</dependency>

 

java代码

 

public static URL picCOS(File cosFile) throws Exception {
		
		COSCredentials cred = new BasicCOSCredentials("SecretId","SecretKey");
		// 2 设置bucket的区域, COS地域的简称请参照
		// https://cloud.tencent.com/document/product/436/6224
		ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
		// 3 生成cos客户端
		COSClient cosClient = new COSClient(cred, clientConfig);
		String bucketName = "bucketName"+"-APPID";
		String key = "image/"+new Date().getTime() + ".png";
		// 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
		// 大文件上传请参照 API 文档高级 API 上传
		// 指定要上传到 COS 上的路径

		PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, cosFile);
		cosClient.putObject(putObjectRequest);
		cosClient.shutdown();
		Date expiration = new Date(new Date().getTime() + 5 * 60 * 10000);
		URL url = cosClient.generatePresignedUrl(bucketName, key, expiration);
		return url;
	}

SecretId SecretKey bucketName APPID四个参数可在你所要访问的腾讯云控制台得到(你所购买的云对象储存服务器)

  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bug修得完

更多技术知识可关注询问,谢谢!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值