package com.doumee.core.utils.huaweiOBS; 
 | 
  
 | 
import com.azure.storage.blob.BlobContainerClient; 
 | 
import com.azure.storage.blob.BlobServiceClient; 
 | 
import com.azure.storage.blob.BlobServiceClientBuilder; 
 | 
import com.doumee.core.utils.azure.AzureBlobUtil; 
 | 
import com.obs.services.BasicObsCredentialsProvider; 
 | 
import com.obs.services.ObsClient; 
 | 
import com.obs.services.exception.ObsException; 
 | 
import com.obs.services.model.*; 
 | 
  
 | 
import java.io.ByteArrayInputStream; 
 | 
import java.io.File; 
 | 
import java.io.IOException; 
 | 
import java.io.InputStream; 
 | 
import java.net.URL; 
 | 
import java.util.List; 
 | 
  
 | 
/** 
 | 
 * @date 2020/4/28 
 | 
 */ 
 | 
public class ObsUtil { 
 | 
  
 | 
    public ObsClient obsClient; 
 | 
    public     String accessKey; 
 | 
    public   String accessId; 
 | 
    public    String endpoint; 
 | 
  
 | 
    public BlobContainerClient blobContainerClient ; 
 | 
  
 | 
    public ObsUtil(String accessId, String accessKey, String endpoint){ 
 | 
        try { 
 | 
            this.accessId = accessId; 
 | 
            this.accessKey = accessKey; 
 | 
            this.endpoint = endpoint; 
 | 
            this.obsClient = new ObsClient(new BasicObsCredentialsProvider(accessId, accessKey), this.endpoint); 
 | 
        }catch (Exception e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
  
 | 
    } 
 | 
    private void initClient() { 
 | 
        this.obsClient = new ObsClient(new BasicObsCredentialsProvider(this.accessId, this.accessKey), this.endpoint); 
 | 
    } 
 | 
    private void uploadLocalFile(File file,String bucket,String key) { 
 | 
        try { 
 | 
            if(this.obsClient == null){ 
 | 
                initClient(); 
 | 
            } 
 | 
            // 文件上传 
 | 
            // localfile 为待上传的本地文件路径,需要指定到具体的文件名 
 | 
            PutObjectRequest request = new PutObjectRequest(); 
 | 
            request.setBucketName(bucket); 
 | 
            request.setObjectKey(key); 
 | 
            request.setFile(file); 
 | 
            obsClient.putObject(request); 
 | 
            System.out.println("putObject successfully"); 
 | 
        } catch (ObsException e) { 
 | 
            System.out.println("putObject failed"); 
 | 
            // 请求失败,打印http状态码 
 | 
            System.out.println("HTTP Code:" + e.getResponseCode()); 
 | 
            // 请求失败,打印服务端错误码 
 | 
            System.out.println("Error Code:" + e.getErrorCode()); 
 | 
            // 请求失败,打印详细错误信息 
 | 
            System.out.println("Error Message:" + e.getErrorMessage()); 
 | 
            // 请求失败,打印请求id 
 | 
            System.out.println("Request ID:" + e.getErrorRequestId()); 
 | 
            System.out.println("Host ID:" + e.getErrorHostId()); 
 | 
            e.printStackTrace(); 
 | 
        } catch (Exception e) { 
 | 
            System.out.println("putObject failed"); 
 | 
            // 其他异常信息打印 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 上传文件 
 | 
     * @param is 
 | 
     * @param objectKey 
 | 
     * @return 
 | 
     * @throws IOException 
 | 
     */ 
 | 
    public  boolean uploadFile(String bucketName,InputStream is, String objectKey) throws Exception { 
 | 
        if(this.obsClient == null){ 
 | 
            initClient(); 
 | 
        } 
 | 
  
 | 
        try { 
 | 
            // 上传字符串(byte数组) 
 | 
            obsClient.putObject(bucketName, objectKey, is); 
 | 
            System.out.println("putObject successfully"); 
 | 
        } catch (ObsException e) { 
 | 
            System.out.println("putObject failed"); 
 | 
            // 请求失败,打印http状态码 
 | 
            System.out.println("HTTP Code:" + e.getResponseCode()); 
 | 
            // 请求失败,打印服务端错误码 
 | 
            System.out.println("Error Code:" + e.getErrorCode()); 
 | 
            // 请求失败,打印详细错误信息 
 | 
            System.out.println("Error Message:" + e.getErrorMessage()); 
 | 
            // 请求失败,打印请求id 
 | 
            System.out.println("Request ID:" + e.getErrorRequestId()); 
 | 
            System.out.println("Host ID:" + e.getErrorHostId()); 
 | 
           return false; 
 | 
        } catch (Exception e) { 
 | 
            System.out.println("putObject failed"); 
 | 
            // 其他异常信息打印 
 | 
            e.printStackTrace(); 
 | 
            return false; 
 | 
        }finally { 
 | 
            if(this.obsClient!=null){ 
 | 
                this.obsClient.close(); 
 | 
            } 
 | 
        } 
 | 
  
 | 
       return true; 
 | 
    } 
 | 
  
 | 
    public static void main(String[] args) { 
 | 
        ObsUtil blobUtil = new ObsUtil("HPUAQVBRXX9A9TLZ3RTA","uHC2uoFh42Z2xgQmCBBtG8rNZ4Caf85qQ2DQqZZf","obs.cn-south-1.myhuaweicloud.com"); 
 | 
        blobUtil.uploadLocalFile(new File("D://static/1.png"),"jinkuai","member/1.png"); 
 | 
//        blobUtil.uploadLocalFile(new File("D://static/2.png"),"jinkuai","identity/2.png"); 
 | 
//        blobUtil.uploadLocalFile(new File("D://static/3.png"),"jinkuai","identity/3.png"); 
 | 
//        blobUtil.uploadLocalFile(new File("D://static/4.png"),"jinkuai","identity/4.png"); 
 | 
    } 
 | 
    public static  Integer uploadNetFile(ObsClient obsClient,String bucketName,String url, String objectKey) throws IOException { 
 | 
        InputStream is = new URL(url).openStream(); 
 | 
        if(is != null){ 
 | 
            Boolean flag = obsClient.doesObjectExist(bucketName, objectKey); 
 | 
            PutObjectResult result = null; 
 | 
            result = obsClient.putObject(bucketName, objectKey, is); 
 | 
            obsClient.close(); 
 | 
            return result.getStatusCode(); 
 | 
        } 
 | 
        //同名文件可能被覆盖 
 | 
        return null; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 获得所有文件 
 | 
     * @return 
 | 
     * @throws IOException 
 | 
     */ 
 | 
    public List<ObsObject> getAllFileInfo( ObsClient obsClient,String bucketName) throws IOException { 
 | 
        ObjectListing objectList = obsClient.listObjects(bucketName); 
 | 
        List<ObsObject> list = objectList.getObjects(); 
 | 
        obsClient.close(); 
 | 
        return list; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 删除文件 
 | 
     * @param objectKey 
 | 
     * @return 
 | 
     * @throws IOException 
 | 
     */ 
 | 
    public Boolean removeFile(ObsClient obsClient,String bucketName,String objectKey) throws IOException { 
 | 
        boolean exist = obsClient.doesObjectExist(bucketName, objectKey); 
 | 
        DeleteObjectResult result = null; 
 | 
        if (exist) { 
 | 
            result = obsClient.deleteObject(bucketName, objectKey); 
 | 
        } 
 | 
        obsClient.close(); 
 | 
        //是否可以被标记为删除 
 | 
        return result.isDeleteMarker(); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 获取文件对象-下载 
 | 
     * @param objectKey 
 | 
     * @return 
 | 
     */ 
 | 
    public ObsObject getFile(ObsClient obsClient,String bucketName,String objectKey) { 
 | 
        boolean exist = obsClient.doesObjectExist(bucketName, objectKey); 
 | 
        if (exist) { 
 | 
            ObsObject object = obsClient.getObject(bucketName, objectKey); 
 | 
            return object; 
 | 
        } 
 | 
        return null; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 预览授权访问-支持流式文件 
 | 
     * 如果是流式文件,返回的链接可以在浏览器预览 
 | 
     * 如果是非流式文件,返回的链接可以在浏览器里下载文件 
 | 
     * @param objectKey 
 | 
     * @return 
 | 
     * @throws IOException 
 | 
     */ 
 | 
    public String preview(ObsClient obsClient,String bucketName,String objectKey) throws IOException { 
 | 
        //300有效时间 
 | 
        TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, 300); 
 | 
        request.setBucketName(bucketName); 
 | 
        request.setObjectKey(objectKey); 
 | 
        TemporarySignatureResponse response = obsClient.createTemporarySignature(request); 
 | 
        obsClient.close(); 
 | 
        return response.getSignedUrl(); 
 | 
    } 
 | 
  
 | 
} 
 |