Java腾讯对象储存存放图片

首先我们要先去腾讯对象储存上创建一个储存桶:


OK,我们已经完成了桶的创建,你可以自己上传一张图片,试试能不能外界访问

package com.fc.blog.utils;
import com.fc.blog.service.image.ImageService;
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.*;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
* @author Lenovo
* 图片上传到腾讯云
*/
public class TencentCOS {

/**
* 图片访问地址
*/
private static final String IMAGE_URL = "";

/**
* 存储桶的名称
*/
private static final String BUCKET_NAME = "";

/**
* 密钥ID
*/
private static final String SECRET_ID = "";

/**
* 密钥Key
*/
private static final String SECRET_KEY = "";

// 1 初始化用户身份信息(secretId, secretKey,可在腾讯云后台中的API密钥管理中查看!
private static COSCredentials credentials = new BasicCOSCredentials(SECRET_ID, SECRET_KEY);

// 2 设置bucket的区域, COS地域的简称请参照
private static ClientConfig clientConfig = new ClientConfig(new Region("ap-chengdu"));

/**
* 上传图片
*
* @param file
* @return
*/
public static String uploadImage(File file) {

// 生成Cos客户端
COSClient cosClient = new COSClient(credentials, clientConfig);

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");

String fileName = "";

try {

fileName = file.getName();

String substring = fileName.substring(fileName.lastIndexOf("."));

Random random = new Random();

// 指定要上传到COS上的路径
fileName = dateFormat.format(new Date()) + "/" + random.nextInt(10000) + System.currentTimeMillis() + substring;

PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, fileName, file);

PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);

} catch (Exception e) {

e.printStackTrace();

return null;

} finally {

cosClient.shutdown();
}

// 返回图片访问地址
return IMAGE_URL + fileName;
}

/**
* 从腾讯云上全部查询数据
*
* @param bucketName
* @return
*/
public static ObjectListing listObjects(String bucketName) {

// 生成Cos客户端
COSClient cosClient = new COSClient(credentials, clientConfig);

// 获取bucket下成员(设delimiter)
ListObjectsRequest listObjectsRequest = new ListObjectsRequest();

listObjectsRequest.setBucketName(bucketName);

// 设置list的prefix,表示list出来的文件key都是以这个prefix开始
listObjectsRequest.setPrefix("/");

listObjectsRequest.setDelimiter("");

listObjectsRequest.setMaxKeys(100);

ObjectListing objectListing = cosClient.listObjects(listObjectsRequest);

String nextMarker = objectListing.getNextMarker();

boolean truncated = objectListing.isTruncated();

List<COSObjectSummary> objectSummaries = objectListing.getObjectSummaries();

return objectListing;
}

/**
* 删除图片,通过Key
* @param bucketName
* @param key
* @return
*/
public static boolean delete(String bucketName, String key) {

boolean flag = false;

try {

// 执行删除
// 生成Cos客户端
COSClient cosClient = new COSClient(credentials, clientConfig);

cosClient.deleteObject(BUCKET_NAME, key);

return flag = true;

} catch (Exception e) {

e.printStackTrace();

return flag;
}
}
}

OK ,这里我们代码就写完了,要想继续了解,可以去文档查看

没有控制层,只写了业务逻辑,可以实践一下。。。

♥♥2020-03-04 15:24:32 星期三