All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jackpanz.spring.util.OSSUtils Maven / Gradle / Ivy

The newest version!
package com.github.jackpanz.spring.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.github.jackpanz.spring.initialization.ConstantsConfigurer;
import org.apache.commons.collections4.CollectionUtils;

import java.io.InputStream;
import java.util.List;

/**
 * Created by jack on 16/8/21.
 */
public class OSSUtils {

    public static void copyOSS(String sourceKey,String destinationKey) {
        OSSClient client = new OSSClient(ConstantsConfigurer.endpoint, ConstantsConfigurer.accessKeyId, ConstantsConfigurer.accessKeySecret);
        client.copyObject(ConstantsConfigurer.bucketName, sourceKey , ConstantsConfigurer.bucketName , destinationKey);
        client.shutdown();
    }

    public static boolean existObject(String ssoKeyName){
        OSSClient client = new OSSClient(ConstantsConfigurer.endpoint, ConstantsConfigurer.accessKeyId, ConstantsConfigurer.accessKeySecret);
        boolean isExist = client.doesObjectExist(ConstantsConfigurer.bucketName,ssoKeyName);
        client.shutdown();
        return isExist;
    }

    public static void uploadToOSS(String ssoKeyName, InputStream inputStream) {
        OSSClient client = new OSSClient(ConstantsConfigurer.endpoint, ConstantsConfigurer.accessKeyId, ConstantsConfigurer.accessKeySecret);
        client.putObject(ConstantsConfigurer.bucketName, ssoKeyName, inputStream);
        client.shutdown();
    }

    public static void deleteFileToOSS(String ssoKeyName) {
        OSSClient client = new OSSClient(ConstantsConfigurer.endpoint, ConstantsConfigurer.accessKeyId, ConstantsConfigurer.accessKeySecret);
        client.deleteObject(ConstantsConfigurer.bucketName, ssoKeyName);
        client.shutdown();
    }

    public static void deleteFileToOSS(List ssoKeyNames) {
        if (CollectionUtils.isNotEmpty(ssoKeyNames)) {
            OSSClient client = new OSSClient(ConstantsConfigurer.endpoint, ConstantsConfigurer.accessKeyId, ConstantsConfigurer.accessKeySecret);
            DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(ConstantsConfigurer.bucketName);
            deleteObjectsRequest.setKeys(ssoKeyNames);
            client.deleteObjects(deleteObjectsRequest);
            client.shutdown();
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy