Search in sources :

Example 1 with UploadResult

use of com.amazonaws.services.s3.transfer.model.UploadResult in project aws-doc-sdk-examples by awsdocs.

the class LowLevelMultipartUpload method main.

public static void main(String[] args) throws IOException {
    Regions clientRegion = Regions.DEFAULT_REGION;
    String bucketName = "*** Bucket name ***";
    String keyName = "*** Key name ***";
    String filePath = "*** Path to file to upload ***";
    File file = new File(filePath);
    long contentLength = file.length();
    // Set part size to 5 MB.
    long partSize = 5 * 1024 * 1024;
    try {
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(clientRegion).withCredentials(new ProfileCredentialsProvider()).build();
        // Create a list of ETag objects. You retrieve ETags for each object part uploaded,
        // then, after each individual part has been uploaded, pass the list of ETags to
        // the request to complete the upload.
        List<PartETag> partETags = new ArrayList<PartETag>();
        // Initiate the multipart upload.
        InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, keyName);
        InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest);
        // Upload the file parts.
        long filePosition = 0;
        for (int i = 1; filePosition < contentLength; i++) {
            // Because the last part could be less than 5 MB, adjust the part size as needed.
            partSize = Math.min(partSize, (contentLength - filePosition));
            // Create the request to upload a part.
            UploadPartRequest uploadRequest = new UploadPartRequest().withBucketName(bucketName).withKey(keyName).withUploadId(initResponse.getUploadId()).withPartNumber(i).withFileOffset(filePosition).withFile(file).withPartSize(partSize);
            // Upload the part and add the response's ETag to our list.
            UploadPartResult uploadResult = s3Client.uploadPart(uploadRequest);
            partETags.add(uploadResult.getPartETag());
            filePosition += partSize;
        }
        // Complete the multipart upload.
        CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(bucketName, keyName, initResponse.getUploadId(), partETags);
        s3Client.completeMultipartUpload(compRequest);
    } catch (AmazonServiceException e) {
        // The call was transmitted successfully, but Amazon S3 couldn't process
        // it, so it returned an error response.
        e.printStackTrace();
    } catch (SdkClientException e) {
        // Amazon S3 couldn't be contacted for a response, or the client
        // couldn't parse the response from Amazon S3.
        e.printStackTrace();
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) ArrayList(java.util.ArrayList) Regions(com.amazonaws.regions.Regions) SdkClientException(com.amazonaws.SdkClientException) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) File(java.io.File)

Example 2 with UploadResult

use of com.amazonaws.services.s3.transfer.model.UploadResult in project alluxio by Alluxio.

the class S3ALowLevelOutputStreamTest method mockS3ClientAndExecutor.

/**
 * Mocks the S3 client and executor.
 */
private void mockS3ClientAndExecutor() throws Exception {
    mMockS3Client = PowerMockito.mock(AmazonS3.class);
    InitiateMultipartUploadResult initResult = new InitiateMultipartUploadResult();
    when(mMockS3Client.initiateMultipartUpload(any(InitiateMultipartUploadRequest.class))).thenReturn(initResult);
    initResult.setUploadId(UPLOAD_ID);
    when(mMockS3Client.uploadPart(any(UploadPartRequest.class))).thenAnswer((InvocationOnMock invocation) -> {
        Object[] args = invocation.getArguments();
        UploadPartResult uploadResult = new UploadPartResult();
        uploadResult.setPartNumber(((UploadPartRequest) args[0]).getPartNumber());
        return uploadResult;
    });
    when(mMockS3Client.completeMultipartUpload(any(CompleteMultipartUploadRequest.class))).thenReturn(new CompleteMultipartUploadResult());
    mMockTag = (ListenableFuture<PartETag>) PowerMockito.mock(ListenableFuture.class);
    when(mMockTag.get()).thenReturn(new PartETag(1, "someTag"));
    mMockExecutor = Mockito.mock(ListeningExecutorService.class);
    when(mMockExecutor.submit(any(Callable.class))).thenReturn(mMockTag);
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) InitiateMultipartUploadResult(com.amazonaws.services.s3.model.InitiateMultipartUploadResult) InitiateMultipartUploadRequest(com.amazonaws.services.s3.model.InitiateMultipartUploadRequest) UploadPartRequest(com.amazonaws.services.s3.model.UploadPartRequest) CompleteMultipartUploadResult(com.amazonaws.services.s3.model.CompleteMultipartUploadResult) PartETag(com.amazonaws.services.s3.model.PartETag) Callable(java.util.concurrent.Callable) UploadPartResult(com.amazonaws.services.s3.model.UploadPartResult) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) CompleteMultipartUploadRequest(com.amazonaws.services.s3.model.CompleteMultipartUploadRequest)

