use of com.qcloud.cos.model.PutObjectResult in project e3mall by colg-cloud.
the class TencentCosTest method testName.
@Test
public void testName() {
COSCredentials cred = new BasicCOSCredentials("AKIDUvjV6VEvvjS4Vliw360iEYpvpgMrqMKF", "sY3NVSL8kLk8KK8lIftzud9VggU5Vkne");
// 采用了新的region名字,可用region的列表可以在官网文档中获取,也可以参考下面的XML SDK和JSON SDK的地域对照表
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket的名字需要的包含appId
String bucketName = "colg-1256242877";
// 上传文件
File localFile = new File(PROJECT_PATH + "\\src\\test\\resources\\images\\FastDfs架构.png");
String key = "colg.png";
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
// 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
putObjectRequest.setStorageClass(StorageClass.Standard);
PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
// putObjectResult会返回文件的etag
String etag = putObjectResult.getETag();
log.info("etag: {}", etag);
// 关闭客户端
cosclient.shutdown();
}
use of com.qcloud.cos.model.PutObjectResult in project RuleApi by buxia97.
the class UploadController method cosUpload.
/**
* 上传道腾讯云服务器(https://cloud.tencent.com/document/product/436/10199)
* @return
*/
@RequestMapping(value = "/cosUpload", method = RequestMethod.POST)
@ResponseBody
public Object cosUpload(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "token", required = false) String token) throws IOException {
Integer uStatus = UStatus.getStatus(token, this.dataprefix, redisTemplate);
if (uStatus == 0) {
return Result.getResultJson(0, "用户未登录或Token验证失败", null);
}
if (file == null) {
return new UploadMsg(0, "文件为空", null);
}
String oldFileName = file.getOriginalFilename();
String eName = oldFileName.substring(oldFileName.lastIndexOf("."));
// 检查是否是图片
BufferedImage bi = ImageIO.read(file.getInputStream());
if (bi == null) {
return Result.getResultJson(0, "请上传图片文件", null);
}
String newFileName = UUID.randomUUID() + eName;
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DATE);
// 1 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
// 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region(bucket));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
String bucketName = this.bucketName;
// 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
// 大文件上传请参照 API 文档高级 API 上传
File localFile = null;
try {
localFile = File.createTempFile("temp", null);
file.transferTo(localFile);
// 指定要上传到 COS 上的路径
String key = "/" + this.prefix + "/" + year + "/" + month + "/" + day + "/" + newFileName;
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
// return new UploadMsg(1,"上传成功",this.path + putObjectRequest.getKey());
Map<String, String> info = new HashMap<String, String>();
info.put("url", this.path + putObjectRequest.getKey());
return Result.getResultJson(1, "上传成功", info);
} catch (IOException e) {
return Result.getResultJson(1, "上传失败", null);
} finally {
// 关闭客户端(关闭后台线程)
cosclient.shutdown();
}
}
use of com.qcloud.cos.model.PutObjectResult in project accright-blog by Accright.
the class COSApi method uploadFileToCOS.
/**
* 使用文件流上传文件至COS
* @param file
* @return
*/
public String uploadFileToCOS(MultipartFile file) throws IOException {
// 获取上传文件的名称用于获取后缀
String fileName = file.getOriginalFilename();
// 获取文件的后缀
String prex = fileName.substring(fileName.lastIndexOf("."));
// 设置文件的key
String key = System.currentTimeMillis() + prex;
// 获取文件输入流
InputStream inputStream = file.getInputStream();
// 获取文件元信息
ObjectMetadata objectMetadata = new ObjectMetadata();
// 设置输入流长度为 500
objectMetadata.setContentLength(file.getSize());
// 设置 Content type, 默认是 application/octet-stream
objectMetadata.setContentType(file.getContentType());
PutObjectResult putObjectResult = cosclient.putObject(bucketName, key, inputStream, objectMetadata);
// 关闭输入流
inputStream.close();
// 设置过期时间 ---可以返回带有过期时间的URL
// Date expiration = new Date(new Date().getTime() + 30L * 60L * 1000L);
// URL urlx = cosclient.generatePresignedUrl(bucketName, key, expiration,HttpMethodName.GET);
// return urlx.toString();
// 关闭客户端
cosclient.shutdown();
// 返回URL
return BUCKET_URL + key;
}
use of com.qcloud.cos.model.PutObjectResult in project cos-java-sdk-v5 by tencentyun.
the class CAMRoleDemo method SimpleUploadFileFromLocal.
public static void SimpleUploadFileFromLocal() {
InstanceMetadataCredentialsEndpointProvider endpointProvider = new InstanceMetadataCredentialsEndpointProvider(InstanceMetadataCredentialsEndpointProvider.Instance.CVM);
InstanceCredentialsFetcher instanceCredentialsFetcher = new InstanceCredentialsFetcher(endpointProvider);
COSCredentialsProvider cosCredentialsProvider = new InstanceCredentialsProvider(instanceCredentialsFetcher);
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
COSClient cosClient = new COSClient(cosCredentialsProvider, clientConfig);
String bucketName = "3399demo-125xxxxxxxx";
String key = "test/demo.txt";
File localFile = new File("test");
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
putObjectRequest.setStorageClass(StorageClass.Standard);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
cosClient.shutdown();
}
use of com.qcloud.cos.model.PutObjectResult in project cos-java-sdk-v5 by tencentyun.
the class PutObjectDemo method putObjectDemo.
static void putObjectDemo() {
String bucketName = "examplebucket-1251668577";
String key = "abc/abc.txt";
String localPath = "abc.txt";
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setHeader("expires", new Date(1660000000000L));
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, new File(localPath));
putObjectRequest.withMetadata(objectMetadata);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
System.out.println(putObjectResult.getRequestId());
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
COSObject cosObject = cosClient.getObject(getObjectRequest);
System.out.println(cosObject.getObjectMetadata().getRequestId());
cosClient.shutdown();
}
Aggregations