/** * Method to generate pre-signed object URL to s3 object * @param bucketName AWS S3 bucket name * @param key (example: android/apkFolder/ApkName.apk) * @param ms espiration time in ms, i.e. 1 hour is 1000*60*60 * @return url String pre-signed URL */ public URL generatePreSignUrl(final String bucketName, final String key, long ms) { java.util.Date expiration = new java.util.Date(); long msec = expiration.getTime(); msec += ms; expiration.setTime(msec); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key); generatePresignedUrlRequest.setMethod(HttpMethod.GET); generatePresignedUrlRequest.setExpiration(expiration); URL url = s3client.generatePresignedUrl(generatePresignedUrlRequest); return url; }
@NotNull @Override public String getPreSignedUrl(@NotNull HttpMethod httpMethod, @NotNull String bucketName, @NotNull String objectKey, @NotNull Map<String, String> params) throws IOException { try { final Callable<String> resolver = () -> S3Util.withS3Client(params, client -> { final GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectKey, httpMethod) .withExpiration(new Date(System.currentTimeMillis() + getUrlLifetimeSec() * 1000)); return client.generatePresignedUrl(request).toString(); }); if (httpMethod == HttpMethod.GET) { return TeamCityProperties.getBoolean(TEAMCITY_S3_PRESIGNURL_GET_CACHE_ENABLED) ? myGetLinksCache.get(getCacheIdentity(params, objectKey, bucketName), resolver) : resolver.call(); } else { return resolver.call(); } } catch (Exception e) { final AWSException awsException = new AWSException(e.getCause()); if (StringUtil.isNotEmpty(awsException.getDetails())) { LOG.warn(awsException.getDetails()); } final String err = "Failed to create " + httpMethod.name() + " pre-signed URL for [" + objectKey + "] in bucket [" + bucketName + "]"; LOG.infoAndDebugDetails(err, awsException); throw new IOException(err + ": " + awsException.getMessage(), awsException); } }
@NotNull static URL generateUrl(@NotNull final String bucketName, @NotNull final String objectKey) throws IOException { try { LOGGER.info("Generating pre-signed URL for bucket: {}\tobject: {}", bucketName, objectKey); final long millisNow = System.currentTimeMillis(); final long expirationMillis = millisNow + 1000 * 60 * 120; //add 2 hours; final Date expiration = new java.util.Date(expirationMillis); final GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey); generatePresignedUrlRequest.setMethod(HttpMethod.GET); generatePresignedUrlRequest.setExpiration(expiration); return s3Client.generatePresignedUrl(generatePresignedUrlRequest); } catch (AmazonServiceException exception) { LOGGER.error("Error Message: {}", exception.getMessage()); LOGGER.error("HTTP Code: {}", exception.getStatusCode()); LOGGER.error("Error Code: {}", exception.getErrorCode()); LOGGER.error("Error Type: {}", exception.getErrorType()); LOGGER.error("Request ID: {}", exception.getRequestId()); } catch (AmazonClientException ace) { LOGGER.error("The client encountered an internal error while trying to communicate with S3"); LOGGER.error("Error Message: {}", ace.getMessage()); } throw new IOException("Failed to create sbp URL."); }
/** * {@inheritDoc} <p/> <p> A mock implementation which generates a URL which reflects the given request. </p> <p> The URL is composed as such: </p> <p/> * <pre> * https://{s3BucketName}/{s3ObjectKey}?{queryParams} * </pre> * <p> Where {@code queryParams} is the URL encoded list of parameters given in the request. </p> <p> The query params include: </p> TODO: list the query * params in the result. */ @Override public URL generatePresignedUrl(GeneratePresignedUrlRequest generatePresignedUrlRequest, AmazonS3 s3) { String host = generatePresignedUrlRequest.getBucketName(); StringBuilder file = new StringBuilder(); file.append('/').append(generatePresignedUrlRequest.getKey()); file.append("?method=").append(generatePresignedUrlRequest.getMethod()); file.append("&expiration=").append(generatePresignedUrlRequest.getExpiration().getTime()); try { return new URL("https", host, file.toString()); } catch (MalformedURLException e) { throw new RuntimeException(e); } }
public URL getSignedUrl(String p_s3_bucket, String p_s3_file, Date p_exires) { GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(p_s3_bucket, p_s3_file); generatePresignedUrlRequest.setMethod(HttpMethod.GET); // Default. generatePresignedUrlRequest.setExpiration(p_exires); return s3Client.generatePresignedUrl(generatePresignedUrlRequest); }
@Override public URL generatePresignedUrl(String bucketName, String key, Date expiration, HttpMethod method) throws SdkClientException { GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, method); request.setExpiration(expiration); return generatePresignedUrl(request); }
public static void main(String[] args) throws Exception { // create the AWS S3 Client AmazonS3 s3 = AWSS3Factory.getS3Client(); // retrieve the key value from user System.out.println( "Enter the object key:" ); String key = new BufferedReader( new InputStreamReader( System.in ) ).readLine(); // retrieve the expiration time for the object from user System.out.print( "How many hours should this tag be valid? " ); String hours = new BufferedReader( new InputStreamReader( System.in ) ).readLine(); // convert hours to a date Date expiration = new Date(); long curTime_msec = expiration.getTime(); long nHours = Long.valueOf(hours); curTime_msec += 60 * 60 * 1000 * nHours; expiration.setTime(curTime_msec); // generate the object's pre-signed URL GeneratePresignedUrlRequest presignedUrl = new GeneratePresignedUrlRequest(AWSS3Factory.S3_BUCKET, key); presignedUrl.setMethod(HttpMethod.GET); presignedUrl.setExpiration(expiration); URL url = s3.generatePresignedUrl(presignedUrl); // print object's pre-signed URL System.out.println( String.format("object [%s/%s] pre-signed URL: [%s]", AWSS3Factory.S3_BUCKET, key, url.toString())); }
@Override public String generateGetObjectPresignedUrl(String bucketName, String key, Date expiration, S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto) { GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.GET); generatePresignedUrlRequest.setExpiration(expiration); AmazonS3Client s3 = getAmazonS3(s3FileTransferRequestParamsDto); try { return s3Operations.generatePresignedUrl(generatePresignedUrlRequest, s3).toString(); } finally { s3.shutdown(); } }
@Override public URL generateObjectURL(final String bucketName, final String fileName) throws AmazonClientException, AmazonServiceException { LOGGER.info("generateObjectURL invoked, bucketName: {}, fileName: {}", bucketName, fileName); final GeneratePresignedUrlRequest presignedUrlReq = new GeneratePresignedUrlRequest(bucketName, fileName); return s3client.generatePresignedUrl(presignedUrlReq); }
@Override public URL generateObjectURL(final String bucketName, final String fileName, final Date expires) throws AmazonClientException, AmazonServiceException { LOGGER.info("generateObjectURL invoked, bucketName: {}, fileName: {} and expires: {}", bucketName, fileName, expires); final GeneratePresignedUrlRequest presignedUrlReq = new GeneratePresignedUrlRequest(bucketName, fileName); presignedUrlReq.setExpiration(expires); return s3client.generatePresignedUrl(presignedUrlReq); }
@Override public Optional<DirectDownloadHandle> getDirectDownloadHandle(String key) { GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucket, key); req.setExpiration(Date.from(Instant.now().plusSeconds(10*60))); String url = client.generatePresignedUrl(req).toString(); return Optional.of(DirectDownloadHandle.of(url)); }
@Override public Optional<DirectUploadHandle> getDirectUploadHandle(String key) { GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucket, key); req.setMethod(HttpMethod.PUT); req.setExpiration(Date.from(Instant.now().plusSeconds(10*60))); String url = client.generatePresignedUrl(req).toString(); return Optional.of(DirectUploadHandle.of(url)); }
/** * Computes the presigned URL for the given S3 resource. * * @param path String like "/bucketName/folder/folder/abc.txt" that * represents the resource to request. */ public URL buildPresignedURL(String path) throws AmazonClientException { AWSCredentials credentials = awsCredentialsProvider.getCredentials(); long expires = System.currentTimeMillis() + 60 * 60 * 1000; GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(path, credentials.getAWSSecretKey()); request.setExpiration(new Date(expires)); AmazonS3 s3 = new AmazonS3Client(credentials); return s3.generatePresignedUrl(request); }
@Override public cfData execute( cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException{ AmazonKey amazonKey = getAmazonKey(_session, argStruct); AmazonS3 s3Client = getAmazonS3(amazonKey); String bucket = getNamedStringParam(argStruct, "bucket", null ); String key = getNamedStringParam(argStruct, "key", null ); if ( bucket == null ) throwException(_session, "Please specify a bucket" ); if ( key == null ) throwException(_session, "Please specify a key" ); if ( key.charAt( 0 ) == '/' ) key = key.substring(1); cfData expired = getNamedParam(argStruct,"expiration", null ); try { Date expirationDate; if ( expired != null ){ if ( expired.getDataType() == cfData.CFDATEDATA ) expirationDate = new Date( expired.getLong() ); else expirationDate = new Date( System.currentTimeMillis() + (expired.getLong() * DateUtil.SECS_MS) ); }else{ expirationDate = new Date( System.currentTimeMillis() + DateUtil.DAY_MS*7 ); } GeneratePresignedUrlRequest g = new GeneratePresignedUrlRequest(bucket, key); g.setExpiration(expirationDate); return new cfStringData( s3Client.generatePresignedUrl(g).toString() ); } catch (Exception e) { throwException(_session, "AmazonS3: " + e.getMessage() ); return cfBooleanData.FALSE; } }
public static URL put(String key, File movie){ try{ System.out.println("Uploading a new object to S3 from a file\n"); s3.putObject(new PutObjectRequest(bucketName, key, movie)); System.out.println("Generating pre-signed URL."); java.util.Date expiration = new java.util.Date(); long milliSeconds = expiration.getTime(); milliSeconds += 1000 * 60 * 60 * 24 * 7; // Add 7 days. expiration.setTime(milliSeconds); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key); generatePresignedUrlRequest.setMethod(HttpMethod.GET); generatePresignedUrlRequest.setExpiration(expiration); return s3.generatePresignedUrl(generatePresignedUrlRequest); }catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon S3, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } return null; }
public static final String makeConfigFileURL(String configUrl) throws Exception { String url = null; if (configUrl.startsWith("http")) { url = configUrl; } else if (configUrl.startsWith("s3")) { AmazonS3 s3Client = new AmazonS3Client(); String bucket = configUrl.split("/")[2]; String prefix = configUrl.substring(configUrl.indexOf(bucket) + bucket.length() + 1); // generate a presigned url for X hours Date expiration = new Date(); long msec = expiration.getTime(); msec += 1000 * 60 * 60; // 1 hour. expiration.setTime(msec); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest( bucket, prefix); generatePresignedUrlRequest.setMethod(HttpMethod.GET); generatePresignedUrlRequest.setExpiration(expiration); URL s3url = s3Client.generatePresignedUrl(generatePresignedUrlRequest); url = s3url.toString(); } else { url = new URL(String.format("file://%s", configUrl)).toString(); } return url; }
protected WorkOrderResult generate(AmazonS3 s3Conn, AuthLinkSpec spec, Date ttl) { GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(spec.getTarget().getBucketName(), spec.getTarget().getObjectName()); generatePresignedUrlRequest.setMethod(HttpMethod.GET); generatePresignedUrlRequest.setExpiration(ttl); URL url =null; WorkOrderResult woResult=new WorkOrderResult(spec); // If WorkOrder contains an https request, generate that if(spec.getProtocol().isHttps()) { // Set HTTPS protocol URL by accessing the s3 https endpoint s3Conn.setEndpoint(HTTPS_ENDPOINT_); url = s3Conn.generatePresignedUrl(generatePresignedUrlRequest); woResult.getSignedHttpsUrl().setUrl(url.toString()); //woResult.getSignedHttpsUrl().setProtocol(HTTPS_); if(spec.getProcessing().isValidateAuthURL() == true) { if(validTarget(url)) { woResult.getSignedHttpsUrl().setValid(true); } } } // If WorkOrder contains an http request, generate that if(spec.getProtocol().isHttp()) { // Set HTTP protocol URL by accessing the s3 http endpoint s3Conn.setEndpoint(HTTP_ENDPOINT_); url = s3Conn.generatePresignedUrl(generatePresignedUrlRequest); woResult.getSignedHttpUrl().setUrl(url.toString()); //woResult.getSignedHttpUrl().setProtocol(HTTP_); if(spec.getProcessing().isValidateAuthURL() == true) { if(validTarget(url)) { woResult.getSignedHttpUrl().setValid(true); } } } return(woResult); }
String setACLurl(String object, String access_key, String secret_key, String endpoint, String bucket) { String URL = null; try { AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key); AmazonS3 s3Client = new AmazonS3Client(credentials, new ClientConfiguration()); if (endpoint.contains("amazonaws.com")) { String aws_endpoint = s3Client.getBucketLocation(new GetBucketLocationRequest(bucket)); if (aws_endpoint.contains("US")) { s3Client.setEndpoint("https://s3.amazonaws.com"); } else if (aws_endpoint.contains("us-west")) { s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com"); } else if (aws_endpoint.contains("eu-west")) { s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com"); } else if (aws_endpoint.contains("ap-")) { s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com"); } else if (aws_endpoint.contains("sa-east-1")) { s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com"); } else { s3Client.setEndpoint("https://s3." + aws_endpoint + ".amazonaws.com"); } } else { s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build()); s3Client.setEndpoint(endpoint); } java.util.Date expiration = new java.util.Date(); long milliSeconds = expiration.getTime(); milliSeconds += 1000 * 60 * 1000; // Add 1 hour. expiration.setTime(milliSeconds); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket, object); generatePresignedUrlRequest.setMethod(HttpMethod.GET); generatePresignedUrlRequest.setExpiration(expiration); URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest); URL = ("Pre-Signed URL = " + url.toString()); StringSelection stringSelection = new StringSelection(url.toString()); } catch (Exception setACLpublic) { } return URL; }
@Override public URL generatePresignedUrl(GeneratePresignedUrlRequest generatePresignedUrlRequest) throws AmazonClientException { return delegate.generatePresignedUrl(generatePresignedUrlRequest); }
@Override public URL generatePresignedUrl(GeneratePresignedUrlRequest generatePresignedUrlRequest) throws AmazonClientException { throw new UnsupportedOperationException(); }
/** * Returns a "complete" S3 specific signer, taking into the S3 bucket, key, * and the current S3 client configuration into account. */ protected Signer createSigner(final Request<?> request, final String bucketName, final String key, final boolean isAdditionalHeadRequestToFindRegion) { // Instead of using request.getEndpoint() for this parameter, we use endpoint which is because // in accelerate mode, the endpoint in request is regionless. We need the client-wide endpoint // to fetch the region information and pick the correct signer. URI uri = clientOptions.isAccelerateModeEnabled() ? endpoint : request.getEndpoint(); final Signer signer = getSignerByURI(uri); if(this.awsCredentialsProvider.getCredentials() instanceof IBMOAuthCredentials) { IBMOAuthCredentials oAuthCreds = (IBMOAuthCredentials)this.awsCredentialsProvider.getCredentials(); if (oAuthCreds.getApiKey() != null || oAuthCreds.getTokenManager() != null) { return new IBMOAuthSigner(); } } if (!isSignerOverridden()) { if ((signer instanceof AWSS3V4Signer) && bucketRegionShouldBeCached(request)) { String region = bucketRegionCache.get(bucketName); if (region != null) { // If cache contains the region for the bucket, create an endpoint for the region and // update the request with that endpoint. resolveRequestEndpoint(request, bucketName, key, RuntimeHttpUtils.toUri(RegionUtils.getRegion(region).getServiceEndpoint(S3_SERVICE_NAME), clientConfiguration)); return updateSigV4SignerWithRegion((AWSS3V4Signer) signer, region); } else if (request.getOriginalRequest() instanceof GeneratePresignedUrlRequest) { return createSigV2Signer(request, bucketName, key); } else if (isAdditionalHeadRequestToFindRegion) { return updateSigV4SignerWithRegion((AWSS3V4Signer) signer, "us-east-1"); } } String regionOverride = getSignerRegionOverride(); if (regionOverride != null) { return updateSigV4SignerWithRegion(new AWSS3V4Signer(), regionOverride); } } if (signer instanceof S3Signer) { // The old S3Signer needs a method and path passed to its // constructor; if that's what we should use, getSigner() // will return a dummy instance and we need to create a // new one with the appropriate values for this request. return createSigV2Signer(request, bucketName, key); } return signer; }
@Override public URL generatePresignedUrl(GeneratePresignedUrlRequest generatePresignedUrlRequest) throws SdkClientException { return call(() -> getDelegate().generatePresignedUrl(generatePresignedUrlRequest)); }
@Override public URL generatePresignedUrl(GeneratePresignedUrlRequest generatePresignedUrlRequest, AmazonS3 s3Client) { return s3Client.generatePresignedUrl(generatePresignedUrlRequest); }
@Override public URL generatePresignedUrl(GeneratePresignedUrlRequest generatePresignedUrlRequest) throws AmazonClientException { return null; }
@Test public void testAwsV2UrlSigningWithOverrideParameters() throws Exception { client = AmazonS3ClientBuilder.standard() .withClientConfiguration(V2_SIGNER_CONFIG) .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withEndpointConfiguration(s3EndpointConfig).build(); String blobName = "foo"; ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(BYTE_SOURCE.size()); client.putObject(containerName, blobName, BYTE_SOURCE.openStream(), metadata); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(containerName, blobName); generatePresignedUrlRequest.setMethod(HttpMethod.GET); ResponseHeaderOverrides headerOverride = new ResponseHeaderOverrides(); headerOverride.setContentDisposition("attachment; " + blobName); headerOverride.setContentType("text/plain"); generatePresignedUrlRequest.setResponseHeaders(headerOverride); Date expiration = new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(1)); generatePresignedUrlRequest.setExpiration(expiration); URL url = client.generatePresignedUrl(generatePresignedUrlRequest); URLConnection connection = url.openConnection(); try (InputStream actual = connection.getInputStream(); InputStream expected = BYTE_SOURCE.openStream()) { String value = connection.getHeaderField("Content-Disposition"); assertThat(value).isEqualTo(headerOverride.getContentDisposition()); value = connection.getHeaderField("Content-Type"); assertThat(value).isEqualTo(headerOverride.getContentType()); assertThat(actual).hasContentEqualTo(expected); } }
@Override public List<FileResource> getFileUrls(Long userId, String type) { List<FileResource> retval = new ArrayList<FileResource>(); long validfor = new Long(validHours).longValue() * HOURS_TO_MILLISECONDS; try { AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); String prefix = userId.toString() + S3_SEPARATOR + type; LOG.debug("Listing objects from bucket " + bucketName + " with prefix " + prefix); ListObjectsRequest listObjectsRequest = new ListObjectsRequest() .withBucketName(bucketName).withPrefix(prefix); ObjectListing objectListing; do { objectListing = s3client.listObjects(listObjectsRequest); for (S3ObjectSummary objectSummary : objectListing .getObjectSummaries()) { LOG.debug(" - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"); Date expiration = new Date(); long milliSeconds = expiration.getTime(); milliSeconds += validfor; expiration.setTime(milliSeconds); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest( bucketName, objectSummary.getKey()); generatePresignedUrlRequest.setMethod(HttpMethod.GET); generatePresignedUrlRequest.setExpiration(expiration); FileResource res = new FileResource(); res.setFileURL(s3client.generatePresignedUrl(generatePresignedUrlRequest)); retval.add(res); } listObjectsRequest.setMarker(objectListing.getNextMarker()); } while (objectListing.isTruncated()); } catch (AmazonServiceException ase) { logAWSServiceException(ase); } catch (AmazonClientException ace) { logAWSClientException(ace); } catch (Exception e) { LOG.error("Unable to retrieve S3 file URLs " + e.getMessage()); } return retval; }
@Override public URL generatePresignedUrl(GeneratePresignedUrlRequest generatePresignedUrlRequest) throws AmazonClientException { // TODO Auto-generated method stub return null; }
/** * Returns a pre-signed URL for accessing an Amazon S3 resource. * * @param generatePresignedUrlRequest the request object containing all the options for generating a pre-signed URL (bucket name, key, expiration date, * etc) * @param s3Client the {@link AmazonS3} implementation to use * * @return the pre-signed URL which expires at the specified time, and can be used to allow anyone to download the specified object from S3, without * exposing the owner's AWS secret access key */ public URL generatePresignedUrl(GeneratePresignedUrlRequest generatePresignedUrlRequest, AmazonS3 s3Client);