Example 3 with UploadResult

use of com.amazonaws.services.s3.transfer.model.UploadResult in project bender by Nextdoor.

the class S3TransporterTest method getMockClient.

private AmazonS3Client getMockClient() {
    AmazonS3Client mockClient = spy(AmazonS3Client.class);
    UploadPartResult uploadResult = new UploadPartResult();
    uploadResult.setETag("foo");
    doReturn(uploadResult).when(mockClient).uploadPart(any(UploadPartRequest.class));
    InitiateMultipartUploadResult initUploadResult = new InitiateMultipartUploadResult();
    initUploadResult.setUploadId("123");
    doReturn(initUploadResult).when(mockClient).initiateMultipartUpload(any(InitiateMultipartUploadRequest.class));
    return mockClient;
}
Also used : UploadPartResult(com.amazonaws.services.s3.model.UploadPartResult) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) InitiateMultipartUploadResult(com.amazonaws.services.s3.model.InitiateMultipartUploadResult) InitiateMultipartUploadRequest(com.amazonaws.services.s3.model.InitiateMultipartUploadRequest) UploadPartRequest(com.amazonaws.services.s3.model.UploadPartRequest)

Example 4 with UploadResult

use of com.amazonaws.services.s3.transfer.model.UploadResult in project camel by apache.

the class S3Producer method processMultiPart.

public void processMultiPart(final Exchange exchange) throws Exception {
    File filePayload = null;
    Object obj = exchange.getIn().getMandatoryBody();
    // Need to check if the message body is WrappedFile
    if (obj instanceof WrappedFile) {
        obj = ((WrappedFile<?>) obj).getFile();
    }
    if (obj instanceof File) {
        filePayload = (File) obj;
    } else {
        throw new InvalidArgumentException("aws-s3: MultiPart upload requires a File input.");
    }
    ObjectMetadata objectMetadata = determineMetadata(exchange);
    if (objectMetadata.getContentLength() == 0) {
        objectMetadata.setContentLength(filePayload.length());
    }
    final String keyName = determineKey(exchange);
    final InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(getConfiguration().getBucketName(), keyName, objectMetadata);
    String storageClass = determineStorageClass(exchange);
    if (storageClass != null) {
        initRequest.setStorageClass(StorageClass.fromValue(storageClass));
    }
    String cannedAcl = exchange.getIn().getHeader(S3Constants.CANNED_ACL, String.class);
    if (cannedAcl != null) {
        CannedAccessControlList objectAcl = CannedAccessControlList.valueOf(cannedAcl);
        initRequest.setCannedACL(objectAcl);
    }
    AccessControlList acl = exchange.getIn().getHeader(S3Constants.ACL, AccessControlList.class);
    if (acl != null) {
        // note: if cannedacl and acl are both specified the last one will be used. refer to
        // PutObjectRequest#setAccessControlList for more details
        initRequest.setAccessControlList(acl);
    }
    LOG.trace("Initiating multipart upload [{}] from exchange [{}]...", initRequest, exchange);
    final InitiateMultipartUploadResult initResponse = getEndpoint().getS3Client().initiateMultipartUpload(initRequest);
    final long contentLength = objectMetadata.getContentLength();
    final List<PartETag> partETags = new ArrayList<PartETag>();
    long partSize = getConfiguration().getPartSize();
    CompleteMultipartUploadResult uploadResult = null;
    long filePosition = 0;
    try {
        for (int part = 1; filePosition < contentLength; part++) {
            partSize = Math.min(partSize, contentLength - filePosition);
            UploadPartRequest uploadRequest = new UploadPartRequest().withBucketName(getConfiguration().getBucketName()).withKey(keyName).withUploadId(initResponse.getUploadId()).withPartNumber(part).withFileOffset(filePosition).withFile(filePayload).withPartSize(partSize);
            LOG.trace("Uploading part [{}] for {}", part, keyName);
            partETags.add(getEndpoint().getS3Client().uploadPart(uploadRequest).getPartETag());
            filePosition += partSize;
        }
        CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(getConfiguration().getBucketName(), keyName, initResponse.getUploadId(), partETags);
        uploadResult = getEndpoint().getS3Client().completeMultipartUpload(compRequest);
    } catch (Exception e) {
        getEndpoint().getS3Client().abortMultipartUpload(new AbortMultipartUploadRequest(getConfiguration().getBucketName(), keyName, initResponse.getUploadId()));
        throw e;
    }
    Message message = getMessageForResponse(exchange);
    message.setHeader(S3Constants.E_TAG, uploadResult.getETag());
    if (uploadResult.getVersionId() != null) {
        message.setHeader(S3Constants.VERSION_ID, uploadResult.getVersionId());
    }
    if (getConfiguration().isDeleteAfterWrite() && filePayload != null) {
        FileUtil.deleteFile(filePayload);
    }
}
Also used : CannedAccessControlList(com.amazonaws.services.s3.model.CannedAccessControlList) AccessControlList(com.amazonaws.services.s3.model.AccessControlList) InitiateMultipartUploadResult(com.amazonaws.services.s3.model.InitiateMultipartUploadResult) Message(org.apache.camel.Message) InitiateMultipartUploadRequest(com.amazonaws.services.s3.model.InitiateMultipartUploadRequest) ArrayList(java.util.ArrayList) UploadPartRequest(com.amazonaws.services.s3.model.UploadPartRequest) AbortMultipartUploadRequest(com.amazonaws.services.s3.model.AbortMultipartUploadRequest) CompleteMultipartUploadResult(com.amazonaws.services.s3.model.CompleteMultipartUploadResult) CannedAccessControlList(com.amazonaws.services.s3.model.CannedAccessControlList) PartETag(com.amazonaws.services.s3.model.PartETag) Endpoint(org.apache.camel.Endpoint) InvalidArgumentException(com.amazonaws.services.cloudfront.model.InvalidArgumentException) InvalidArgumentException(com.amazonaws.services.cloudfront.model.InvalidArgumentException) WrappedFile(org.apache.camel.WrappedFile) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) CompleteMultipartUploadRequest(com.amazonaws.services.s3.model.CompleteMultipartUploadRequest)

