List of usage examples for com.amazonaws.services.s3.model GetObjectRequest getBucketName
public String getBucketName()
From source file:com.davidsoergel.s3napback.StreamingTransferManager.java
License:Apache License
private Download download(final GetObjectRequest getObjectRequest, final BufferedOutputStream os, final TransferStateChangeListener stateListener) { appendUserAgent(getObjectRequest, USER_AGENT); String description = "Downloading from " + getObjectRequest.getBucketName() + "/" + getObjectRequest.getKey(); // Add our own transfer progress listener TransferProgressImpl transferProgress = new TransferProgressImpl(); ProgressListenerChain listenerChain = new ProgressListenerChain( new TransferProgressUpdatingListener(transferProgress), getObjectRequest.getProgressListener()); getObjectRequest.setProgressListener(listenerChain); final S3Object s3Object = s3.getObject(getObjectRequest); final DownloadImpl download = new DownloadImpl(description, transferProgress, listenerChain, s3Object, stateListener);/*from www.j a v a 2s.c o m*/ // null is returned when constraints aren't met if (s3Object == null) { download.setState(Transfer.TransferState.Canceled); download.setMonitor(new DownloadMonitor(download, null)); return download; } long contentLength = s3Object.getObjectMetadata().getContentLength(); if (getObjectRequest.getRange() != null && getObjectRequest.getRange().length == 2) { long startingByte = getObjectRequest.getRange()[0]; long lastByte = getObjectRequest.getRange()[1]; contentLength = lastByte - startingByte; } transferProgress.setTotalBytesToTransfer(contentLength); Future<?> future = threadPool.submit(new Callable<Object>() { //@Override public Object call() throws Exception { try { download.setState(Transfer.TransferState.InProgress); StreamingServiceUtils.downloadObjectToStream(s3Object, os); download.setState(Transfer.TransferState.Completed); return true; } catch (Exception e) { // Downloads aren't allowed to move from canceled to failed if (download.getState() != Transfer.TransferState.Canceled) { download.setState(Transfer.TransferState.Failed); } throw e; } } }); download.setMonitor(new DownloadMonitor(download, future)); return download; }
From source file:edu.si.services.beans.cameratrap.AmazonS3ClientMock.java
License:Apache License
@Override public S3Object getObject(GetObjectRequest getObjectRequest) throws AmazonClientException, AmazonServiceException { return getObject(getObjectRequest.getBucketName(), getObjectRequest.getKey()); }
From source file:org.finra.dm.dao.impl.MockS3OperationsImpl.java
License:Apache License
@Override public S3Object getS3Object(GetObjectRequest getObjectRequest, AmazonS3 s3) { String bucketName = getObjectRequest.getBucketName(); String key = getObjectRequest.getKey(); if (MOCK_S3_BUCKET_NAME_NO_SUCH_BUCKET_EXCEPTION.equals(bucketName)) { AmazonServiceException amazonServiceException = new AmazonServiceException( S3Operations.ERROR_CODE_NO_SUCH_BUCKET); amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_NO_SUCH_BUCKET); throw amazonServiceException; }/*from w w w . j a v a 2 s. com*/ if (MOCK_S3_BUCKET_NAME_ACCESS_DENIED.equals(bucketName)) { AmazonServiceException amazonServiceException = new AmazonServiceException( S3Operations.ERROR_CODE_ACCESS_DENIED); amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_ACCESS_DENIED); throw amazonServiceException; } if (MOCK_S3_BUCKET_NAME_INTERNAL_ERROR.equals(bucketName)) { AmazonServiceException amazonServiceException = new AmazonServiceException( S3Operations.ERROR_CODE_INTERNAL_ERROR); amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_INTERNAL_ERROR); throw amazonServiceException; } MockS3Bucket mockS3Bucket = getOrCreateBucket(bucketName); MockS3Object mockS3Object = mockS3Bucket.getObjects().get(key); if (mockS3Object == null) { AmazonServiceException amazonServiceException = new AmazonServiceException( S3Operations.ERROR_CODE_NO_SUCH_KEY); amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_NO_SUCH_KEY); throw amazonServiceException; } S3Object s3Object = new S3Object(); s3Object.setBucketName(bucketName); s3Object.setKey(key); s3Object.setObjectContent(new ByteArrayInputStream(mockS3Object.getData())); s3Object.setObjectMetadata(mockS3Object.getObjectMetadata()); return s3Object; }
From source file:org.finra.dm.dao.impl.S3DaoImpl.java
License:Apache License
@Override public S3Object getS3Object(GetObjectRequest getObjectRequest, S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto) { AmazonS3Client s3 = getAmazonS3(s3FileTransferRequestParamsDto); try {/*from w w w. ja v a 2 s.c om*/ return s3Operations.getS3Object(getObjectRequest, s3); } catch (AmazonServiceException amazonServiceException) { String errorCode = amazonServiceException.getErrorCode(); switch (errorCode) { case S3Operations.ERROR_CODE_ACCESS_DENIED: throw new ObjectNotFoundException( "Application does not have access to the specified S3 object at bucket '" + getObjectRequest.getBucketName() + "' and key '" + getObjectRequest.getKey() + "'.", amazonServiceException); case S3Operations.ERROR_CODE_NO_SUCH_BUCKET: throw new ObjectNotFoundException( "Specified S3 bucket '" + getObjectRequest.getBucketName() + "' does not exist.", amazonServiceException); case S3Operations.ERROR_CODE_NO_SUCH_KEY: throw new ObjectNotFoundException( "Specified S3 object key '" + getObjectRequest.getKey() + "' does not exist.", amazonServiceException); default: throw amazonServiceException; } } }
From source file:org.finra.herd.dao.impl.MockS3OperationsImpl.java
License:Apache License
@Override public S3Object getS3Object(GetObjectRequest getObjectRequest, AmazonS3 s3) { MockS3Object mockS3Object = getMockS3Object(getObjectRequest.getBucketName(), getObjectRequest.getKey()); S3Object s3Object = new S3Object(); s3Object.setBucketName(getObjectRequest.getBucketName()); s3Object.setKey(getObjectRequest.getKey()); s3Object.setObjectContent(new ByteArrayInputStream(mockS3Object.getData())); s3Object.setObjectMetadata(mockS3Object.getObjectMetadata()); return s3Object; }
From source file:ws.salient.aws.s3.AmazonS3Wagon.java
License:Apache License
private boolean getIfModifiedSince(String path, File file, Date lastModified) throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException { GetObjectRequest getObject = new GetObjectRequest(repository.getHost(), getKey(path)); try {//from ww w. j a v a 2 s .c o m if (lastModified != null) { getObject.setModifiedSinceConstraint(lastModified); } S3Object s3Object = s3.getObject(getObject); if (s3Object != null) { log.info(file.getAbsolutePath() + ": " + String.valueOf(s3Object.getObjectMetadata().getContentLength())); try (InputStream in = s3Object.getObjectContent(); FileOutputStream fileOut = new FileOutputStream(file)) { IOUtils.copy(in, fileOut); } return true; } return false; } catch (AmazonS3Exception ex) { if (ex.getErrorCode().equals("NoSuchKey") || ex.getErrorCode().equals("AccessDenied")) { log.warn("s3://" + getObject.getBucketName() + "/" + getObject.getKey(), ex); throw new ResourceDoesNotExistException( "s3://" + getObject.getBucketName() + "/" + getObject.getKey(), ex); } throw new TransferFailedException(ex.getMessage(), ex); } catch (IOException ex) { throw new TransferFailedException(ex.getMessage(), ex); } }