23 Şubat 2021 Salı

MinIO API

Giriş
docker-compose.yml dosyasında şöyle yaparız
minio1:
  image: minio/minio:RELEASE.2020-08-27T05-16-20Z
  volumes:
    - data1-1:/data1
    - data1-2:/data2
  ports:
    - "9001:9000"
  environment:
    MINIO_ACCESS_KEY: minio
    MINIO_SECRET_KEY: minio123
  command: server http://minio{1...4}/data{1...2}
  healthcheck:
    test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
    interval: 30s
    timeout: 20s
    retries: 3

  minio2:
    ...
  minio3:
    ...
  minio4:
    ...
## By default this config uses default local driver,
## For custom volumes replace with volume driver configuration.
volumes:
  data1-1:
  data1-2:
  data2-1:
  data2-2:
  data3-1:
  data3-2:
  data4-1:
  data4-2:
Açıklaması şöyle
In this configuration, we are running MinIO in distributed mode. Basically, it can withstand multiple node failures and yet ensure full data protection because the drives are distributed across several nodes.

To run it in this mode, we need four disks according to the requirements. You can see that we named them minio1, minio2, minio3, minio4.

To start a distributed MinIO instance, we pass the drive locations as parameters to the minio server command. All nodes should have the same access key and secret key for the nodes to connect. Note that we have created login credentials MINIO_ACCESS_KEY: minio and MINIO_SECRET_KEY: minio123 . Feel free to change them as you wish.
MinioClient Sınıfı
constructor
Şöyle yaparız
private MinioClient getMinioClient() {
  return MinioClient.builder()
    .endpoint("localhost", 9001, false)
    .credentials("minio", "minio123")
    .build();
}
getObject metodu
Şöyle yaparız
private InputStream downloadOriginalBookAsStream(){
  InputStream stream;
  try {
    stream = getMinioClient().getObject(
      GetObjectArgs.builder()
        .bucket("original-ebook")
        .object("alice.epub")
        .build());
  } catch (InvalidKeyException | NoSuchAlgorithmException | ErrorResponseException | 
    InvalidResponseException | InvalidBucketNameException | ServerException | 
    XmlParserException | InsufficientDataException |
    InternalException | IOException e) {
    
    System.err.println(e.getMessage());
    throw new IllegalArgumentException("The original ebook file was not found");
  }
  return stream;
}
getPresignedObjectUrl metodu
Şöyle yaparız
private String createURL(MinioClient minioClient, String filename) throws
  IOException, InvalidKeyException, InvalidResponseException, 
  InsufficientDataException, InvalidExpiresRangeException, ServerException, 
  InternalException, NoSuchAlgorithmException, XmlParserException, 
  InvalidBucketNameException, ErrorResponseException {
  
  return minioClient.getPresignedObjectUrl(
    GetPresignedObjectUrlArgs.builder()
      .method(Method.GET)
      .bucket("ebookcreator")
      .object(filename)
      .expiry(2, TimeUnit.HOURS)
      .build());
}
Açıklaması şöyle
This URL will be valid for two hours. You can modify the expiration date by using the expiry parameter.
uploadObject metodu
Şöyle yaparız
private void handleFileUpload(String filename) {

  MinioClient minioClient = getMinioClient();
  try {
            
    ObjectWriteResponse response = createBucketAndUploadFile(minioClient, filename);
    if (response != null) {
      String url = createURL(minioClient, filename);
      System.out.println("Created url: " + url);
    }
  } catch (InvalidKeyException | NoSuchAlgorithmException | ErrorResponseException | 
      InvalidResponseException | InvalidBucketNameException |
      ServerException | RegionConflictException | InvalidExpiresRangeException | 
      XmlParserException | InsufficientDataException |
      InternalException | IOException e) {
      
    System.err.println(e.getMessage());
  }
}
private ObjectWriteResponse createBucketAndUploadFile(MinioClient minioClient,
String filename) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException, RegionConflictException { if (!minioClient.bucketExists(BucketExistsArgs.builder()
.bucket("ebookcreator").build())) { minioClient.makeBucket(MakeBucketArgs.builder().bucket("ebookcreator").build()); } return minioClient.uploadObject(UploadObjectArgs.builder() .bucket("ebookcreator") .object(filename) .filename(filename) .contentType("application/epub") .build()); }




Hiç yorum yok:

Yorum Gönder