Example 5 with UploadResult

use of com.amazonaws.services.s3.transfer.model.UploadResult in project sandbox by irof.

the class S3MultipartUploadTest method 高レベルAPIでマルチパートアップロードする.

@Test
public void 高レベルAPIでマルチパートアップロードする() throws Exception {
    TransferManager transferManager = new TransferManager();
    TransferManagerConfiguration configuration = new TransferManagerConfiguration();
    // パートのサイズ: 5MB-5GB, default: 5MB
    configuration.setMinimumUploadPartSize(5 * Constants.MB);
    // 閾値: デフォルト16MB
    configuration.setMultipartUploadThreshold(5 * Constants.MB);
    transferManager.setConfiguration(configuration);
    // アップロードするデータを雑に用意(閾値より大きいの)
    byte[] bytes = new byte[5 * Constants.MB + 1];
    // 渡すものは通常のputObjectと同じ
    ObjectMetadata meta = new ObjectMetadata();
    meta.setContentLength(bytes.length);
    PutObjectRequest request = new PutObjectRequest("irof-sandbox", "S3MultipartUpload/hi-level-api.dat", new ByteArrayInputStream(bytes), meta);
    // アップロード実行
    Upload upload = transferManager.upload(request);
    // 完了まで待つ
    UploadResult result = upload.waitForUploadResult();
    // ETagは末尾に "-2" がつく
    logger.info(result.getETag());
    assertTrue(result.getETag().endsWith("-2"));
}
Also used : TransferManager(com.amazonaws.services.s3.transfer.TransferManager) TransferManagerConfiguration(com.amazonaws.services.s3.transfer.TransferManagerConfiguration) ByteArrayInputStream(java.io.ByteArrayInputStream) Upload(com.amazonaws.services.s3.transfer.Upload) UploadResult(com.amazonaws.services.s3.transfer.model.UploadResult) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Test(org.junit.Test)

Aggregations

InitiateMultipartUploadRequest (com.amazonaws.services.s3.model.InitiateMultipartUploadRequest)4 InitiateMultipartUploadResult (com.amazonaws.services.s3.model.InitiateMultipartUploadResult)4 UploadPartRequest (com.amazonaws.services.s3.model.UploadPartRequest)4 UploadPartResult (com.amazonaws.services.s3.model.UploadPartResult)3 AmazonS3 (com.amazonaws.services.s3.AmazonS3)2 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)2 CompleteMultipartUploadRequest (com.amazonaws.services.s3.model.CompleteMultipartUploadRequest)2 CompleteMultipartUploadResult (com.amazonaws.services.s3.model.CompleteMultipartUploadResult)2 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)2 PartETag (com.amazonaws.services.s3.model.PartETag)2 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)2 UploadResult (com.amazonaws.services.s3.transfer.model.UploadResult)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonServiceException (com.amazonaws.AmazonServiceException)1 SdkClientException (com.amazonaws.SdkClientException)1 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)1 Regions (com.amazonaws.regions.Regions)1