@Override protected void logout() throws BackgroundException { try { client.shutdown(); } catch(ServiceException e) { throw new S3ExceptionMappingService().map(e); } }
@Override public BackgroundException map(final ServiceException e) { if(e.getCause() instanceof ServiceException) { return this.map((ServiceException) e.getCause()); this.append(buffer, StringEscapeUtils.unescapeXml(e.getErrorMessage())); this.append(buffer, e.getResponseStatus()); this.append(buffer, e.getMessage());
private Future<MultipartPart> submit(final Path source, final MultipartUpload multipart, final int partNumber, final long offset, final long length) { if(log.isInfoEnabled()) { log.info(String.format("Submit part %d of %s to queue with offset %d and length %d", partNumber, source, offset, length)); } return pool.execute(new Callable<MultipartPart>() { @Override public MultipartPart call() throws BackgroundException { try { final HttpRange range = HttpRange.byLength(offset, length); final MultipartPart part = session.getClient().multipartUploadPartCopy(multipart, partNumber, containerService.getContainer(source).getName(), containerService.getKey(source), null, null, null, null, range.getStart(), range.getEnd(), source.attributes().getVersionId()); if(log.isInfoEnabled()) { log.info(String.format("Received response %s for part number %d", part, partNumber)); } // Populate part with response data that is accessible via the object's metadata return new MultipartPart(partNumber, null == part.getLastModified() ? new Date(System.currentTimeMillis()) : part.getLastModified(), null == part.getEtag() ? StringUtils.EMPTY : part.getEtag(), part.getSize()); } catch(S3ServiceException e) { throw new S3ExceptionMappingService().map("Cannot copy {0}", e, source); } } }); } }
throw new S3ExceptionMappingService().map("Upload {0} failed", e, file);
@Override public List<MultipartPart> list(final MultipartUpload multipart) throws BackgroundException { if(log.isInfoEnabled()) { log.info(String.format("List completed parts of %s", multipart.getUploadId())); } // This operation lists the parts that have been uploaded for a specific multipart upload. try { return session.getClient().multipartListParts(multipart); } catch(S3ServiceException e) { throw new S3ExceptionMappingService().map(MessageFormat.format("Upload {0} failed", multipart.getObjectKey()), e); } }
throw new S3ExceptionMappingService().map("Listing directory {0} failed", e, prefix);
@Override public boolean getStatus(final Path file) throws BackgroundException { final Path bucket = containerService.getContainer(file); try { return session.getClient().getAccelerateConfig(bucket.getName()).isEnabled(); } catch(S3ServiceException failure) { throw new S3ExceptionMappingService().map("Failure to read attributes of {0}", failure, bucket); } }
@Override public StorageObject call(final AbstractHttpEntity entity) throws BackgroundException { try { final RequestEntityRestStorageService client = session.getClient(); client.putObjectWithRequestEntityImpl( containerService.getContainer(file).getName(), object, entity, status.getParameters()); if(log.isDebugEnabled()) { log.debug(String.format("Saved object %s with checksum %s", file, object.getETag())); } } catch(ServiceException e) { throw new S3ExceptionMappingService().map("Upload {0} failed", e, file); } return object; }
@Override public boolean retryRequest(final HttpResponse response, final int executionCount, final HttpContext context) { switch(response.getStatusLine().getStatusCode()) { case HttpStatus.SC_BAD_REQUEST: if(executionCount <= MAX_RETRIES) { final S3ServiceException failure; try { if(null != response.getEntity()) { EntityUtils.updateEntity(response, new BufferedHttpEntity(response.getEntity())); failure = new S3ServiceException(response.getStatusLine().getReasonPhrase(), EntityUtils.toString(response.getEntity())); if(new S3ExceptionMappingService().map(failure) instanceof ExpiredTokenException) { try { host.setCredentials(new STSCredentialsConfigurator(prompt).configure(host)); return true; } catch(LoginFailureException | LoginCanceledException e) { log.warn(String.format("Attempt to renew expired token failed. %s", e.getMessage())); } } } } catch(IOException e) { log.warn(String.format("Failure parsing response entity from %s", response)); } } } return false; } }
@Override public void delete(final MultipartUpload upload) throws BackgroundException { if(log.isInfoEnabled()) { log.info(String.format("Delete multipart upload %s", upload.getUploadId())); } try { session.getClient().multipartAbortUpload(upload); } catch(S3ServiceException e) { throw new S3ExceptionMappingService().map("Cannot delete {0}", e, new Path(new Path(PathNormalizer.normalize(upload.getBucketName()), EnumSet.of(Path.Type.directory)), upload.getObjectKey(), EnumSet.of(Path.Type.file))); } } }
protected String copy(final Path source, final S3Object destination, final TransferStatus status) throws BackgroundException { try { // Copying object applying the metadata of the original final Map<String, Object> stringObjectMap = session.getClient().copyVersionedObject(source.attributes().getVersionId(), containerService.getContainer(source).getName(), containerService.getKey(source), destination.getBucketName(), destination, false); final Map complete = (Map) stringObjectMap.get(Constants.KEY_FOR_COMPLETE_METADATA); return (String) complete.get(Constants.AMZ_VERSION_ID); } catch(ServiceException e) { throw new S3ExceptionMappingService().map("Cannot copy {0}", e, source); } }
final BackgroundException failure = new S3ExceptionMappingService().map("Upload {0} failed", e, directory); if(failure instanceof NotfoundException) { return Collections.emptyList();
@Override public void setStatus(final Path file, final boolean enabled) throws BackgroundException { final Path bucket = containerService.getContainer(file); try { if(!ServiceUtils.isBucketNameValidDNSName(bucket.getName())) { throw new InteroperabilityException("The name of the bucket used for Transfer Acceleration must be DNS-compliant and must not contain periods."); } session.getClient().setAccelerateConfig(bucket.getName(), new AccelerateConfig(enabled)); } catch(S3ServiceException failure) { throw new S3ExceptionMappingService().map("Failure to write attributes of {0}", failure, bucket); } }
throw new S3ExceptionMappingService().map("Cannot copy {0}", e, source);
throw new S3ExceptionMappingService().map("Cannot read bucket versioning status", e);
@Override public MultipartPart call() throws BackgroundException { try { final HttpRange range = HttpRange.byLength(offset, length); final MultipartPart part = session.getClient().multipartUploadPartCopy(multipart, partNumber, containerService.getContainer(source).getName(), containerService.getKey(source), null, null, null, null, range.getStart(), range.getEnd(), source.attributes().getVersionId()); if(log.isInfoEnabled()) { log.info(String.format("Received response %s for part number %d", part, partNumber)); } // Populate part with response data that is accessible via the object's metadata return new MultipartPart(partNumber, null == part.getLastModified() ? new Date(System.currentTimeMillis()) : part.getLastModified(), null == part.getEtag() ? StringUtils.EMPTY : part.getEtag(), part.getSize()); } catch(S3ServiceException e) { throw new S3ExceptionMappingService().map("Cannot copy {0}", e, source); } } });
throw new IOException(e.getErrorMessage(), new S3ExceptionMappingService().map(e));
public void create(final Path bucket, final String location) throws BackgroundException { // Create bucket if(!ServiceUtils.isBucketNameValidDNSName(bucket.getName())) { throw new InteroperabilityException(LocaleFactory.localizedString("Bucket name is not DNS compatible", "S3")); } AccessControlList acl; if(PreferencesFactory.get().getProperty("s3.bucket.acl.default").equals("public-read")) { acl = GSAccessControlList.REST_CANNED_PUBLIC_READ; } else { acl = GSAccessControlList.REST_CANNED_PRIVATE; } try { session.getClient().createBucket(new S3PathContainerService().getContainer(bucket).getName(), location, acl); } catch(ServiceException e) { throw new S3ExceptionMappingService().map("Cannot create folder {0}", e, bucket); } } }
@Override public void setConfiguration(final Path file, final LoggingConfiguration configuration) throws BackgroundException { // Logging target bucket final Path bucket = containerService.getContainer(file); try { final S3BucketLoggingStatus status = new S3BucketLoggingStatus( StringUtils.isNotBlank(configuration.getLoggingTarget()) ? configuration.getLoggingTarget() : bucket.getName(), null); if(configuration.isEnabled()) { status.setLogfilePrefix(PreferencesFactory.get().getProperty("s3.logging.prefix")); } session.getClient().setBucketLoggingStatus(bucket.getName(), status, true); } catch(ServiceException e) { throw new S3ExceptionMappingService().map("Failure to write attributes of {0}", e, file); } } }
throw new S3ExceptionMappingService().map("Upload {0} failed", e, file);