Use Qiniu Cloud to Store Pictures

Posted by MichaelMackey on Tue, 23 Jul 2019 12:06:47 +0200

Now many websites will use a large number of pictures, and pictures are the main amount of data in the transmission of web pages, but also one of the factors affecting the performance of websites. As a result, many websites will separate image storage from websites, and build one or more servers to store pictures, while pictures on webpages use a URL address to point to the address of pictures on these servers, so the performance of the website will be significantly improved. Picture servers can be built by themselves, or use cloud servers on the Internet, such as Ali, this paper chooses Qiniuyun object storage to achieve. After personal registration and real-name certification on Qiniuyun, 10GB of storage space can be used free of charge, which is enough for testing.

Use steps:

1. Preparations
After registration, we can authenticate the real name, and then we can get AccessKey and SecretKey. This is mainly used when calling the interface later.

2. Create storage space
Click on the "Management Console" - > "Object Storage" - > to create a new storage space, and fill in the corresponding information.

If you want users to have direct access to resources under any circumstances, you can make the space public directly.

3. Introduce JAVA-SDK provided by Qiniuyun into the project
Note: This SDK is applicable to Java 7 and above

Introduce in the way of maven:

<dependency>
  <groupId>com.qiniu</groupId>
  <artifactId>qiniu-java-sdk</artifactId>
  <version>[7.2.0, 7.2.99]</version>
</dependency>

Here version specifies a range of versions, and every time you update pom.xml, you try to download the latest version in version 7.2.x, but you can also manually specify a fixed version.

4. Use SDK-provided image uploading tools
Qiniu file upload can be divided into two scenarios: client upload (mainly referring to the scenarios for end users such as web and mobile) and server upload. Specifically, it can refer to official documents. In this paper, the way of server upload is used.

Combine Spring MVC to upload pictures to Qiniuyun:

/**
 * File Upload Tool
 */
public class UploadUtil {

    /**
     * Upload to Qiniuyun
     * @param file Uploaded pictures
     * @return The name of the picture in Qiniuyun
     */
    public static String uploadQiniu(MultipartFile file) {
        //Construct a configuration class with a specified Zone object
        Configuration cfg = new Configuration(Zone.zone2());
        //... other parameter reference class annotations _____________
        UploadManager uploadManager = new UploadManager(cfg);
        //... Generate upload credentials and prepare for upload
        String accessKey = "Your accessKey";
        String secretKey = "Your secretKey";
        //Name of storage space
        String bucket = "wolfcode";
        //If the default key is not specified, the hash value of the file content is used as the file name.
        String key = null;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(file.getBytes(), key, upToken);
            //Analysis of successful upload results
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            return putRet.key;
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}

Remarks:
Seven Bull Storage Support Spaces are created in different computer rooms. Zone Object is to specify which computer room to use.

2. If the key is not specified by default, the hash value of the file content is used as the file name. The advantage of using hash can also remove duplicate pictures and upload the same picture repeatedly, which only takes up one picture space in Qiniu Cloud.

3. The DefaultPutRet object returned after the successful upload contains the hash value of the image, and the key value of the image is actually the file name.

The last step is to access the image, open the object storage space, you can see the Qiniuyun allocated domain name, as well as uploaded images, the domain name and the key value of the image are stitched together, that is, the image URL, such as: http://pgd3zoxnk.bkt.clouddn.com/FgaeuBs1QwDZUd9UO4betMtgZOMs

Topics: Programming SDK Java Maven xml