Seven cattle cloud file upload and file management

Seven cattle cloud file upload and file management

Seven cattle cloud official website tutorial: https://developer.qiniu.com/kodo/sdk/1239/java
Project GitHub address: https://github.com/lx972/qiniu-test

1 upload

There are two ways to upload

(1) Server direct transfer: server direct transfer refers to that the customer directly uploads files from the server to qiniu cloud by using qiniu server SDK. The interactive parties are generally in the computer room, so the server can generate upload vouchers by itself, then upload them by using the upload logic in the SDK, and finally obtain the upload results from qiniu cloud. In this process, because both parties are business servers, they rarely use the function of upload callback, Instead, you can directly customize the returnBody to get the customized reply content.

(2) Client direct transmission: this method is to upload the browser directly to qiniu cloud, but before uploading, go to your own server to obtain a signature, which includes identification. With this, qiniu cloud can store resources in the corresponding user space.

The following figure is the programming model given on the official website of qiniuyun.

[the external chain picture transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-n2a1mru2-1605521018308)(image\FkPZ31ECmtGnEisOahMKc5kQkuRr.png)]

1.1 server direct transmission

It is rewritten according to the byte array upload case given by qiniuyun official website

https://developer.qiniu.com/kodo/sdk/1239/java#upload-config

/**
     * Server direct transmission
     *
     * @param accessKey
     * @param secretKey
     * @param bucket
     * @param externaLink Seven cattle cloud outside the chain domain name
     * @param file
     * @return
     */
public static String upload(String accessKey, String secretKey, String bucket, String externaLink, MultipartFile file){
    //Construct a configuration class with a specified Region object
    Configuration cfg = new Configuration(Region.region2());
    //... Other parameters refer to class notes
    UploadManager uploadManager = new UploadManager(cfg);
    //... Generate upload voucher and prepare for upload
    byte[] uploadBytes = new byte[0];
    try {
        uploadBytes = file.getBytes();
    } catch (IOException e) {
        e.printStackTrace();
    }
    String originalFilename = file.getOriginalFilename();
    //File suffix
    String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
    String key = UUID.randomUUID().toString()  + suffix;

    Auth auth = Auth.create(accessKey, secretKey);
    String upToken = auth.uploadToken(bucket);
    try {
        Response response = uploadManager.put(uploadBytes, key, upToken);
        //Analyze the result of successful upload
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        log.info(putRet.key);
        log.info(putRet.hash);
        return externaLink+"/"+key;
    } catch (QiniuException ex) {
        Response r = ex.response;
        log.error(r.toString());
        try {
            log.error(r.bodyString());
        } catch (QiniuException ex2) {
            //ignore
        }
    }
    return null;
}

1.2 client direct transmission

1.2.1 preparation

Bring back the voucher of the transfer service server: sometimes the client needs to obtain relevant information from the service server after the file is uploaded to qiniu. At this time, it needs to use the upload callback of qiniu and the setting of relevant callback parameters.

String accessKey = "access key";
String secretKey = "secret key";
String bucket = "bucket name";

Auth auth = Auth.create(accessKey, secretKey);
StringMap putPolicy = new StringMap();
//The callback address, the address of your own service, and qiniu cloud will notify you of the uploaded resource information to this address
putPolicy.put("callbackUrl", "http://api.example.com/qiniu/upload/callback");
//json data format of callback notification
putPolicy.put("callbackBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"fsize\":$(fsize)}");
//Format of callback data
putPolicy.put("callbackBodyType", "application/json");
//Expiration time of voucher
long expireSeconds = 3600;
//Generate voucher
String upToken = auth.uploadToken(bucket, null, expireSeconds, putPolicy);
System.out.println(upToken);

When the upload callback is used, the reply received by the client is the JSON format content of the business server response.

#Examples of callback contents are as follows
key=2020-10-1-15-52-4-768.png, hash=Fk-zGb08eAJvR5QTkCjBamCBo1s4, bucket=lx1, fsize=39262

The above is the code for generating the voucher. When submitting the upload form, we first request our own server to obtain the voucher, and then upload it with the voucher

The following is the front-end code given on the official website

