Create a bucket on the Tencent object store:

OK, we have finished creating the bucket, you can upload a picture and try to access it

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;

/ * * *@authorLenovo * Upload images to Tencent Cloud */
public class TencentCOS {

    /** ** *
    private static final String IMAGE_URL = "";

    /** * The bucket name */
    private static final String BUCKET_NAME = "";

    /** * key ID */
    private static final String SECRET_ID = "";

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

    // 1 Initialize user identity information (secretId, secretKey, which can be viewed in API key management in Tencent cloud background!
    private static COSCredentials credentials = new BasicCOSCredentials(SECRET_ID, SECRET_KEY);

    // 2 Set the bucket area. For the short name of the COS area, see
    private static ClientConfig clientConfig = new ClientConfig(new Region("ap-chengdu"));
    
    /** * upload image **@param file
     * @return* /
    public static String uploadImage(File file) {
    
     // Generate Cos client
        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();

            // Specify the path to upload to 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 the image access address
        return IMAGE_URL + fileName;
    }  
    
    /** * Query all data from the Cloud **@param bucketName
     * @return* /
    public static ObjectListing listObjects(String bucketName) {

        // Generate Cos client
        COSClient cosClient = new COSClient(credentials, clientConfig);

        // Get the bucket members (delimiter)
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest();

        listObjectsRequest.setBucketName(bucketName);

        // Set list keys to start with 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;
    }

    /** * delete image by Key *@param bucketName
     * @param key
     * @return* /
    public static boolean delete(String bucketName, String key) {

        boolean flag = false;

        try {

            // Perform the delete
            // Generate Cos client
            COSClient cosClient = new COSClient(credentials, clientConfig);

            cosClient.deleteObject(BUCKET_NAME, key);

            return flag = true;

        } catch (Exception e) {

            e.printStackTrace();

            returnflag; }}}Copy the code

OK, so we’re done with the code, and if you want to follow up, you can go to the documentation

No control layer, only business logic, can practice…

♥2020-03-04 15:24:32 Wednesday