Search in sources :

Example 16 with PutObjectRequest

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;
    }
}
Also used : PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) PutObjectPresignRequest(software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) URL(java.net.URL)

Example 17 with PutObjectRequest

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();
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) S3ObjectInfo(com.amazonaws.services.neptune.util.S3ObjectInfo) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 18 with PutObjectRequest

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();
    }
}
Also used : S3ObjectInfo(com.amazonaws.services.neptune.util.S3ObjectInfo) Upload(com.amazonaws.services.s3.transfer.Upload) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 19 with PutObjectRequest

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);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) InputStream(java.io.InputStream) Upload(com.amazonaws.services.s3.transfer.Upload) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) ProgressEvent(com.amazonaws.event.ProgressEvent) Date(java.util.Date) S3ProgressListener(com.amazonaws.services.s3.transfer.internal.S3ProgressListener) PersistableTransfer(com.amazonaws.services.s3.transfer.PersistableTransfer) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 20 with PutObjectRequest

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();
}
Also used : ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Aggregations

PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)301 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)193 ByteArrayInputStream (java.io.ByteArrayInputStream)113 Test (org.junit.Test)113 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)78 File (java.io.File)68 IOException (java.io.IOException)65 InputStream (java.io.InputStream)55 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)42 AmazonClientException (com.amazonaws.AmazonClientException)40 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)39 Upload (com.amazonaws.services.s3.transfer.Upload)37 AmazonServiceException (com.amazonaws.AmazonServiceException)35 Test (org.junit.jupiter.api.Test)30 AmazonS3 (com.amazonaws.services.s3.AmazonS3)28 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)20 Date (java.util.Date)20 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)20 StorageUnitEntity (org.finra.herd.model.jpa.StorageUnitEntity)20 RequestBody (software.amazon.awssdk.core.sync.RequestBody)19