/** * @deprecated use {@link AmazonS3ClientBuilder#setEndpointConfiguration(AwsClientBuilder.EndpointConfiguration)} */ @Override @Deprecated public synchronized void setEndpoint(String endpoint) { if (ServiceUtils.isS3AccelerateEndpoint(endpoint)) { throw new IllegalStateException("To enable accelerate mode, please use AmazonS3ClientBuilder.withAccelerateModeEnabled(true)"); } else { super.setEndpoint(endpoint); /* * Extract the region string from the endpoint if it's not known to be a * global S3 endpoint. */ if (!ServiceUtils.isS3USStandardEndpoint(endpoint)) { clientRegion = AwsHostNameUtils.parseRegionName(this.endpoint.getHost(), S3_SERVICE_NAME); } } }
@Override public ObjectMetadata getObject(final GetObjectRequest getObjectRequest, File destinationFile) throws SdkClientException, AmazonServiceException { rejectNull(destinationFile, "The destination file parameter must be specified when downloading an object directly to a file"); S3Object s3Object = ServiceUtils.retryableDownloadS3ObjectToFile(destinationFile, new ServiceUtils.RetryableS3DownloadTask() { @Override public S3Object getS3ObjectStream() { return getObject(getObjectRequest); } @Override public boolean needIntegrityCheck() { return !skipMd5CheckStrategy.skipClientSideValidationPerRequest(getObjectRequest); } }, ServiceUtils.OVERWRITE_MODE); // getObject can return null if constraints were specified but not met if (s3Object == null) return null; return s3Object.getObjectMetadata(); }
private void addTransitions(XmlWriter xml, List<Transition> transitions) { if (transitions == null || transitions.isEmpty()) { return; } for (Transition t : transitions) { if (t != null) { xml.start("Transition"); if (t.getDate() != null) { xml.start("Date"); xml.value(ServiceUtils.formatIso8601Date(t.getDate())); xml.end(); } if (t.getDays() != -1) { xml.start("Days"); xml.value(Integer.toString(t.getDays())); xml.end(); } xml.start("StorageClass"); xml.value(t.getStorageClass().toString()); xml.end(); // <StorageClass> xml.end(); // </Transition> } } }
@Override protected void doEndElement(String uri, String name, String qName) { if (in("CopyObjectResult") || in ("CopyPartResult")) { if (name.equals("LastModified")) { result.setLastModifiedDate(ServiceUtils.parseIso8601Date(getText())); } else if (name.equals("ETag")) { result.setETag(ServiceUtils.removeQuotes(getText())); } } else if (in("Error")) { if (name.equals("Code")) { errorCode = getText(); } else if (name.equals("Message")) { errorMessage = getText(); } else if (name.equals("RequestId")) { errorRequestId = getText(); } else if (name.equals("HostId")) { errorHostId = getText(); } } }
@Override public File call() throws Exception { for (Future<File> file : partFiles) { ServiceUtils.appendFile(file.get(), destinationFile); download.updatePersistableTransfer(currentPartNumber++); } download.setState(Transfer.TransferState.Completed); return destinationFile; }
@Override protected void doEndElement(String uri, String name, String qName) { if (atTopLevel()) { if (name.equals("Error")) { if (ase != null) { ase.setErrorCode(errorCode); ase.setRequestId(requestId); ase.setExtendedRequestId(hostId); } } } else if (in("CompleteMultipartUploadResult")) { if (name.equals("Location")) { result.setLocation(getText()); } else if (name.equals("Bucket")) { result.setBucketName(getText()); } else if (name.equals("Key")) { result.setKey(getText()); } else if (name.equals("ETag")) { result.setETag(ServiceUtils.removeQuotes(getText())); } } else if (in("Error")) { if (name.equals("Code")) { errorCode = getText(); } else if (name.equals("Message")) { ase = new AmazonS3Exception(getText()); } else if (name.equals("RequestId")) { requestId = getText(); } else if (name.equals("HostId")) { hostId = getText(); } } }
@Override public Signer getSigner(SignerProviderContext signerProviderContext) { URI uri = signerProviderContext.getUri(); // Return the default signer if no URI is passed, the client is configured with region override, // or if we are hitting an accelerate endpoint. if (uri == null || ServiceUtils.isS3AccelerateEndpoint(uri.getHost()) || isSignerRegionOverrideSet()) { return signer; } if (signer instanceof RegionAwareSigner) { // Parse region name from the host component of the URL and // assign it to the signer RegionAwareSigner regionSigner = (RegionAwareSigner) signer; try { regionSigner.setRegionName(AwsHostNameUtils.parseRegionName( uri.getHost(), "s3")); } catch (RuntimeException e) { log.warn("Failed to parse the endpoint " + uri + ", and skip re-assigning the signer region", e); } } return signer; }
/** * Get or initialize the S3 client. * Note: this method must be synchronized because we're accessing the * {@link #s3Client} field and we're calling this method from a worker thread. * @return the S3 client */ private synchronized AmazonS3 getS3Client() { if (s3Client == null) { BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); AmazonS3ClientBuilder builder = AmazonS3ClientBuilder .standard() .withCredentials(new AWSStaticCredentialsProvider(credentials)); if (forceSignatureV2) { ClientConfigurationFactory configFactory = new ClientConfigurationFactory(); ClientConfiguration config = configFactory.getConfig(); config.setSignerOverride("S3SignerType"); builder = builder.withClientConfiguration(config); } String endpoint = "http://" + host + ":" + port; String clientRegion = null; if (!ServiceUtils.isS3USStandardEndpoint(endpoint)) { clientRegion = AwsHostNameUtils.parseRegion(host, AmazonS3Client.S3_SERVICE_NAME); } builder = builder.withEndpointConfiguration(new EndpointConfiguration( endpoint, clientRegion)); builder = builder.withPathStyleAccessEnabled(pathStyleAccess); s3Client = builder.build(); } return s3Client; }
@Override public URL getCreateSpeechUrl(CreateSpeechRequest createSpeechRequest) throws UnsupportedEncodingException { createSpeechRequest.setMethodType(MethodType.GET); Request<CreateSpeechRequest> request = CreateSpeechRequestMarshallerFactory.getMarshaller( createSpeechRequest.getMethodType()).marshall(createSpeechRequest); return ServiceUtils.convertRequestToUrl(prepareRequestForGetUrl(request)); }
@Override public URL getListVoicesUrl(ListVoicesRequest listVoicesRequest) throws UnsupportedEncodingException { listVoicesRequest.setMethodType(MethodType.GET); Request<ListVoicesRequest> request = ListVoicesRequestMarshallerFactory.getMarshaller( listVoicesRequest.getMethodType()).marshall(listVoicesRequest); return ServiceUtils.convertRequestToUrl(prepareRequestForGetUrl(request)); }
@Override public URL getUrl(String bucketName, String key) { Request<?> request = new DefaultRequest<Object>(Constants.S3_SERVICE_DISPLAY_NAME); resolveRequestEndpoint(request, bucketName, key, endpoint); return ServiceUtils.convertRequestToUrl(request, false, false); }
/** * This method must return a non-null object, or else the existing * implementation in {@link AbstractTransfer#waitForCompletion()} * would block forever. * * @return the downloaded file */ @Override public File call() throws Exception { try { latch.await(); if (isTimeoutEnabled()) { timedExecutor.schedule(new Runnable() { public void run() { try { if (download.getState() != TransferState.Completed) { download.abort(); } } catch(Exception e) { throw new SdkClientException( "Unable to abort download after timeout", e); } } }, timeout, TimeUnit.MILLISECONDS); } download.setState(TransferState.InProgress); ServiceUtils.createParentDirectoryIfNecessary(dstfile); if (isDownloadParallel) { downloadInParallel(ServiceUtils.getPartCount(req, s3)); } else { S3Object s3Object = retryableDownloadS3ObjectToFile(dstfile, new DownloadTaskImpl(s3, download, req)); updateDownloadStatus(s3Object); } return dstfile; } catch (Throwable t) { // Cancel all the futures for (Future<File> f : futureFiles) { f.cancel(true); } // Downloads aren't allowed to move from canceled to failed if (download.getState() != TransferState.Canceled) { download.setState(TransferState.Failed); } if (t instanceof Exception) throw (Exception) t; else throw (Error) t; } }
private S3Object retryableDownloadS3ObjectToFile(File file, RetryableS3DownloadTask retryableS3DownloadTask) { boolean hasRetried = false; S3Object s3Object; for (;;) { final boolean appendData = resumeExistingDownload || (resumeOnRetry && hasRetried); if (appendData && hasRetried) { // Need to adjust the get range or else we risk corrupting the downloaded file adjustRequest(req); } s3Object = retryableS3DownloadTask.getS3ObjectStream(); if (s3Object == null) return null; try { if (testing && resumeExistingDownload && !hasRetried) { throw new SdkClientException("testing"); } ServiceUtils.downloadToFile(s3Object, file, retryableS3DownloadTask.needIntegrityCheck(), appendData, expectedFileLength); return s3Object; } catch (AmazonClientException ace) { if (!ace.isRetryable()) throw ace; // Determine whether an immediate retry is needed according to the captured SdkClientException. // (There are three cases when downloadObjectToFile() throws SdkClientException: // 1) SocketException or SSLProtocolException when writing to disk (e.g. when user aborts the download) // 2) Other IOException when writing to disk // 3) MD5 hashes don't match // For 1) If SocketException is the result of the client side resetting the connection, this is retried // Cases 2) and 3) will always be retried final Throwable cause = ace.getCause(); if ((cause instanceof SocketException && !cause.getMessage().equals("Connection reset")) || (cause instanceof SSLProtocolException)) { throw ace; } else { if (hasRetried) throw ace; else { LOG.info("Retry the download of object " + s3Object.getKey() + " (bucket " + s3Object.getBucketName() + ")", ace); hasRetried = true; } } } finally { s3Object.getObjectContent().abort(); } } }
private void writeRule(XmlWriter xml, Rule rule) { xml.start("Rule"); if (rule.getId() != null) { xml.start("ID").value(rule.getId()).end(); } writePrefix(xml, rule); xml.start("Status").value(rule.getStatus()).end(); writeLifecycleFilter(xml, rule.getFilter()); addTransitions(xml, rule.getTransitions()); addNoncurrentTransitions(xml, rule.getNoncurrentVersionTransitions()); if (hasCurrentExpirationPolicy(rule)) { // The rule attributes below are mutually exclusive, the service will throw an error if // more than one is provided xml.start("Expiration"); if (rule.getExpirationInDays() != -1) { xml.start("Days").value("" + rule.getExpirationInDays()).end(); } if (rule.getExpirationDate() != null) { xml.start("Date").value(ServiceUtils.formatIso8601Date(rule.getExpirationDate())).end(); } if (rule.isExpiredObjectDeleteMarker() == true) { xml.start("ExpiredObjectDeleteMarker").value("true").end(); } xml.end(); // </Expiration> } if (rule.getNoncurrentVersionExpirationInDays() != -1) { xml.start("NoncurrentVersionExpiration"); xml.start("NoncurrentDays") .value(Integer.toString( rule.getNoncurrentVersionExpirationInDays())) .end(); xml.end(); // </NoncurrentVersionExpiration> } if (rule.getAbortIncompleteMultipartUpload() != null) { xml.start("AbortIncompleteMultipartUpload"); xml.start("DaysAfterInitiation"). value(Integer.toString(rule.getAbortIncompleteMultipartUpload().getDaysAfterInitiation())) .end(); xml.end(); // </AbortIncompleteMultipartUpload> } xml.end(); // </Rule> }
@Override protected void doEndElement(String uri, String name, String qName) { if (in("ListMultipartUploadsResult")) { if (name.equals("Bucket")) { result.setBucketName(getText()); } else if (name.equals("KeyMarker")) { result.setKeyMarker(checkForEmptyString(getText())); } else if (name.equals("Delimiter")) { result.setDelimiter(checkForEmptyString(getText())); } else if (name.equals("Prefix")) { result.setPrefix(checkForEmptyString(getText())); } else if (name.equals("UploadIdMarker")) { result.setUploadIdMarker(checkForEmptyString(getText())); } else if (name.equals("NextKeyMarker")) { result.setNextKeyMarker(checkForEmptyString(getText())); } else if (name.equals("NextUploadIdMarker")) { result.setNextUploadIdMarker(checkForEmptyString(getText())); } else if (name.equals("MaxUploads")) { result.setMaxUploads(Integer.parseInt(getText())); } else if (name.equals("EncodingType")) { result.setEncodingType(checkForEmptyString(getText())); } else if (name.equals("IsTruncated")) { result.setTruncated(Boolean.parseBoolean(getText())); } else if (name.equals("Upload")) { result.getMultipartUploads().add(currentMultipartUpload); currentMultipartUpload = null; } } else if (in("ListMultipartUploadsResult", "CommonPrefixes")) { if (name.equals("Prefix")) { result.getCommonPrefixes().add(getText()); } } else if (in("ListMultipartUploadsResult", "Upload")) { if (name.equals("Key")) { currentMultipartUpload.setKey(getText()); } else if (name.equals("UploadId")) { currentMultipartUpload.setUploadId(getText()); } else if (name.equals("Owner")) { currentMultipartUpload.setOwner(currentOwner); currentOwner = null; } else if (name.equals("Initiator")) { currentMultipartUpload.setInitiator(currentOwner); currentOwner = null; } else if (name.equals("StorageClass")) { currentMultipartUpload.setStorageClass(getText()); } else if (name.equals("Initiated")) { currentMultipartUpload.setInitiated( ServiceUtils.parseIso8601Date(getText())); } } else if (in("ListMultipartUploadsResult", "Upload", "Owner") || in("ListMultipartUploadsResult", "Upload", "Initiator")) { if (name.equals("ID")) { currentOwner.setId(checkForEmptyString(getText())); } else if (name.equals("DisplayName")) { currentOwner.setDisplayName(checkForEmptyString(getText())); } } }
@Override protected void doEndElement(String uri, String name, String qName) { if (in("ListPartsResult")) { if (name.equals("Bucket")) { result.setBucketName(getText()); } else if (name.equals("Key")) { result.setKey(getText()); } else if (name.equals("UploadId")) { result.setUploadId(getText()); } else if (name.equals("Owner")) { result.setOwner(currentOwner); currentOwner = null; } else if (name.equals("Initiator")) { result.setInitiator(currentOwner); currentOwner = null; } else if (name.equals("StorageClass")) { result.setStorageClass(getText()); } else if (name.equals("PartNumberMarker")) { result.setPartNumberMarker(parseInteger(getText())); } else if (name.equals("NextPartNumberMarker")) { result.setNextPartNumberMarker(parseInteger(getText())); } else if (name.equals("MaxParts")) { result.setMaxParts(parseInteger(getText())); } else if (name.equals("EncodingType")) { result.setEncodingType(checkForEmptyString(getText())); } else if (name.equals("IsTruncated")) { result.setTruncated(Boolean.parseBoolean(getText())); } else if (name.equals("Part")) { result.getParts().add(currentPart); currentPart = null; } } else if (in("ListPartsResult", "Part")) { if (name.equals("PartNumber")) { currentPart.setPartNumber(Integer.parseInt(getText())); } else if (name.equals("LastModified")) { currentPart.setLastModified( ServiceUtils.parseIso8601Date(getText())); } else if (name.equals("ETag")) { currentPart.setETag(ServiceUtils.removeQuotes(getText())); } else if (name.equals("Size")) { currentPart.setSize(Long.parseLong(getText())); } } else if (in("ListPartsResult", "Owner") || in("ListPartsResult", "Initiator")) { if (name.equals("ID")) { currentOwner.setId(checkForEmptyString(getText())); } else if (name.equals("DisplayName")) { currentOwner.setDisplayName(checkForEmptyString(getText())); } } }
/** * <p> * Adds the specified date header in RFC 822 date format to the specified * request. * This method will not add a date header if the specified date value is <code>null</code>. * </p> * * @param request * The request to add the header to. * @param header * The header name. * @param value * The header value. */ private static void addDateHeader(Request<?> request, String header, Date value) { if (value != null) { request.addHeader(header, ServiceUtils.formatRfc822Date(value)); } }
/** * <p> * Adds the specified string list header, joined together separated with * commas, to the specified request. * This method will not add a string list header if the specified values * are <code>null</code> or empty. * </p> * * @param request * The request to add the header to. * @param header * The header name. * @param values * The list of strings to join together for the header value. */ private static void addStringListHeader(Request<?> request, String header, List<String> values) { if (values != null && !values.isEmpty()) { request.addHeader(header, ServiceUtils.join(values)); } }