Minio8 获取文件信息(文件大小)

Minio8 获取文件信息

  • pom文件引入jar包
  • 方法
    • 获取MInio链接
    • 封装获取文件信息
    • statObject(StatObjectArgs args)方法
    • StatObjectArgs类
    • StatObjectResponse 类
  • 例子(获取文件大小)

pom文件引入jar包

       
            io.minio
            minio
            8.1.0
        

方法

获取MInio链接

配置类
/**
 * @author wqy
 */
@ConfigurationProperties(prefix = "min-io")
@Component
@Data
public class MinIoProperties {
    private String url;
    private String accessKey;
    private String secretKey;
}

    /**
     * 获取MinIO链接
     *
     * @return MinIoClient
     */
    public static MinioClient getMiniClient() {
       
        MinIoProperties minIoProperties = ApplicationContextHelper.popBean(MinIoProperties.class);
        return MinioClient.builder()
                .endpoint(Objects.requireNonNull(minIoProperties, "minIoProperties不能为空").getUrl())
                .credentials(minIoProperties.getAccessKey(), minIoProperties.getSecretKey())
                .build();
    }

封装获取文件信息

  /**
     * 获取文件信息
     *
     * @param minIoClient MinIO链接
     * @param bucketName  bucket名称
     * @param objectName  文件名称
     * @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#statObject
     */
    public static StatObjectResponse getObjectInfo(MinioClient minIoClient, String bucketName, String objectName) throws Exception {
        return minIoClient.statObject(StatObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .build());
    }

statObject(StatObjectArgs args)方法

  public StatObjectResponse statObject(StatObjectArgs args)
      throws ErrorResponseException, InsufficientDataException, InternalException,
          InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException,
          ServerException, XmlParserException {
    checkArgs(args);
    args.validateSsec(baseUrl);
    Response response =
        executeHead(
            args,
            args.getHeaders(),
            (args.versionId() != null) ? newMultimap("versionId", args.versionId()) : null);
    return new StatObjectResponse(response.headers(), args.bucket(), args.region(), args.object());
  }

StatObjectArgs类

public class StatObjectArgs extends ObjectConditionalReadArgs {
  protected StatObjectArgs() {}

  public StatObjectArgs(ObjectReadArgs args) {
    this.extraHeaders = args.extraHeaders;
    this.extraQueryParams = args.extraQueryParams;
    this.bucketName = args.bucketName;
    this.region = args.region;
    this.objectName = args.objectName;
    this.versionId = args.versionId;
    this.ssec = args.ssec;
  }

  public static Builder builder() {
    return new Builder();
  }

  /** Argument builder of {@link StatObjectArgs}. */
  public static final class Builder
      extends ObjectConditionalReadArgs.Builder {}
}
public class StatObjectArgs extends ObjectConditionalReadArgs {
  protected StatObjectArgs() {}

  public StatObjectArgs(ObjectReadArgs args) {
    this.extraHeaders = args.extraHeaders;
    this.extraQueryParams = args.extraQueryParams;
    this.bucketName = args.bucketName;
    this.region = args.region;
    this.objectName = args.objectName;
    this.versionId = args.versionId;
    this.ssec = args.ssec;
  }

  public static Builder builder() {
    return new Builder();
  }

  /** Argument builder of {@link StatObjectArgs}. */
  public static final class Builder
      extends ObjectConditionalReadArgs.Builder {}
}

StatObjectResponse 类

public class StatObjectResponse extends GenericResponse {
  private String etag; 
  private long size; // 文件大小
  private ZonedDateTime lastModified;
  private RetentionMode retentionMode;
  private ZonedDateTime retentionRetainUntilDate;
  private LegalHold legalHold;
  private boolean deleteMarker;
  private Map userMetadata;

  public StatObjectResponse(Headers headers, String bucket, String region, String object) {
    super(headers, bucket, region, object);
    String value;

    value = headers.get("ETag");
    this.etag = (value != null ? value.replaceAll("\"", "") : "");

    value = headers.get("Content-Length");
    this.size = (value != null ? Long.parseLong(value) : -1);

    this.lastModified =
        ZonedDateTime.parse(headers.get("Last-Modified"), Time.HTTP_HEADER_DATE_FORMAT);

    value = headers.get("x-amz-object-lock-mode");
    this.retentionMode = (value != null ? RetentionMode.valueOf(value) : null);

    value = headers.get("x-amz-object-lock-retain-until-date");
    this.retentionRetainUntilDate =
        (value != null ? ResponseDate.fromString(value).zonedDateTime() : null);

    this.legalHold = new LegalHold("ON".equals(headers.get("x-amz-object-lock-legal-hold")));

    this.deleteMarker = Boolean.parseBoolean(headers.get("x-amz-delete-marker"));

    Map userMetadata = new HashMap<>();
    for (String key : headers.names()) {
      if (key.startsWith("x-amz-meta-")) {
        userMetadata.put(key.substring("x-amz-meta-".length(), key.length()), headers.get(key));
      }
    }

    this.userMetadata = Collections.unmodifiableMap(userMetadata);
  }

  public String etag() {
    return etag;
  }

  public long size() {
    return size;
  }

  public ZonedDateTime lastModified() {
    return lastModified;
  }

  public RetentionMode retentionMode() {
    return retentionMode;
  }

  public ZonedDateTime retentionRetainUntilDate() {
    return retentionRetainUntilDate;
  }

  public LegalHold legalHold() {
    return legalHold;
  }

  public boolean deleteMarker() {
    return deleteMarker;
  }

  public String versionId() {
    return this.headers().get("x-amz-version-id");
  }

  public String contentType() {
    return this.headers().get("Content-Type");
  }

  public Map userMetadata() {
    return userMetadata;
  }

  @Override
  public String toString() {
    return "ObjectStat{"
        + "bucket="
        + bucket()
        + ", object="
        + object()
        + ", last-modified="
        + lastModified
        + ", size="
        + size
        + "}";
  }

例子(获取文件大小)

       try {
          StatObjectResponse objectInfo = MinIoUtil.getObjectInfo(miniClient, bucketName, objectName);
          fileSize = objectInfo.size();
        } catch (Exception e) {
               e.printStackTrace();
               fileSize = 0;
         }

你可能感兴趣的:(java,开发语言)