package nxm.minio.intr;

import io.minio.MinioClient;
import io.minio.RemoveBucketArgs;
import io.minio.RemoveObjectArgs;
import nxm.minio.lib.MinIOResource;
import nxm.sys.inc.ProvisionalUseOnly;
import nxm.sys.lib.Intrinsic;

/**
  Removes a file from an S3 Bucket

  @author Jeanette McCall
*/
@ProvisionalUseOnly("prototype")
public class S3rm extends Intrinsic {

  /** default constructor */
  public S3rm () {}

  public int open () {
    String url  = MA.getCS("URL");

    MinIOResource minRes = new MinIOResource(url);
    MinioClient minioClient = minRes.getMinioClient();
    try {
      if ("".equals(minRes.getKey())) {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(minRes.getBucket()).build());
      }
      else {
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(minRes.getBucket()).object(minRes.getKey()).build());
      }
    }
    catch (Exception e) {
      M.warnStackTrace("FAILED S3RM URL="+url, e);
    }

    return FINISH;
  }

}
