package nxm.minio.intr;

import io.minio.DownloadObjectArgs;
import io.minio.MinioClient;
import nxm.minio.lib.MinIOResource;
import nxm.sys.inc.ProvisionalUseOnly;
import nxm.sys.lib.FileName;
import nxm.sys.lib.Intrinsic;

/**
  Gets a file from an S3 Bucket

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

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

  public int open () {
    String url  = MA.getCS("URL");
    FileName fn = new FileName(MA.getCS("FILE"));

    MinIOResource minRes = new MinIOResource(url);
    MinioClient minioClient = minRes.getMinioClient();
    try {
      minioClient.downloadObject(DownloadObjectArgs.builder().bucket(minRes.getBucket()).object(minRes.getKey()).filename(fn.toLocalFileName()).build());
    }
    catch (Exception e) {
      M.warnStackTrace("FAILED S3GET URL="+url+" FILE="+fn, e);
    }

    return FINISH;
  }

}
