简介:腾讯云对象存储(Cloud Object Storage,COS)是腾讯云为企业和个人开发者们提供的一种能够存储海量数据的分布式存储服务,用户可随时通过互联网对您的大量数据进行批量存储和处理。
首先先创建1个存储桶
然后去云API**管理,新建1个**
然后就可以通过这个**访问云资源了
1.先引入腾讯云COS的依赖包
<dependencies> <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> </dependencies>2.以下代码可以对文件进行上传下载和删除操作
package com.ljm.test; 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.GetObjectRequest; import com.qcloud.cos.model.PutObjectRequest; import com.qcloud.cos.region.Region; import com.qcloud.cos.transfer.Download; import com.qcloud.cos.transfer.TransferManager; import com.qcloud.cos.transfer.Upload; import java.io.File; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 高级sdk操作 */ public class CosDemo { static String bucketName = "bj-demo"; //桶的名称 static String key = "/upload_demo.pdf"; //上传到云上路径 static String region = "ap-beijing";//区域北京则 beijing static String appId = "1257072160"; //APPID static COSCredentials cred = null; static TransferManager transferManager = null; static COSClient cosClient = null; static { // 1 初始化用户身份信息(secretId, secretKey) //SecretId 是用于标识 API 调用者的身份 String SecretId = ""; //SecretKey是用于加密签名字符串和服务器端验证签名字符串的** String SecretKey = ""; cred = new BasicCOSCredentials(SecretId, SecretKey); // 2 设置bucket的区域, ClientConfig clientConfig = new ClientConfig(new Region(region)); // 3 生成cos客户端 cosClient = new COSClient(cred, clientConfig); // 指定要上传到 COS 上的路径 ExecutorService threadPool = Executors.newFixedThreadPool(32); // 传入一个 threadpool, 若不传入线程池, 默认 TransferManager 中会生成一个单线程的线程池。 transferManager = new TransferManager(cosClient, threadPool); } public static void main(String[] args) { //上传 upload(); //下载 //download(); //删除 //delete(); cosClient.shutdown(); } static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm ss"); /** * 上传 */ public static void upload() { new Thread(new Runnable() { public void run() { try { System.out.println("上传开始时间:" + sdf.format(new Date())); // .....(提交上传下载请求, 如下文所属) // bucket 的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式 String bucket = bucketName + "-" + appId; //本地文件路径 File localFile = new File("src/test/resources/test.pdf"); PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile); // 本地文件上传 Upload upload = transferManager.upload(putObjectRequest); // 异步(如果想等待传输结束,则调用 waitForUploadResult) //UploadResult uploadResult = upload.waitForUploadResult(); //同步的等待上传结束waitForCompletion upload.waitForCompletion(); System.out.println("上传结束时间:" + sdf.format(new Date())); System.out.println("上传成功"); //获取上传成功之后文件的下载地址 URL url = cosClient.generatePresignedUrl(bucketName + "-" + appId, key, new Date(new Date().getTime() + 5 * 60 * 10000)); System.out.println(url); } catch (Throwable tb) { System.out.println("上传失败"); tb.printStackTrace(); } finally { // 关闭 TransferManger transferManager.shutdownNow(); } } }).start(); } /** * 下载 */ public static void download() { try { //下载到本地指定路径 File localDownFile = new File("src/test/resources/download.pdf"); GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName + "-" + appId, key); // 下载文件 Download download = transferManager.download(getObjectRequest, localDownFile); // 等待传输结束(如果想同步的等待上传结束,则调用 waitForCompletion) download.waitForCompletion(); System.out.println("下载成功"); } catch (Throwable tb) { System.out.println("下载失败"); tb.printStackTrace(); } finally { // 关闭 TransferManger transferManager.shutdownNow(); } } /** * 删除 */ public static void delete() { new Thread(new Runnable() { public void run() { // 指定要删除的 bucket 和路径 try { cosClient.deleteObject(bucketName + "-" + appId, key); System.out.println("删除成功"); } catch (Throwable tb) { System.out.println("删除文件失败"); tb.printStackTrace(); } } }).start(); } }下面2个文件上要上传的文件,及重云上下载下来的文件
这个是上传成功的效果,下面生成的链接是可以通过外网直接下载该文件的地址