package com.doumee.core.utils.tyyun; 
 | 
  
 | 
import com.amazonaws.ClientConfiguration; 
 | 
import com.amazonaws.Protocol; 
 | 
import com.amazonaws.auth.AWSCredentials; 
 | 
import com.amazonaws.auth.AWSStaticCredentialsProvider; 
 | 
import com.amazonaws.auth.BasicAWSCredentials; 
 | 
import com.amazonaws.client.builder.AwsClientBuilder; 
 | 
import com.amazonaws.services.s3.AmazonS3; 
 | 
import com.amazonaws.services.s3.AmazonS3ClientBuilder; 
 | 
import com.amazonaws.services.s3.model.*; 
 | 
import com.doumee.core.oss.FileContent; 
 | 
import com.doumee.core.oss.FileModel; 
 | 
import com.doumee.core.utils.FileDigest; 
 | 
  
 | 
import java.io.*; 
 | 
import java.net.URL; 
 | 
import java.time.LocalDate; 
 | 
import java.time.format.DateTimeFormatter; 
 | 
import java.util.ArrayList; 
 | 
import java.util.List; 
 | 
import java.util.Map; 
 | 
  
 | 
import com.doumee.core.utils.ID; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
import org.apache.commons.lang3.StringUtils; 
 | 
import org.springframework.web.multipart.MultipartFile; 
 | 
  
 | 
@Slf4j 
 | 
public class TyyZosUtil { 
 | 
    private AmazonS3 client; 
 | 
  
 | 
    // public static final String ENDPOINT = "oss-cn-shanghai.aliyuncs.com"; 
 | 
  