<form method="post" action="http://upload.qiniup.com/"
 enctype="multipart/form-data">
  <input name="key" type="hidden" value="<resource_key>">
  <input name="x:<custom_name>" type="hidden" value="<custom_value>">
  <input name="token" type="hidden" value="<upload_token>">
  <input name="crc32" type="hidden" />
  <input name="accept" type="hidden" />
  <input name="file" type="file" />
  <input type="submit" value="Upload file" />
</form>

1.2.2 implementation of client direct transmission

(1) Join Thymeleaf

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

(2) Front end page (placed in templates under resources)

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>upload</title>
    </head>
    <body>
        <form id="upload" method="post" action="http://upload-z2.qiniup.com"
              enctype="multipart/form-data">
            <!--file name-->
            <input name="key" type="hidden" id="key">

            <!--autograph-->
            <input name="token" type="hidden" id="token">

            <input name="file" id="file" type="file" />
        </form>
        <button onclick="upload()">Upload file</button>

    </body>
    <!--jquery Put on resources lower static in-->
    <script src="/jquery/jquery-1.12.4.min.js"></script>
    <script>

        //upload
        function upload() {
            //Get the file name of the uploaded file
            var originalFileName=$("#file").val();
            console.log("originalFileName:"+originalFileName)
            //Gets the suffix of the file
            var suffix=originalFileName.substring(originalFileName.lastIndexOf("."),originalFileName.length)
            var date=new Date()
            //The file name is date
            var prefix=date.getFullYear()+"-"+date.getMonth()+"-"+date.getDay()
            +"-"+date.getHours()+"-"+date.getMinutes()+"-"+date.getSeconds()+"-"+date.getMilliseconds()

            //Set the file name in the form and submit it together
            $("#key").val(prefix+suffix)
            console.log("key:"+prefix+suffix)
            //This asynchronous request is used to obtain the upload credentials
            $.ajax({
                url: "/file/upToken",
                type: "post",
                success:function (resp) {
                    if (resp!=null){
                        console.log("resp:"+resp)
                        $("#token").val(resp)
                        //Form submission
                        $("#upload").submit()
                    }else {
                        alert("obtain token fail")
                    }
                }
            })
        }
    </script>
</html>

Next, we add a controller to this page

/**
 *
 * @EnableWebMvc This annotation has its own set of spring mvc default configuration. Using it will override the spring boot mvc default configuration
 * @Author Administrator
 * @date 11:27
 */
//@EnableWebMvc
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    /**
     * Configure simple automated controllers pre-configured with the response
     * status code and/or a view to render the response body. This is useful in
     * cases where there is no need for custom controller logic -- e.g. render a
     * home page, perform simple site URL redirects, return a 404 status with
     * HTML content, a 204 with no content, and more.
     *
     * @param registry
     * @see ViewControllerRegistry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/view/upload").setViewName("upload");
    }
}

(3) Seven cattle cloud callback

If you need a public network, you can use the intranet penetration tool

@RestController
@RequestMapping("/callback")
@Slf4j
public class CallbackController {

    @RequestMapping("/upload")
    public String upload(@RequestBody QiniuCallbackBody body) {
        log.info("QiniuCallbackBody:{}",body.toString());
        //Do something
        return null;
    }
}

Corresponding vo

@Data
public class QiniuCallbackBody {

    private String key;

    private String hash;

    private String bucket;

    private String fsize;
}

(4) Upload voucher

@PostMapping("/upToken")
public String getUpToken() {
    Auth auth = Auth.create(accessKey, secretKey);
    StringMap putPolicy = new StringMap();
    //Public network
    putPolicy.put("callbackUrl", "http://33833g4w32.wicp.vip:49739/callback/upload");
    putPolicy.put("callbackBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"fsize\":$(fsize)}");
    putPolicy.put("callbackBodyType", "application/json");
    long expireSeconds = 3600;
    String upToken = auth.uploadToken(bucketName, null, expireSeconds, putPolicy);
    log.info("upToken:{}",upToken);

    return upToken;
}

1.2.3 testing

visit http://localhost:9090/view/upload

Select a file, and then click the upload file button. The console will print the following contents, and then flash by

Page auto jump

idea console printing

2020-11-16 16:43:05.423  INFO 47448 --- [nio-9090-exec-5] c.l.qiniu.controller.CallbackController  : QiniuCallbackBody:QiniuCallbackBody(key=2020-10-1-16-43-3-343.png, hash=Fg_iscmKkXKEdbw1EMWUxxDaQkpN, bucket=lx1, fsize=82592)
2020-11-16 16:47:52.931  INFO 47448 --- [nio-9090-exec-1] cn.lx.qiniu.controller.FileController    : upToken:cCDDLE7qNzBcbAlpkOhqspM6Snim1UIREGTAfdcu:xM3tHR4MISJW8-l5Bc5hlam-PBw=:eyJjYWxsYmFja0JvZHlUeXBlIjoiYXBwbGljYXRpb24vanNvbiIsInNjb3BlIjoibHgxIiwiY2FsbGJhY2tVcmwiOiJodHRwOi8vMzM4MzNnNHczMi53aWNwLnZpcDo0OTczOS9jYWxsYmFjay91cGxvYWQiLCJkZWFkbGluZSI6MTYwNTUyMDA3MiwiY2FsbGJhY2tCb2R5Ijoie1wia2V5XCI6XCIkKGtleSlcIixcImhhc2hcIjpcIiQoZXRhZylcIixcImJ1Y2tldFwiOlwiJChidWNrZXQpXCIsXCJmc2l6ZVwiOiQoZnNpemUpfSJ9
2020-11-16 16:47:53.660  INFO 47448 --- [nio-9090-exec-3] c.l.qiniu.controller.CallbackController  : QiniuCallbackBody:QiniuCallbackBody(key=2020-10-1-16-47-52-920.png, hash=Fg_iscmKkXKEdbw1EMWUxxDaQkpN, bucket=lx1, fsize=82592)

Go to qiniu cloud object storage to check and find that the upload is successful

2 document management

2.1 obtaining document information

/**
     * Get the file information corresponding to the file name
     *
     * @param accessKey
     * @param secretKey
     * @param bucket
     * @param filename
     */
