Search in sources :

Example 1 with PutObjectResult

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();
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) BaseTest(cn.e3mall.common.tencent.cos.BaseTest) Test(org.junit.Test)

Example 2 with PutObjectResult

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();
    }
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) HashMap(java.util.HashMap) Calendar(java.util.Calendar) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) COSClient(com.qcloud.cos.COSClient) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with PutObjectResult

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;
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) InputStream(java.io.InputStream) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 4 with PutObjectResult

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();
}
Also used : COSClient(com.qcloud.cos.COSClient) PutObjectResult(com.qcloud.cos.model.PutObjectResult) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 5 with PutObjectResult

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();
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) COSObject(com.qcloud.cos.model.COSObject) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Date(java.util.Date) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Aggregations

PutObjectResult (com.qcloud.cos.model.PutObjectResult)34 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)22 File (java.io.File)21 COSClient (com.qcloud.cos.COSClient)14 ClientConfig (com.qcloud.cos.ClientConfig)14 Region (com.qcloud.cos.region.Region)14 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)11 COSCredentials (com.qcloud.cos.auth.COSCredentials)11 CosServiceException (com.qcloud.cos.exception.CosServiceException)11 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)11 CosClientException (com.qcloud.cos.exception.CosClientException)10 CIObject (com.qcloud.cos.model.ciModel.persistence.CIObject)4 CIUploadResult (com.qcloud.cos.model.ciModel.persistence.CIUploadResult)4 PicOperations (com.qcloud.cos.model.ciModel.persistence.PicOperations)4 IOException (java.io.IOException)4 LinkedList (java.util.LinkedList)4 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 FileOperationException (run.halo.app.exception.FileOperationException)3