Search in sources :

Example 11 with PutObjectRequest

use of com.amazonaws.s3.model.PutObjectRequest in project quality-measure-and-cohort-service by Alvearie.

the class ValueSetImporter method putToS3.

private static void putToS3(ValueSetImporterArguments arguments, FhirContext fhirContext, ValueSet vs, String vsFileName, AmazonS3 S3Client) {
    ObjectMetadata metadata = new ObjectMetadata();
    byte[] arr = fhirContext.newJsonParser().encodeResourceToString(vs).getBytes();
    metadata.setContentLength(arr.length);
    PutObjectRequest put = new PutObjectRequest(arguments.bucket, vsFileName, new ByteArrayInputStream(arr), metadata);
    S3Client.putObject(put);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 12 with PutObjectRequest

use of com.amazonaws.s3.model.PutObjectRequest in project entrada by SIDN.

the class S3FileManagerImpl method uploadFile.

private boolean uploadFile(File src, S3Details dstDetails, boolean archive) {
    PutObjectRequest request = new PutObjectRequest(dstDetails.getBucket(), FileUtil.appendPath(dstDetails.getKey(), src.getName()), src);
    ObjectMetadata meta = new ObjectMetadata();
    if (archive) {
        meta.setHeader(Headers.STORAGE_CLASS, StorageClass.fromValue(StringUtils.upperCase(archiveStorageClass)));
    }
    if (encrypt) {
        meta.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
    }
    request.setMetadata(meta);
    try {
        amazonS3.putObject(request);
        return true;
    } catch (Exception e) {
        log.error("Error while uploading file: {}", src, e);
    }
    return false;
}
Also used : ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) URISyntaxException(java.net.URISyntaxException) AmazonServiceException(com.amazonaws.AmazonServiceException)

Example 13 with PutObjectRequest

use of com.amazonaws.s3.model.PutObjectRequest in project datarouter by hotpads.

the class BaseDatarouterS3Client method putObjectAsBytesWithExpirationTime.

@Override
public void putObjectAsBytesWithExpirationTime(String bucket, String key, ContentType contentType, String cacheControl, ObjectCannedACL acl, byte[] bytes, Instant expirationTime) {
    S3Client s3Client = getS3ClientForBucket(bucket);
    PutObjectRequest request = makePutObjectRequestBuilder(bucket, key, contentType).cacheControl(cacheControl).acl(acl).expires(expirationTime).build();
    RequestBody requestBody = RequestBody.fromBytes(bytes);
    try (var $ = TracerTool.startSpan("S3 putObject", TraceSpanGroupType.CLOUD_STORAGE)) {
        s3Client.putObject(request, requestBody);
        TracerTool.appendToSpanInfo("Content-Length", request.contentLength());
    }
}
Also used : S3Client(software.amazon.awssdk.services.s3.S3Client) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) RequestBody(software.amazon.awssdk.core.sync.RequestBody)

Example 14 with PutObjectRequest

use of com.amazonaws.s3.model.PutObjectRequest in project mapfish-print by mapfish.

the class S3ReportStorage method createPutRequest.

private PutObjectRequest createPutRequest(final String ref, final String filename, final String extension, final String mimeType, final File file) {
    final PutObjectRequest request = new PutObjectRequest(bucket, getKey(ref, filename, extension), file);
    request.withCannedAcl(CannedAccessControlList.PublicRead);
    final ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentType(mimeType);
    request.withMetadata(metadata);
    return request;
}
Also used : ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 15 with PutObjectRequest

use of com.amazonaws.s3.model.PutObjectRequest in project Synapse-Repository-Services by Sage-Bionetworks.

the class DataUploaderMultipartImpl method uploadDataMultiPart.

@Override
public void uploadDataMultiPart(S3Token s3Token, File dataFile) throws SynapseException {
    // Formulate the request, note that S3 does not verify that the entire
    // upload matches this md5, unlike the single part upload
    String base64Md5;
    try {
        byte[] encoded = Base64.encodeBase64(Hex.decodeHex(s3Token.getMd5().toCharArray()));
        base64Md5 = new String(encoded, "ASCII");
    } catch (DecoderException ex) {
        throw new SynapseException(ex);
    } catch (UnsupportedEncodingException ex) {
        throw new SynapseException(ex);
    }
    ObjectMetadata s3Metadata = new ObjectMetadata();
    s3Metadata.setContentType(s3Token.getContentType());
    s3Metadata.setContentMD5(base64Md5);
    // S3 keys do not start with a slash but sometimes we are storing them
    // that way in Synapse
    String s3Key = (s3Token.getPath().startsWith("/")) ? s3Token.getPath().substring(1) : s3Token.getPath();
    PutObjectRequest request = new PutObjectRequest(s3Token.getBucket(), s3Key, dataFile).withMetadata(s3Metadata);
    if (null != progressListener) {
        request.setProgressListener(progressListener);
    }
    request.setCannedAcl(CannedAccessControlList.BucketOwnerFullControl);
    // Initiate the multipart uploas
    AWSCredentials credentials = new BasicSessionCredentials(s3Token.getAccessKeyId(), s3Token.getSecretAccessKey(), s3Token.getSessionToken());
    TransferManager tx = new TransferManager(credentials);
    Upload upload = tx.upload(request);
    if (null != progressListener) {
        progressListener.setUpload(upload);
    }
    // synchronous, can change it later if we want asynchronous behavior)
    try {
        upload.waitForUploadResult();
    } catch (Exception e) {
        throw new SynapseException("AWS S3 multipart upload of " + dataFile + " failed", e);
    }
    tx.shutdownNow();
}
Also used : DecoderException(org.apache.commons.codec.DecoderException) TransferManager(com.amazonaws.services.s3.transfer.TransferManager) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) SynapseException(org.sagebionetworks.client.exceptions.SynapseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Upload(com.amazonaws.services.s3.transfer.Upload) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) AWSCredentials(com.amazonaws.auth.AWSCredentials) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) DecoderException(org.apache.commons.codec.DecoderException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SynapseException(org.sagebionetworks.client.exceptions.SynapseException)

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