public static void getFileInfo(String accessKey, String secretKey, String bucket, String filename) {
    //Construct a configuration class with a specified Region object
    Configuration cfg = new Configuration(Region.region0());
    //... Other parameters refer to class notes
    Auth auth = Auth.create(accessKey, secretKey);
    BucketManager bucketManager = new BucketManager(auth, cfg);
    try {
        FileInfo fileInfo = bucketManager.stat(bucket, filename);
        log.info("hash:{}", fileInfo.hash);
        log.info("fsize:{}", fileInfo.fsize);
        log.info("mimeType:{}", fileInfo.mimeType);
        log.info("putTime:{}", fileInfo.putTime);
    } catch (QiniuException ex) {
        log.error(ex.response.toString());
    }
}

2.2 deleting files

 /**
     * Delete file
     *
     * @param accessKey
     * @param secretKey
     * @param bucket
     * @param filename
     */
    public static void delete(String accessKey, String secretKey, String bucket, String filename) {
        //Construct a configuration class with a specified Region object
        Configuration cfg = new Configuration(Region.region0());
        //... Other parameters refer to class notes
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket, filename);
        } catch (QiniuException ex) {
            //If an exception is encountered, the deletion fails
            log.error("code:{}", ex.code());
            log.error(ex.response.toString());
        }

    }

2.3 get file list

/**
     * Get file list
     *
     * @param accessKey
     * @param secretKey
     * @param bucket
     */
public static void list(String accessKey, String secretKey, String bucket) {
    //Construct a configuration class with a specified Region object
    Configuration cfg = new Configuration(Region.region0());
    //... Other parameters refer to class notes
    Auth auth = Auth.create(accessKey, secretKey);
    BucketManager bucketManager = new BucketManager(auth, cfg);

    //File Name Prefix 
    String prefix = "";
    //The maximum length limit of each iteration is 1000, and the recommended value is 1000
    int limit = 1000;
    //Specify the directory separator to list all public prefixes (simulating the effect of listing directories). The default value is an empty string
    String delimiter = "";

    //List space files
    BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, prefix, limit, delimiter);
    while (fileListIterator.hasNext()) {
        //Processing the obtained file list results
        FileInfo[] items = fileListIterator.next();
        for (FileInfo item : items) {
            log.info("start");
            log.info("hash:{}", item.hash);
            log.info("fsize:{}", item.fsize);
            log.info("mimeType:{}", item.mimeType);
            log.info("putTime:{}", item.putTime);
            log.info("end");
        }
    }
}

Tags: Java Spring Boot html

Posted by developer on Sat, 07 May 2022 09:57:31 +0300