华为云obs上传文件

obs文档:https://support.huaweicloud.com/qs-obs/obs_qs_0005.html

1.引入依赖

 <dependency>
            <groupId>com.huaweicloudgroupId>
            <artifactId>esdk-obs-javaartifactId>
            <version>3.21.11version>
        dependency>

2.配置参数,

密钥获取
进入华为云登录控制台。
鼠标指向界面右上角的登录用户名,在下拉列表中单击“我的凭证”。
在左侧导航栏单击“访问密钥”。
单击“新增访问密钥”,进入“新增访问密钥”页面。
将新增的密钥下载到本地credentials.csv,打开后有对应的ak和sk

在这里插入图片描述

#obs
huawei:
  obs:
    ak:  #访问密钥
    sk:  #访问密钥
    upload:
      endPoint:  #上传的终端节点
    bucketName:  #桶,存放地点
    parentDir:  #上传的文件夹路径


3.配置类

@Configuration
public class ObsConfig {
    @Value("${huawei.obs.ak}")
    private String ak;

    @Value("${huawei.obs.sk}")
    private String sk;

    @Value("${huawei.obs.upload.endPoint}")
    private String endPoint;

    @Bean
    public ObsClient getObsClient(){
        ObsClient obsClient = new ObsClient(ak, sk, endPoint);
        return obsClient;
    }

}

4.工具类

@Component
public class ObsUtils {

    // @Autowired
    private static ObsClient obsClient;

    // @Value("${huawei.obs.bucketName}")
    private static String bucketName;

    //"antu/bdcjgxt/"
    private static String parentDir;

    //静态属性注入
    @Autowired
    public void setObsClient(ObsClient obsClient) {
        ObsUtils.obsClient = obsClient;
    }

    @Value("${huawei.obs.bucketName}")
    public void setBucketName(String bucketName) {
        ObsUtils.bucketName = bucketName;
    }
    @Value("${huawei.obs.parentDir}")
    public void setParentDir(String parentDir) {
        ObsUtils.parentDir = parentDir;
    }

    /**
     * 上传文件
     *
     * @param multipartFile 文件
     * @param objectKey     文件名,如果桶中有文件夹的话,如往test文件上传test.txt文件,那么objectKey就是test/test.txt
     * @throws Exception
     */
    public static PutObjectResult uploadFile(MultipartFile multipartFile, String objectKey) throws Exception {
        InputStream inputStream = multipartFile.getInputStream();
        PutObjectResult putObjectResult = obsClient.putObject(bucketName, parentDir+"/"+objectKey, inputStream);
        inputStream.close();
        //obsClient.close();
        return putObjectResult;
    }

    /**
     * 上传文件
     *
     * @param file      文件
     * @param objectKey 文件名,如果桶中有文件夹的话,如往test文件上传test.txt文件,那么objectKey就是test/test.txt
     * @throws Exception
     */
    public static PutObjectResult uploadFileByFile(File file, String objectKey) throws Exception {
        InputStream inputStream = new FileInputStream(file);
        PutObjectResult putObjectResult = obsClient.putObject(bucketName, parentDir+"/"+objectKey, inputStream);
        inputStream.close();
       // obsClient.close();
        return putObjectResult;
    }


    /**
     * 文件下载
     * @param objectKey 文件名
     * @return
     */
    public static ObsObject downFile(String objectKey){
        ObsObject obsObject = obsClient.getObject(bucketName, parentDir+"/"+objectKey);
       // InputStream ins = obsObject.getObjectContent();

        return obsObject;
    }

    /**
     * 文件下载
     * @param bucketName 桶
     * @param objectKey 文件名
     * @return
     */
    public static ObsObject downFile(String bucketName,String objectKey){
        ObsObject obsObject = obsClient.getObject(bucketName, objectKey);
       // InputStream ins = obsObject.getObjectContent();

        return obsObject;
    }
    /**
     * 删除文件
     *
     * @param objectKey 文件名,如果桶中有文件夹的话,如往test文件上传test.txt文件,那么objectKey就是test/test.txt
     * @throws Exception
     */
    public static DeleteObjectResult deleteFile(String objectKey) throws Exception {
        DeleteObjectResult deleteObjectResult = obsClient.deleteObject(bucketName, objectKey);
        //obsClient.close();
        return deleteObjectResult;
    }


}

5.使用

 //上传到obs
 PutObjectResult putObjectResult = ObsUtils.uploadFileByFile(file, fileName);

6.效果
华为云obs上传文件_第1张图片

你可能感兴趣的:(华为云,obs)