
prerna.aws.s3.S3UploaderReactor Maven / Gradle / Ivy
The newest version!
package prerna.aws.s3;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.Logger;
import com.amazonaws.AmazonClientException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
import com.amazonaws.services.s3.transfer.Upload;
import prerna.algorithm.api.SemossDataType;
import prerna.engine.api.IHeadersDataRow;
import prerna.reactor.task.TaskBuilderReactor;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.PixelOperationType;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import prerna.util.Constants;
import prerna.util.Utility;
/**
* Deprecated on March 21st 2025. Please use StorageEngine Directly
*/
@Deprecated
public class S3UploaderReactor extends TaskBuilderReactor {
private static final String CLASS_NAME = S3UploaderReactor.class.getName();
private static final String FILE_NAME = "fileName";
private static final String BUCKET = "bucket";
private String fileLocation = null;
private Logger logger;
public S3UploaderReactor() {
this.keysToGet = S3Utils.addCommonS3Keys(new String[] { FILE_NAME, BUCKET });
}
@Override
public String getDescriptionForKey(String key) {
if (key.equals(FILE_NAME)) {
return "Base file name to use for S3 object";
} else if(key.equals(BUCKET)) {
return "S3 bucket name";
} else {
String commonDescription = S3Utils.getDescriptionForCommonS3Key(key);
if(commonDescription != null) {
return commonDescription;
}
}
return super.getDescriptionForKey(key);
}
@Override
public String getReactorDescription() {
return "Upload task data as a CSV to an S3 bucket. Credentials can be set via a profile path/name or with an explicit access key and secret";
}
@Override
public NounMetadata execute() {
organizeKeys();
String fileName = keyValue.get(keysToGet[0]);
String bucketName = this.keyValue.get(this.keysToGet[1]);
if (fileName == null || fileName.length() <= 0) {
throw new IllegalArgumentException("Need to specify file name");
}
if (bucketName == null || bucketName.length() <= 0) {
throw new IllegalArgumentException("Need to specify bucket");
}
logger = getLogger(CLASS_NAME);
this.task = getTask();
this.fileLocation = Utility.getBaseFolder() + DIR_SEPARATOR + fileName + ".csv";
//make file
buildTask();
AmazonS3 s3Client = S3Utils.getInstance().getS3Client(this.keyValue);
File fileToPush = new File(Utility.normalizePath(this.fileLocation));
TransferManager xferMgr = TransferManagerBuilder.standard().withS3Client(s3Client).build();
boolean transferFailure = false;
try {
Upload xfer = xferMgr.upload(bucketName, fileToPush.getName(), fileToPush);
xfer.waitForCompletion();
} catch (AmazonClientException | InterruptedException e) {
logger.error("Amazon upload failure: " + e.getMessage());
transferFailure = true;
}
xferMgr.shutdownNow();
if(transferFailure) {
return getError("Error occurred during upload");
}
return new NounMetadata(true, PixelDataType.BOOLEAN, PixelOperationType.SUCCESS);
}
@Override
protected void buildTask() {
File f = new File(this.fileLocation);
try {
long start = System.currentTimeMillis();
try {
f.createNewFile();
} catch (IOException e) {
logger.error(Constants.STACKTRACE, e);
}
FileWriter writer = null;
BufferedWriter bufferedWriter = null;
try {
writer = new FileWriter(f);
bufferedWriter = new BufferedWriter(writer);
// store some variables and just reset
// should be faster than creating new ones each time
int i = 0;
int size = 0;
StringBuilder builder = null;
// create typesArr as an array for faster searching
String[] headers = null;
SemossDataType[] typesArr = null;
// we need to iterate and write the headers during the first time
if(this.task.hasNext()) {
IHeadersDataRow row = this.task.next();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy