Abstract:

This article introduces the GitClub small program back-end server file migration and update pictures uploaded to Ali Cloud OSS storage server, if you do not know GitClub small program friends can see my last article about Android open source library sharing platform, (GitClub) wechat small program development experience, in this special correction, The current version of the Android open source library to share, the future will add Java, iOS, front-end and other different language classification, interested can go to pay attention to our small program, source address: GitClub. Please scan this QR code to log in GitClub applet.

1. Import the dependency package and add it to pox. XML

< the dependency > < groupId > com. Aliyun. Oss < / groupId > < artifactId > aliyun - SDK - oss < / artifactId > < version > 2.8.3 < / version > </dependency>Copy the code

2. Configure the OSS parameters and create the OSSConfig class

1. Add the oss.properties configuration file as follows:# Ali Cloud OSS configuration
# original server address
bucketUrl = https://bucketName.oss-cn-shenzhen.aliyuncs.com
# Customize the server address after parsing
baseUrl = https://xxx.502tech.com
You can select another address
endpoint = https://oss-cn-qingdao.aliyuncs.com
# Bucket already created on the console
bucketName = bucketName
# Save path of the file you uploaded. If bucket does not exist, create it.
picLocation = GitClub/image/
# The corresponding ID and key value, please fill in your specific value, here is not convenient to show my own.OSSConfig public class OSSConfig {accessKeyId = accessKeyId = accessKeySecret private String bucketUrl; Private String baseUrl; // Customize the image server address after parsing private String endpoint; // Connection area address private String accessKeyId; KeyId private String accessKeySecret; Private String bucketName; // bucketName private String picLocation; // Image save path publicOSSConfig() {
        try {
            this.bucketUrl = SystemConfig.getConfigResource("bucketUrl");
            this.baseUrl = SystemConfig.getConfigResource("baseUrl");
            this.endpoint = SystemConfig.getConfigResource("endpoint");
            this.bucketName = SystemConfig.getConfigResource("bucketName");
            this.picLocation = SystemConfig.getConfigResource("picLocation");
            this.accessKeyId = SystemConfig.getConfigResource("accessKeyId");
            this.accessKeySecret = SystemConfig.getConfigResource("accessKeySecret"); } catch (IOException e) { e.printStackTrace(); }}... Omit the get,setmethodsCopy the code

Create the OSS tool class OSSUploadUtil

