Introduction to image storage scheme --- qiniu cloud storage

1. Picture storage scheme

1.1 INTRODUCTION

In the actual development, we will have many servers dealing with different functions. For example:

Application server: responsible for deploying our applications

Database server: running our database

File server: the server responsible for storing files uploaded by users

The purpose of sub server processing is to let the server perform its duties, so as to improve the operation efficiency of our project.

Common picture storage schemes:

Scheme 1: use nginx to build a picture server

Scheme 2: use open source distributed file storage systems, such as Fastdfs, HDFS, etc

Scheme 3: use cloud storage, such as Alibaba cloud and qiniu cloud

1.2 qiniu cloud storage

Seven cattle cloud (affiliated to Shanghai qiniu Information Technology Co., Ltd.) is a leading enterprise level cloud computing service provider with visual intelligence and data intelligence as the core in China. It is also a well-known intelligent video cloud service provider in China. It has provided services to more than 700000 enterprises, covering 80% of domestic Internet users. It has launched object storage, integrated CDN acceleration, container cloud and big data around rich media scenarios Platform, in-depth learning platform and other products, and provide one-stop intelligent video cloud solutions. Provide sustainable intelligent video cloud ecology for various industries and applications, help enterprises quickly go to the cloud and create broader business value.

Official website: https://www.qiniu.com/

Through the introduction of qiniu cloud's official website, we can know that it provides a variety of services. We mainly use the object storage service provided by qiniu cloud to store pictures.

1.2. 1. Registration and login

To use qiniu cloud's services, you first need to register as a member. Address: https://portal.qiniu.com/signup

After registration, you can log in to qiniu cloud with the just registered email and password:

After successful login, click the management console in the upper right corner of the page:

Note: after successful login, real name authentication is also required to carry out relevant operations.

1.2. 2. Create a new storage space

To store pictures, we need to create a new storage space on the qiniu cloud management console. Click the add now button under object storage on the homepage of the management console, and the page will jump to the new storage space page:

Multiple storage spaces can be created, and each storage space is independent of each other.

1.2. 3 view storage space information

After the storage space is created, the name of the created storage space will be displayed in the storage space list menu on the left. Click the storage space name to view the relevant information of the current storage space

1.2. 4 Developer Center

You can learn how to operate the qiniu cloud service through the developer center provided by qiniu cloud. Address: https://developer.qiniu.com/

Click object storage to jump to the object storage development page, address: https://developer.qiniu.com/kodo

Qiniu cloud provides a variety of ways to operate object storage services. The project adopts Java SDK, address: https://developer.qiniu.com/kodo/sdk/1239/java

To operate qiniu cloud using Java SDK, you need to import the following maven coordinates:

<dependency>
  <groupId>com.qiniu</groupId>
  <artifactId>qiniu-java-sdk</artifactId>
  <version>7.2.0</version>
</dependency>

1.2. 5 authentication

All functions of the Java SDK require legal authorization. The signing of authorization credentials requires a pair of valid access keys and secret keys under the qiniu account. These keys can be used in the personal center of the qiniu cloud management console( https://portal.qiniu.com/user/key )Obtained, as shown below:

1.2.6 Java SDK operation qiniu cloud

In this chapter, we need to use the Java SDK provided by qiniu cloud to upload and delete pictures. We can refer to the examples provided by the official.

//Construct a configuration class with a specified Zone object
Configuration cfg = new Configuration(Zone.zone0());
//... Other parameters refer to class notes
UploadManager uploadManager = new UploadManager(cfg);
//... Generate upload voucher and prepare to upload
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
//In the case of Windows, the format is D: \ \ qiniu \ \ test png
String localFilePath = "/home/qiniu/test.png";
//If the key is not specified by default, 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(localFilePath, key, upToken);
    //Analyze the results of successful upload
    DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
    System.out.println(putRet.key);
    System.out.println(putRet.hash);
} catch (QiniuException ex) {
    Response r = ex.response;
    System.err.println(r.toString());
    try {
        System.err.println(r.bodyString());
    } catch (QiniuException ex2) {
        //ignore
    }
}
//Construct a configuration class with a specified Zone object
Configuration cfg = new Configuration(Zone.zone0());
//... Other parameters refer to class notes

String accessKey = "your access key";
String secretKey = "your secret key";

String bucket = "your bucket name";
String key = "your file key";

Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
    bucketManager.delete(bucket, key);
} catch (QiniuException ex) {
    //If an exception is encountered, the deletion fails
    System.err.println(ex.code());
    System.err.println(ex.response.toString());
}

1.2. 7 packaging tools

In order to facilitate the operation of qiniu cloud storage service, we can simply transform the official case into a tool class and directly use this tool class in our project:

package com.itheima.utils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * Seven cattle cloud tools
 */
public class QiniuUtils {
    public  static String accessKey = "dulF9Wze9bxujtuRvu3yyYb9JX1Sp23jzd3tO708";
    public  static String secretKey = "vZkhW7iot3uWwcWz9vXfbaP4JepdWADFDHVLMZOe";
    public  static String bucket = "qiniutest";

    public static void upload2Qiniu(String filePath,String fileName){
        //Construct a configuration class with a specified Zone object
        Configuration cfg = new Configuration(Zone.zone0());
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(filePath, fileName, upToken);
            //Analyze the results of successful upload
            DefaultPutRet putRet = 
              new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        } catch (QiniuException ex) {
            Response r = ex.response;
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
    }

    //Upload file
    public static void upload2Qiniu(byte[] bytes, String fileName){
        //Construct a configuration class with a specified Zone object
        Configuration cfg = new Configuration(Zone.zone0());
        //... Other parameters refer to class notes
        UploadManager uploadManager = new UploadManager(cfg);
        //If the key is not specified by default, the hash value of the file content is used as the file name
        String key = fileName;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(bytes, key, upToken);
            //Analyze the results of successful upload
            DefaultPutRet putRet = 
              new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
    }

    //Delete file
    public static void deleteFileFromQiniu(String fileName){
        //Construct a configuration class with a specified Zone object
        Configuration cfg = new Configuration(Zone.zone0());
        String key = fileName;
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket, key);
        } catch (QiniuException ex) {
            //If an exception is encountered, the deletion fails
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
        }
    }
}

Keywords: cloud computing Alibaba Cloud

Added by hogleg on Wed, 29 Dec 2021 01:45:32 +0200