Introduction and Use of Qiniuyun _Tool class for uploading Qiniuyun files

 

 

  1. 1. Picture Storage Scheme

    1. introduce

In actual development, we have many servers that handle different functions.For example:

Application Server: Responsible for deploying our applications

Database Server: Run Our Database

File Server: Server responsible for storing user uploaded files

Role of individual servers

 

 

 

 

 

 

The purpose of sub-server processing is to make the servers do their jobs, thereby improving the efficiency of our projects.

Common picture storage schemes:

Option 1: Build a picture server using nginx

Scenario 2: Use open source distributed file storage systems such as Fastdfs, HDFS, etc.

Option 3: Use cloud storage, such as Ali Cloud and Qiniu Cloud

2. Qiniuyun Storage

Qiniuyun (subordinate to Shanghai Qiniu Information Technology Co., Ltd.) is a leading enterprise cloud computing service provider with visual intelligence and data intelligence as the core, and is also a well-known intelligent video cloud service provider in China. It provides services to more than 700,000 enterprises, covering 80% of the netizens in China.Products such as object storage, CDN acceleration, container cloud, large data platform, in-depth learning platform, and one-stop smart video cloud solutions are introduced around rich media scenarios.Provide a sustainable and intelligent video cloud ecology for all walks of life, industries and applications, help enterprises to quickly go to the cloud, and create broader business value.

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

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

3. Registration and Login of Qiniuyun

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

register

 

Once the registration is complete, you can use the mailbox and password you just registered to log in to Qiniuyun.After successful login, name authentication is required to operate (create storage space).

Title

Since we use it personally, we only need to apply for personal certification.

   

Title

4. New Storage Space

To store pictures, we need to create a new storage space in the Qiniuyun Management Console.Click the Add Now button under the Object Storage on the first page of the Administration Console to jump to the New Storage Space page:

Title

You can create multiple storage spaces that are independent of each other.

After successful creation, we can see:

Title

View Storage Space Information

Title

5. Developer Center

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

Title

Click Object Storage.

Qiniuyun provides a variety of ways to operate object storage services. This project uses Java SDK mode with address: https://developer.qiniu.com/kodo/sdk/1239/java

Title

Operating Qiniuyun with Java SDK requires the following maven coordinates to be imported: (We imported them when we built the project).)

6. Authentication

All functions of the Java SDK require legal authorization.Authorization voucher signing requires a pair of valid Access Key s and Secret Key s under the seven bull account, which can be used in the personal center of the seven bull cloud management console (https://portal.qiniu.com/user/key) Obtained as follows:

Java SDK Operations Qiniuyun

Title

Upload Files

public class QiNiuYunTest {
    //Add Files
@Test
    public void test01() {
        //Construct a configuration class with a specified Region object
        Configuration cfg = new Configuration(Zone.zone0());
        //Create File Upload Manager
        UploadManager uploadManager = new UploadManager(cfg);
        //...Generate upload credentials and prepare to upload
        String accessKey = "h-f5rWEELnqQPXG-ho65HrWdYJqfQyYf8EL0_r2y";
        String secretKey = "fad5c3NcdcOMWLk_Ss41G9gLFfeRSQ0--KY0fYz6";
        String bucket = "add1";//Name of the storage space created
        //In the case of Windows, the format is D:\\qiniu\Test.png
        String localFilePath = "C:\\Users\\Xiang Jiewei\\Desktop\\schema Reference to Constraint.png";
        //The name of the uploaded file, using the hash value of the file content as the file name without specifying the key by default
        String key = null;
        //Equivalent to authentication
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            //Uploading to Qiniuyun
            Response response = uploadManager.put(localFilePath, key, upToken);
            //Resolve upload success results
            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
            }
        }
  }
}
Title

 

 

 

 

 

 

Output results in console:

Title

 

 

 

 

 

 

 

Enter Qiniuyun File Management Center to query the effect of file upload:

Title

 

 

 

 

 

 

 

Click Details to view the details of the uploaded file.

Title

 

 

 

 

 

 

 

You can directly access the uploaded picture by copying the file link in the picture above.

Delete Files

 //Delete Files
    @Test
    public void test02() {
        //Construct a configuration class with a specified Region object
        Configuration cfg = new Configuration(Zone.zone0());
        //...other parameters reference class notes
        //...Generate upload credentials and prepare to upload
        String accessKey = "h-f5rWEELnqQPXG-ho65HrWdYJqfQyYf8EL0_r2y";
        String secretKey = "fad5c3NcdcOMWLk_Ss41G9gLFfeRSQ0--KY0fYz6";
        //Name of the storage space created
        String bucket = "add1";
        String key = "FsxdrfgDkabrRRDiHCOKwxxnLTR2";
        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, deletion fails
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
        }
    }

Encapsulation Tool Class

To facilitate the operation of Qiniuyun Storage Service, we can simply transform the case provided by the government into a tool class that can be used directly in our project.

Copy the tool class into the health-common submodule.

public class QiniuUtils {
    public  static String accessKey = "zDx9DjpPq37DnvVo6Je_eBvhAnCh2hQFYg4PVxO7";
    public  static String secretKey = "Kg8YaOlgxwhrw6iMOvFeNTxLLzilpVAm3bc-uMBQ";
    public  static String bucket = "bianyihealth-01";

    public static void upload2Qiniu(String filePath,String fileName){
        //Construct a configuration class with the 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);
            //Resolve upload success results
            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 Files
    public static void upload2Qiniu(byte[] bytes, String fileName){
        //Construct a configuration class with the specified Zone object
        Configuration cfg = new Configuration(Zone.zone0());
        //...other parameters reference class notes
        UploadManager uploadManager = new UploadManager(cfg);

        //Use hash value of file content as file name without specifying key by default
        String key = fileName;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(bytes, key, upToken);
            //Resolve upload success results
            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 Files
    public static void deleteFileFromQiniu(String fileName){
        //Construct a configuration class with the 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, deletion fails
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
        }
    }
}

The file directory after definition is as follows:

Title

Tags: Java SDK Database Nginx

Posted on Thu, 18 Jun 2020 21:30:19 -0400 by chick3n