private static OSSConfig config = null; /** ** @methodName: uploadFile * @description: UPLOAD a single file * @param file * @param fileType File suffix * @return*/ public static String uploadFile(File File,String fileType){config = config == null? new OSSConfig():config; String fileName = config.getPicLocation() + uuID.randomuuid ().toString().toupperCase ().replace()"-"."")
                +"."+fileType;
        returnputFile(file,fileType,fileName); } /** ** @methodName: updateFile * @description: Updates the file: only the content is updated, but the file name and file address are not updated. * @param file * @param fileType * @param oldUrl * @return String
     */
    public static String updateFile(File file,String fileType,String oldUrl){
        String fileName = getFileName(oldUrl);
        if(fileName==null) return null;
        returnputFile(file,fileType,fileName); } /** * * @MethodName: replaceFile * @Description: Replace file: delete the original file and upload a new file, replace the file name and address at the same time * solve the problem of raw data cache, as long as the address is updated, * @param file * @param fileType file suffix * @param oldUrl The address of the file to be deleted * @returnString File address */ public static String replaceFile(File File,String fileType,String oldUrl){Boolean flag = deleteFile(oldUrl); // Delete the original file firstif(! Flag){// Change the expiration time of the file so that it is automatically deleted when it expires. }returnuploadFile(file, fileType); } /** ** @methodName: deleteFile * @description: single file deletion * @param fileUrl Url of the file to be deleted * @return*/ public static Boolean deleteFile(String fileUrl){config = config == null? new OSSConfig():config; String bucketName = OSSUploadUtil.getBucketName(fileUrl); / / based on the url to obtain bucketName String fileName = OSSUploadUtil. GetFileName (fileUrl); // Get fileName from the URLif(bucketName==null||fileName==null) return false;
        OSSClient ossClient = null;
        try {
            ossClient = new OSSClient(config.getEndpoint(), config.getAccessKeyId(), config.getAccessKeySecret());
            GenericRequest request = new DeleteObjectsRequest(bucketName).withKey(fileName);
            ossClient.deleteObject(request);
        } catch (Exception oe) {
            oe.printStackTrace();
            return false;
        } finally {
            ossClient.shutdown();
        }
        return true; } /** * * @MethodName: batchDeleteFiles * @Description: Batch file deletion (fast) : applies to the same endPoint and BucketName * @param fileUrls set of fileUrls to be deleted * @return*/ public static int deleteFile(List<String> fileUrls){int deleteCount = 0; / / the number of deleted successfully String bucketName = OSSUploadUtil. GetBucketName (fileUrls. Get (0)); / / based on the url to obtain bucketName List < String > fileNames. = OSSUploadUtil getFileName (fileUrls); // Get fileName from the URLif(bucketName==null||fileNames.size()<=0) return 0;
        OSSClient ossClient = null;
        try {
            ossClient = new OSSClient(config.getEndpoint(), config.getAccessKeyId(), config.getAccessKeySecret());
            DeleteObjectsRequest request = new DeleteObjectsRequest(bucketName).withKeys(fileNames);
            DeleteObjectsResult result = ossClient.deleteObjects(request);
            deleteCount = result.getDeletedObjects().size();
        } catch (OSSException oe) {
            oe.printStackTrace();
            throw new RuntimeException("OSS service exception :", oe);
        } catch (ClientException ce) {
            ce.printStackTrace();
            throw new RuntimeException("OSS client exception :", ce);
        } finally {
            ossClient.shutdown();
        }
        returndeleteCount; } /** * * @MethodName: batchDeleteFiles * @Description: Batch file deletion (slow) : applies to different endpoints and BucketName * @param fileUrls set of fileUrls to be deleted * @return*/ public static int deleteFiles(List<String> fileUrls){int count = 0;for (String url : fileUrls) {
            if(deleteFile(url)){ count++; }}returncount; } /** ** @methodName: putFile * @description: Upload a file * @param file * @param fileType * @param fileName * @returnString */ private static String putFile(File file, String fileType, String fileName){ config = config==null? new OSSConfig():config; String url = null; OSSClient = null; try { ossClient = new OSSClient(config.getEndpoint(), config.getAccessKeyId(), config.getAccessKeySecret()); InputStream input = new FileInputStream(file); ObjectMetadata meta = new ObjectMetadata(); / / create upload Object Metadata meta. SetContentType (OSSUploadUtil. ContentType fileType ()); // Set the upload content type meta-.setCachecontrol ("no-cache"); PutObjectRequest request = new PutObjectRequest(config.getBucketName(), fileName,input,meta); // Create the upload request ossclient.putobject (request); Date expiration = new Date(new Date().getTime() + 3600L * 1000 * 24 * 365 * 10); / / set the URL expiration time for 10 years, 3600 * 1000 * 24 l * 365 * 10 / / upload success again return to the file path of the URL = ossClient. GeneratePresignedUrl (config. GetBucketName (), fileName, expiration) .toString() .replaceFirst(config.getBucketUrl(), config.getBaseUrl()); } catch (OSSException | FileNotFoundException | ClientException oe) { oe.printStackTrace();return null;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        returnurl; } /** ** @methodname: contentType * @description: gets the fileType * @param fileType * @return String
     */
    private static String contentType(String fileType){
        fileType = fileType.toLowerCase();
        String contentType = "";
        switch (fileType) {
            case "bmp":	contentType = "image/bmp";
                break;
            case "gif":	contentType = "image/gif";
                break;
            case "png":
            case "jpeg":
            case "jpg":	contentType = "image/jpeg";
                break;
            case "html":contentType = "text/html";
                break;
            case "txt":	contentType = "text/plain";
                break;
            case "vsd":	contentType = "application/vnd.visio";
                break;
            case "ppt":
            case "pptx":contentType = "application/vnd.ms-powerpoint";
                break;
            case "doc":
            case "docx":contentType = "application/msword";
                break;
            case "xml":contentType = "text/xml";
                break;
            case "mp4":contentType = "video/mp4";
                break;
            default: contentType = "application/octet-stream";
                break;
        }
        returncontentType; } /** ** @methodname: getBucketName * @description: obtain bucketName * @param fileUrl fileUrl * @return String bucketName
     */
    private static String getBucketName(String fileUrl){
        String http = "http://";
        String https = "https://";
        int httpIndex = fileUrl.indexOf(http);
        int httpsIndex = fileUrl.indexOf(https);
        int startIndex  = 0;
        if(httpIndex==-1){
            if(httpsIndex==-1){
                return null;
            }else{ startIndex = httpsIndex+https.length(); }}else{
            startIndex = httpIndex+http.length();
        }
        int endIndex = fileUrl.indexOf(".oss-");
        returnfileUrl.substring(startIndex, endIndex); } /** ** @methodname: getFileName * @description: obtains fileName * @param fileUrl fileUrl * @ according to the urlreturn String fileName
     */
    private static String getFileName(String fileUrl){
        String str = "aliyuncs.com/";
        int beginIndex = fileUrl.indexOf(str);
        if(beginIndex==-1) return null;
        returnfileUrl.substring(beginIndex+str.length()); } /** ** @methodName: getFileName * @description: Get the fileNames collection * @param fileUrls file url * @returnList<String> fileName collection */ private static List<String> getFileName(List<String> fileUrls){List<String> names = new ArrayList<>();for (String url : fileUrls) {
            names.add(getFileName(url));
        }
        returnnames; }}Copy the code

Four, test,

// Change the logic uploaded to the project-specific directory to upload to oss@postmapping ("/uploadArticleImg")
    public Result<Map<String, String>> uploadArticleImg(@RequestParam(value = "article_img") MultipartFile file) {
        if (file == null || file.isEmpty() || file.getSize() == 0) {
            return ResultUtils.error(ResultCode.UPLOAD_FILE_EMPTY);
        }
        if (file.getSize() > 10 * 1024 * 1024) {
            return ResultUtils.error(ResultCode.UPLOAD_FILE_LIMIT);
        }
        Map<String, String> map = new HashMap<>();
        String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1); // Url on the OSS storage server after successfully uploading a single file String fileName = ossuploadUtil.uploadfile (fileutils.multi2file (file), fileType); map.put(file.getName(), fileName);return ResultUtils.ok(map);
    }
