0
点赞
收藏
分享

微信扫一扫

阿里云oss使用案例

hwwjian 2023-04-29 阅读 84

上传文件

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;
    }

 



举报

相关推荐

0 条评论