use of com.amazonaws.s3.model.PutObjectRequest in project formkiq-core by formkiq.
the class S3Service method presignPutUrl.
/**
* Generate a S3 Signed Url for creating an object using PUT request.
*
* @param bucket {@link String}
* @param key {@link String}
* @param duration {@link Duration}
* @return {@link URL}
*/
public URL presignPutUrl(final String bucket, final String key, final Duration duration) {
try (S3Presigner signer = this.builder.buildPresigner()) {
PutObjectRequest putObjectRequest = PutObjectRequest.builder().bucket(bucket).key(key).build();
PutObjectPresignRequest putRequest = PutObjectPresignRequest.builder().signatureDuration(duration).putObjectRequest(putObjectRequest).build();
PresignedPutObjectRequest req = signer.presignPutObject(putRequest);
URL url = req.url();
return url;
}
}
use of com.amazonaws.s3.model.PutObjectRequest in project amazon-neptune-tools by awslabs.
the class ExportToS3NeptuneExportEventHandler method uploadCompletionFileToS3.
private void uploadCompletionFileToS3(TransferManager transferManager, File directory, S3ObjectInfo outputS3ObjectInfo, ExportStats stats, GraphSchema graphSchema) throws IOException {
if (StringUtils.isEmpty(completionFileS3Path)) {
return;
}
if (directory == null || !directory.exists()) {
logger.warn("Ignoring request to upload completion file to S3 because directory from which to upload files does not exist");
return;
}
String completionFilename = s3UploadParams.createExportSubdirectory() ? directory.getName() : String.valueOf(System.currentTimeMillis());
File completionFile = new File(localOutputPath, completionFilename + ".json");
ObjectNode neptuneExportNode = JsonNodeFactory.instance.objectNode();
completionFilePayload.set("neptuneExport", neptuneExportNode);
neptuneExportNode.put("outputS3Path", outputS3ObjectInfo.toString());
stats.addTo(neptuneExportNode, graphSchema);
for (CompletionFileWriter completionFileWriter : completionFileWriters) {
completionFileWriter.updateCompletionFile(completionFilePayload);
}
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(completionFile), UTF_8))) {
ObjectWriter objectWriter = new ObjectMapper().writer().withDefaultPrettyPrinter();
writer.write(objectWriter.writeValueAsString(completionFilePayload));
}
S3ObjectInfo completionFileS3ObjectInfo = new S3ObjectInfo(completionFileS3Path).replaceOrAppendKey("_COMPLETION_ID_", FilenameUtils.getBaseName(completionFile.getName()), completionFile.getName());
logger.info("Uploading completion file to {}", completionFileS3ObjectInfo.key());
try (InputStream inputStream = new FileInputStream(completionFile)) {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(completionFile.length());
objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
PutObjectRequest putObjectRequest = new PutObjectRequest(completionFileS3ObjectInfo.bucket(), completionFileS3ObjectInfo.key(), inputStream, objectMetadata).withTagging(createObjectTags(profiles));
Upload upload = transferManager.upload(putObjectRequest);
upload.waitForUploadResult();
} catch (InterruptedException e) {
logger.warn(e.getMessage());
Thread.currentThread().interrupt();
}
}
use of com.amazonaws.s3.model.PutObjectRequest in project amazon-neptune-tools by awslabs.
the class NeptuneMachineLearningExportEventHandlerV1 method uploadTrainingJobConfigurationFileToS3.
private void uploadTrainingJobConfigurationFileToS3(String filename, TransferManager transferManager, File trainingJobConfigurationFile, S3ObjectInfo outputS3ObjectInfo) throws IOException {
S3ObjectInfo s3ObjectInfo = outputS3ObjectInfo.withNewKeySuffix(filename);
try (InputStream inputStream = new FileInputStream(trainingJobConfigurationFile)) {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(trainingJobConfigurationFile.length());
objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
PutObjectRequest putObjectRequest = new PutObjectRequest(s3ObjectInfo.bucket(), s3ObjectInfo.key(), inputStream, objectMetadata).withTagging(ExportToS3NeptuneExportEventHandler.createObjectTags(profiles));
Upload upload = transferManager.upload(putObjectRequest);
upload.waitForUploadResult();
} catch (InterruptedException e) {
logger.warn(e.getMessage());
Thread.currentThread().interrupt();
}
}
use of com.amazonaws.s3.model.PutObjectRequest in project knime-cloud by knime.
the class S3RemoteFile method write.
/**
* Write the given remote file into this files location.
*
* This method will overwrite the old file if it exists.
*
* @param file Source remote file
* @param exec Execution context for <code>checkCanceled()</code> and
* <code>setProgress()</code>
* @throws Exception If the operation could not be executed
*/
@SuppressWarnings("rawtypes")
@Override
public void write(final RemoteFile file, final ExecutionContext exec) throws Exception {
// Default implementation using just remote file methods
try (final InputStream in = file.openInputStream()) {
final String uri = getURI().toString();
final ObjectMetadata metadata = new ObjectMetadata();
// Add SSEncryption --> See AP-8823
if (getConnection().useSSEncryption()) {
metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
}
long fileSize = file.getSize();
metadata.setContentLength(fileSize);
final PutObjectRequest putRequest = new PutObjectRequest(getContainerName(), getBlobName(), in, metadata);
Upload upload = getTransferManager().upload(putRequest);
S3ProgressListener progressListener = new S3ProgressListener() {
long totalTransferred = 0;
@Override
public void progressChanged(final ProgressEvent progressEvent) {
totalTransferred += progressEvent.getBytesTransferred();
double percent = totalTransferred / (fileSize / 100);
exec.setProgress(percent / 100, () -> "Written: " + FileUtils.byteCountToDisplaySize(totalTransferred) + " to file " + uri);
}
@Override
public void onPersistableTransfer(final PersistableTransfer persistableTransfer) {
// Not used since we are not going to pause/unpause upload
}
};
upload.addProgressListener(progressListener);
upload.waitForCompletion();
} catch (InterruptedException e) {
// removes uploaded parts of failed uploads on given bucket uploaded before now
final Date now = new Date(System.currentTimeMillis());
getTransferManager().abortMultipartUploads(getContainerName(), now);
// check if canceled, otherwise throw exception
exec.checkCanceled();
throw e;
} catch (AmazonS3Exception amazonException) {
throw new KnimeS3Exception(amazonException);
}
}
use of com.amazonaws.s3.model.PutObjectRequest in project xin-open-enterprise-platform by hui0xin.
the class S3FileUploadService method uploadFile.
@Override
public FileUploadVo uploadFile(final String fileName, final File file, final FileUploadMetadata uploadMetadata) {
final String key = this.getKey(fileName);
final ObjectMetadata objectMetadata = this.createObjectMetadata(uploadMetadata);
this.s3Client.putObject(new PutObjectRequest(this.properties.getBucketName(), key, file).withMetadata(objectMetadata));
return FileUploadVo.builder().fileName(fileName).key(key).url(this.getSignedUrl(fileName)).build();
}
Aggregations