SpringBoot整合华为云OBS

   一、参考项

华为云 OBS(官网): 对象存储服务OBS官网_海量安全高可靠_数据云存储解决方案-华为云
OBS SDK for Java(官网): SDK下载_对象存储服务 OBS_Java_华为云

三、引入Pom文件

<!-- https://mvnrepository.com/artifact/com.huaweicloud/esdk-obs-java -->
<dependency>
    <groupId>com.huaweicloud</groupId>
    <artifactId>esdk-obs-java</artifactId>
    <version>3.21.12</version>
</dependency>

二、定义抽象类

public abstract class BaseObjectStorage {

    /**
     * 上传文件
     *
     * @param pathAndName
     * @param file
     */
    public abstract void upload(String pathAndName, File file);

    /**
     * 授权
     *
     * @param pathAndName
     * @param time
     * @return
     */
    public abstract String authorize(String pathAndName, long time);

    /**
     * 授权(路径全)
     *
     * @param pathAndName
     * @param time
     * @return
     */
    public abstract String authorizeAllName(String pathAndName, long time);

    /**
     * 临时上传文件授权
     *
     * @param dir
     * @return
     */
    public abstract Map<String, Object> tokens(String dir);

    /**
     * 删除文件
     *
     * @param pathAndName
     */
    public abstract void deleteFile(String pathAndName);

}

三、OSS实现类

package cn.xhh.xhh.core.objectstorage;

import com.google.common.collect.Maps;
import com.obs.services.ObsClient;
import com.obs.services.model.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class ObsObjectStorage extends BaseObjectStorage {

  @Data
  @Component
  @ConfigurationProperties(prefix = "obs")
  public static class ObsInfo {

    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;
    private String rootDirectory;
    private String wgImage;
  }

  @Autowired
  private ObsInfo obsInfo;

  @Override
  public void upload(String pathAndName, File file) {
    ObsClient obsClient = new ObsClient(obsInfo.accessKeyId,
        obsInfo.accessKeySecret, obsInfo.endpoint);
    try {
      obsClient.putObject(obsInfo.bucketName, obsInfo.rootDirectory + pathAndName, file);
    } finally {
      try {
        obsClient.close();
      } catch (IOException e) {
      }
    }
  }

  @Override
  public String authorize(String pathAndName, long time) {
    ObsClient obsClient = new ObsClient(obsInfo.accessKeyId,
        obsInfo.accessKeySecret, obsInfo.endpoint);
    try {
      String path = obsInfo.rootDirectory + pathAndName;
      boolean doesObjectExist = obsClient.doesObjectExist(obsInfo.bucketName, path);
      if (!doesObjectExist) {
        path = obsInfo.wgImage;
      }
      TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, time);
      request.setBucketName(obsInfo.bucketName);
      request.setObjectKey(path);
      TemporarySignatureResponse response = obsClient.createTemporarySignature(request);
      String url = response.getSignedUrl();
      return url;
    } finally {
      try {
        obsClient.close();
      } catch (IOException e) {
      }
    }
  }

  @Override
  public String authorizeAllName(String pathAndName, long time) {
    ObsClient obsClient = new ObsClient(obsInfo.accessKeyId,
        obsInfo.accessKeySecret, obsInfo.endpoint);
    try {
      String path = pathAndName;
      TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, time);
      request.setBucketName(obsInfo.bucketName);
      request.setObjectKey(path);
      TemporarySignatureResponse response = obsClient.createTemporarySignature(request);
      String url = response.getSignedUrl();
      return url;
    } finally {
      try {
        obsClient.close();
      } catch (IOException e) {
      }
    }
  }

  @Override
  public Map<String, Object> tokens(String dir) {
    Map<String, Object> result = Maps.newHashMap();
    ObsClient obsClient = new ObsClient(obsInfo.accessKeyId,
        obsInfo.accessKeySecret, obsInfo.endpoint);
    try {
      long expireTime = 60;
      PostSignatureRequest request = new PostSignatureRequest();
      Map<String, Object> formParams = new HashMap<String, Object>();
      formParams.put("x-obs-acl", "public-read");
      formParams.put("content-type", "text/plain");
      request.setFormParams(formParams);
      request.setExpires(expireTime);
      PostSignatureResponse response = obsClient.createPostSignature(request);
      result.put("storeType", "obs");
      result.put("storageType", "obs");
      result.put("AccessKeyId", obsInfo.accessKeyId);
      result.put("policy", response.getPolicy());
      result.put("signature", response.getSignature());
      result.put("x-obs-acl", "public-read");
      result.put("content-type", "text/plain");
      result.put("dir", dir);
      result.put("host", obsInfo.endpoint.split("://")[0] + "://" + obsInfo.bucketName + "."
          + obsInfo.endpoint.split("://")[1]);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        obsClient.close();
      } catch (IOException e) {
      }
    }
    return result;
  }

  @Override
  public void deleteFile(String pathAndName) {
    ObsClient obsClient = new ObsClient(obsInfo.accessKeyId,
        obsInfo.accessKeySecret, obsInfo.endpoint);
    try {
      obsClient.deleteObject(obsInfo.bucketName, obsInfo.rootDirectory + pathAndName);
    } finally {
      try {
        obsClient.close();
      } catch (IOException e) {
      }
    }
  }

}

四、application配置文件

objectstorage.type: obs
obs:
  endpoint: https://oss-cn-xhh.aliyuncs.com
  access-key-id: LTAI4GKXXXYuEzsXXXXyY63x
  access-key-secret: BXXXPwUVZr8XXXXjFMPjkw
  bucket-name: data
  root-directory: xhh/export/

猜你喜欢

转载自blog.csdn.net/u010953816/article/details/123357345