 | 
    public TyyZosUtil(String END_POINT, String ACCESS_KEY, String SECRET_KEY) { 
 | 
        try { 
 | 
            AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY,SECRET_KEY); 
 | 
            ClientConfiguration awsClientConfig = new ClientConfiguration(); 
 | 
            awsClientConfig.setSignerOverride("AWSS3V4SignerType"); 
 | 
            awsClientConfig.setProtocol(Protocol.HTTP); 
 | 
  
 | 
            client = AmazonS3ClientBuilder.standard() 
 | 
                    .withCredentials(new 
 | 
                            AWSStaticCredentialsProvider(credentials)) 
 | 
                    .withClientConfiguration(awsClientConfig) 
 | 
                    .withEndpointConfiguration(new 
 | 
                            AwsClientBuilder.EndpointConfiguration(END_POINT, "")) 
 | 
                    .disableChunkedEncoding() 
 | 
                    .enablePathStyleAccess() 
 | 
                    .build(); 
 | 
        }catch (Exception e){ 
 | 
                log.error("对象存储====================连接天翼云ZOS失败"+e.getMessage()); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 上传文件 
 | 
     * 
 | 
     * @param bucketName 云端存放bucket名称 
 | 
     * @param key        重新命名的文件名 
 | 
     * @param filepath   将要上传的文件名称 
 | 
     * @throws FileNotFoundException 
 | 
     */ 
 | 
  
 | 
    public void uploadObject(String bucketName, String key, String filepath, 
 | 
                             String mime){ 
 | 
        try { 
 | 
            File file= new File(filepath); 
 | 
            PutObjectRequest request = new PutObjectRequest(bucketName, key, file); 
 | 
            PutObjectResult result = client.putObject(request); 
 | 
        }catch (Exception e){ 
 | 
            log.error("对象存储===================="+filepath+"文件上传失败"+e.getMessage()); 
 | 
        }finally { 
 | 
            shutDown(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 讲key文件存储在本地filename目标文件中 
 | 
     * @param bucketName 
 | 
     * @param key 
 | 
     * @param filename 
 | 
     */ 
 | 
    public void getObject(String bucketName, String key, String filename ){ 
 | 
        try { 
 | 
            GetObjectRequest request = new GetObjectRequest(bucketName, key); 
 | 
            S3Object result = client.getObject(request); 
 | 
            System.out.print("=====request success=====\n"); 
 | 
            try { 
 | 
                InputStream in = result.getObjectContent(); 
 | 
                File outputFile = new File(filename); 
 | 
                FileOutputStream outputStream = new 
 | 
                        FileOutputStream(outputFile); 
 | 
                byte[] read_buf = new byte[1024 * 1024]; 
 | 
                int read_len = 0; 
 | 
                while ((read_len = in.read(read_buf)) > 0) { 
 | 
                    outputStream.write(read_buf, 0, read_len); 
 | 
                } 
 | 
                in.close(); 
 | 
                outputStream.close(); 
 | 
            } catch (IOException e){ 
 | 
                e.printStackTrace(); 
 | 
            } 
 | 
        }catch (Exception e){ 
 | 
            log.error("对象存储===================="+filename+"文件读取失败"+e.getMessage()); 
 | 
        }finally { 
 | 
            shutDown(); 
 | 
        } 
 | 
    } 
 | 
    public void deleteObject(String bucketName, String key  ){ 
 | 
        try { 
 | 
            DeleteObjectRequest request = new DeleteObjectRequest(bucketName, key); 
 | 
            client.deleteObject(request); 
 | 
            System.out.print("=====request deleteObject success====="); 
 | 
        }catch (Exception e){ 
 | 
            log.error("对象存储===================="+key+"文件删除失败"+e.getMessage()); 
 | 
        }finally { 
 | 
            shutDown(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 关闭 
 | 
     * 
 | 
     * @throws FileNotFoundException 
 | 
     */ 
 | 
  
 | 
    public void shutDown() { 
 | 
        if (client != null) { 
 | 
            // 关闭client 
 | 
            client.shutdown(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    public static void main(String[] args) { 
 | 
        TyyZosUtil aLiYunUtil = new TyyZosUtil("", "uc4nnpsqep1i9fijqr37nokh", 
 | 
                "/rp41xCx/XdGEVCptdH6v7xpc9w="); 
 | 
        // aLiYunUtil.uploadObject("pongto", "work/li2.txt", "D://哈.txt", 
 | 
        // ".html,.html text/html");D://装机软件/办公学习 
 | 
        // aLiYunUtil.partUploadObject("pongto", "work/ps.exe", 
 | 
        // "D://装机软件/办公学习/Adobe_Illustrator_CS6_XiaZaiBa.exe", 
 | 
        // ".html,.html text/html"); 
 | 
        // aLiYunUtil.deleteBucket("pongto"); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 上传网络文件 
 | 
     * 
 | 
     * @param bucketName 云端存放bucket名称 
 | 
     * @param key        重新命名的文件名 
 | 
     *                   将要上传的文件名称 
 | 
     * @throws IOException 
 | 
     */ 
 | 
  
 | 
    public boolean uploadOnlineObject(String url, String bucketName, String key )     { 
 | 
        try { 
 | 
  
 | 
            InputStream inputStream = new URL(url).openStream(); 
 | 
            if (inputStream != null) { 
 | 
                ObjectMetadata metadata = new ObjectMetadata(); 
 | 
                PutObjectRequest request = new PutObjectRequest(bucketName, key, inputStream,metadata); 
 | 
                PutObjectResult result = client.putObject(request); 
 | 
            }else { 
 | 
                log.error("对象存储===================="+url+"网络文件上传失败,网络文件读取失败"); 
 | 
            } 
 | 
        }catch (Exception e){ 
 | 
            log.error("对象存储===================="+url+"网络文件上传失败"+e.getMessage()); 
 | 
        }finally { 
 | 
            shutDown(); 
 | 
        } 
 | 
        return false; 
 | 
  
 | 
    } 
 | 
    public boolean uploadInputstreamObject(InputStream inputStream, String bucketName, String key )     { 
 | 
        try { 
 | 
  
 | 
            if (inputStream != null) { 
 | 
                ObjectMetadata metadata = new ObjectMetadata(); 
 | 
                metadata.setContentLength(inputStream.available()); 
 | 
  
 | 
                PutObjectRequest request = new PutObjectRequest(bucketName, key, inputStream,metadata); 
 | 
                request.setCannedAcl(CannedAccessControlList.PublicRead); 
 | 
                PutObjectResult result = client.putObject(request); 
 | 
                return true; 
 | 
            } 
 | 
        }catch (Exception e){ 
 | 
            log.error("对象存储==================== 文件上传失败"+e.getMessage()); 
 | 
        }finally { 
 | 
            shutDown(); 
 | 
        } 
 | 
        return false; 
 | 
  
 | 
    } 
 | 
  
 | 
} 
 |