Copy the code

5. How to update the URL of previously uploaded files in the database to the URL returned by OSS

@PostMapping("/trasform2OSS")
    public Result<Boolean> trasformImg2OSS(){
        File path = null;
        try {
            path = new File(ResourceUtils.getURL("classpath:").getPath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if(! path.exists()) path = new File("");
        System.out.println("path:"+ path.getAbsolutePath()); File upload = new File(path.getabsolutePath (),"static/images/upload/");
        if(! upload.exists()) upload.mkdirs(); System.out.println("upload url:"+ upload.getAbsolutePath()); File[] files = upload.listFiles(); / / update the url of the picture to the oss and the latest pictures of oss address saved to the database List < Article > articles = articleRepository. The.findall (); articles.forEach(article -> {if(null ! = article){for (File file : files){
                    if(null ! = file){if(null == article.getImg_url() || null == file.getName()){
                            continue;
                        }
                        if(article.getimg_url ().contains(file.getName())){// Upload to oss String fileType = file.getName().substring(file.getName().lastIndexOf(".") + 1); String url = OSSUploadUtil.uploadFile(file, fileType); logger.info("Upload to OSS address:"+url); // Update to the server article. SetImg_url (url); articleRepository.saveAndFlush(article); / / update the data to the engine articleSearchRepository. Save (new ESArticle (article)); }}}}});return ResultUtils.ok(true);
    }
Copy the code

Six, Ali cloud OSS management configuration of some pits

Because previously our images were uploaded to the project directory in the specified path such as: 502tech.com/geekdaily/x… Note: xxx.502tech.com is our secondary domain name. After uploading files to OSS, the default return will be as follows: BucketName.oss-cn-shenzhen.aliyuncs.com/123.png format. If you want to use HTTPS, you need to apply for an SSL certificate. Here you can apply for a free one. You can search for it on your own, and the application will be successful in about 15 minutes. Download the nginx version of the certificate, and then copy and paste the corresponding public and private keys.