博主首页

阿里云oss使用案例

  
上传文件



    private final AliyunOssProperties aliyunOssProperties;
    private final ClientBuilderConfiguration config;

 


public static OSS getOssClient(AliyunOssProperties aliyunOssProperties,ClientBuilderConfiguration config){ return new OSSClientBuilder() .build(aliyunOssProperties.getEndpoint(), aliyunOssProperties.getAccessKeyId(), aliyunOssProperties.getAccessKeySecret(),config); }
    public void postFileToOSS(OSS ossClient,String bucketName,String path,File file) throws IOException{
        boolean st = aliyunOssProperties.getSt();
        if(st){
            ossClient.putObject(bucketName,path,new FileInputStream(file));
        }
    }

获取文件  

    public File  getOSSObjectToFile(String objectName,String filePath){
        OSS ossClient = getOssClient(aliyunOssProperties,config);
        File file = new File(filePath);
        try{
            if(!file.getParentFile().exists()){
                Files.createDirectories(file.getParentFile().toPath());
            }
            if(!file.exists()){
                Files.createFile(file.toPath());
            }
            if(aliyunOssProperties.getSt()){
                ossClient.getObject(new GetObjectRequest(aliyunOssProperties.getBucketName(), objectName), file);
            }
        }finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return file;
    }

 

posted @ 2023-04-26 16:22  笑~笑  阅读(35)  评论(0编辑  收藏  举报