Java uses Alibaba cloud OSS and SMS

Posted by StroiX on Sun, 02 Feb 2020 12:47:37 +0100

Alibaba cloud OSS and SMS tools

Import dependency

<dependency>
    <groupId>cn.gjing</groupId>
    <artifactId>tools-aliyun</artifactId>
    <version>1.0.0</version>
</dependency>

I. OSS

1, configuration

The following configurations are required except for the last four

aliyun:
  # User key, obtained in alicloud
  access-key: xxxxxxx
  # User secret key, obtained in alicloud
  access-key-secret: xxxxxxx
  oss:
   # Node, go to Alibaba cloud to check
   end-point: xxxxxxxx
   # Storage space, it will be created if it does not exist
   bucket: xxxxxxx
   # Maximum connections, default: 1024
   max-connections: 1024
   # Maximum idle time (MS), default 60000
   idle-time: 60000
   # socket timeout (MS), default 50000
   socket-timeout: 50000
   # Connection timeout (MS), default 50000
   connection-timeout: 50000

2. File upload

If there is a directory, you can specify the directory name. If the directory does not exist, it will be created. If the upload succeeds, the oss file name will be returned

/**
 * @author Gjing
 **/
@RestController
public class TestController {
    @Resource
    private OssUpload ossUpload;

    @PostMapping("/file")
    public String upload(MultipartFile file) {
        // Upload by file
        return this.ossUpload.upload(file);
    }

    @PostMapping("/file2")
    public String upload(MultipartFile file) {
        // Upload to test directory
        return this.ossUpload.upload(file, "test");
    }

    @PostMapping("/file3")
    public String upload(MultipartFile file) throws IOException {
        // To upload through stream or byte [], you need to specify the file name
        return this.ossUpload.upload(file.getInputStream(), UUID.randomUUID().toString()+".jpg");
    }
}

The parameters of the upload() method are as follows

parameter describe
fileName The file name after uploading to oss will be overwritten if it is repeated. If you want to upload to the specified directory, you can use the directory as the file name, such as files/1.jpg
file File, file stream, byte array
dir The directory name corresponding to the directory saved to the bucket

3. File deletion

Using the batch deletion method will return a list of file names that have been successfully deleted after the execution. The maximum number of file names to be deleted in batch deletion is 1000 at the same time

/**
 * @author Gjing
 **/
@RestController
public class TestController {
    @Resource
    private OssUpload ossUpload;

    @DeleteMapping("/test1")
    public void deleteFile(String fileName) {
        // Delete the specified oss file
        this.ossUpload.deleteFile(fileName);
    }

    @DeleteMapping("/test2")
    public String deleteFiles(String[] fileNames) {
        // Batch deletion
        return this.ossUpload.deleteFiles(Arrays.asList(fileNames)).toString();
    }
}

The parameters of deleteFile() and deleteFiles() methods are as follows

parameter describe
fileName oss file name, which will be returned to you after upload
fileNames oss file name collection, up to 1000 deleted at the same time

4. Judge whether the file exists

/**
 * @author Gjing
 **/
@RestController
public class TestController {
    @Resource
    private OssDownload ossDownload;
    
    @PostMapping("/test")
    public boolean test(String fileName) {
        return this.ossDownload.isExist(fileName);
    }
}

The parameters of the isExist() method are as follows

parameter describe
fileName oss file name, which will be returned to you after upload

5. File download

/**
 * @author Gjing
 **/
@RestController
public class TestController {
    @Resource
    private OssDownload ossDownload;

    @GetMapping("/test")
    public void downLocal(String fileName, HttpServletResponse response) {
        // 1. Download to local specified directory
        this.ossDownload.downByLocal("/Users/colin/Desktop/1/", fileName);
        // 2. Download via stream
        this.ossDownload.downByStream(fileName, response);
    }
}

The downByLocal() and downByStream() method parameters are as follows

parameter describe
fileName oss file name, which will be returned to you after upload
dir Local file directory address, which will be created if it does not exist

Two, SMS

1, configuration

The following configuration is required except for region

aliyun:
  # User key, obtained in alicloud
  access-key: xxxxxxx
  # User secret key, obtained in alicloud
  access-key-secret: xxxxxx
  sms:
    # SMS template ID, which must be added and approved
    template-code: xxxxxxx
    # SMS signature name, which must be added and approved
    sign-name: xxxxxx
    # Zone, default
    region: default

2. Send SMS

Actual value corresponding to SMS template variable

/**
 * @author Gjing
 **/
@RestController
public class SmsController {
    @Resource
    private SmsHelper smsHelper;

    @PostMapping("/sms")
    public String send(String phones) {
        Map<String, Integer> param = new HashMap<>();
        param.put("code", 12345);
        return this.smsHelper.send(phones, param);
    }
}

The send() method has the following parameters

parameter describe
phones 11 mobile phone numbers separated by English commas, up to 1000
templateCode SMS template code, must be existing and approved
signName SMS signature name, which must be existing and approved
param Actual value corresponding to SMS template variable

3. Query the sending record of the specified number

/**
 * @author Gjing
 **/
@RestController
public class SmsController {
    @Resource
    private SmsHelper smsHelper;

    @GetMapping("/sms_record")
    public String findSmsRecord(String phone) {
        return this.smsHelper.findSendDetail(phone, "2020-02-01", 1, 5);
    }
}

The findSendDetail() method has the following parameters

parameter describe
phone 11 digit mobile number
sendDate Send date, format: yyyy MM DD
page The number of pages
row Number of pieces per page, max. 50

Source code address: tools-aliyun

Topics: Java socket Mobile