com.amazonaws.HttpMethod Java Examples
The following examples show how to use
com.amazonaws.HttpMethod.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: S3PresignUrlStepTests.java From pipeline-aws-plugin with Apache License 2.0 | 7 votes |
@Test public void presignWithCustomMethod() throws Exception { WorkflowJob job = this.jenkinsRule.jenkins.createProject(WorkflowJob.class, "s3PresignTest"); job.setDefinition(new CpsFlowDefinition("" + "node {\n" + " def url = s3PresignURL(bucket: 'foo', key: 'bar', httpMethod: 'POST')\n" + " echo \"url=$url\"\n" + "}\n", true) ); String urlString = "http://localhost:283/sdkd"; URL url = new URL(urlString); Mockito.when(this.s3.generatePresignedUrl(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(url); WorkflowRun run = this.jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0)); jenkinsRule.assertLogContains("url=" + urlString, run); ArgumentCaptor<Date> expirationCaptor = ArgumentCaptor.forClass(Date.class); Mockito.verify(s3).generatePresignedUrl(Mockito.eq("foo"), Mockito.eq("bar"), Mockito.any(), Mockito.eq(HttpMethod.POST)); }
Example #2
Source File: S3PresignUrlStep.java From pipeline-aws-plugin with Apache License 2.0 | 6 votes |
@DataBoundConstructor public S3PresignUrlStep(String bucket, String key, String httpMethod, Integer durationInSeconds, boolean pathStyleAccessEnabled, boolean payloadSigningEnabled) { super(pathStyleAccessEnabled, payloadSigningEnabled); this.bucket = bucket; this.key = key; if (durationInSeconds == null) { this.durationInSeconds = 60; //60 seconds } else { this.durationInSeconds = durationInSeconds; } if (httpMethod == null) { this.httpMethod = HttpMethod.GET; } else { this.httpMethod = HttpMethod.valueOf(httpMethod); } }
Example #3
Source File: S3PresignUrlStepTests.java From pipeline-aws-plugin with Apache License 2.0 | 6 votes |
@Test public void presignWithDefaultExpiration() throws Exception { WorkflowJob job = this.jenkinsRule.jenkins.createProject(WorkflowJob.class, "s3PresignTest"); job.setDefinition(new CpsFlowDefinition("" + "node {\n" + " def url = s3PresignURL(bucket: 'foo', key: 'bar')\n" + " echo \"url=$url\"\n" + "}\n", true) ); //defaults to 1 minute //minus a buffer for the test Date expectedDate = DateTime.now().plusMinutes(1).minusSeconds(10).toDate(); String urlString = "http://localhost:283/sdkd"; URL url = new URL(urlString); Mockito.when(this.s3.generatePresignedUrl(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(url); WorkflowRun run = this.jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0)); jenkinsRule.assertLogContains("url=" + urlString, run); ArgumentCaptor<Date> expirationCaptor = ArgumentCaptor.forClass(Date.class); Mockito.verify(s3).generatePresignedUrl(Mockito.eq("foo"), Mockito.eq("bar"), expirationCaptor.capture(), Mockito.eq(HttpMethod.GET)); Assertions.assertThat(expirationCaptor.getValue()).isAfterOrEqualsTo(expectedDate); }
Example #4
Source File: S3PresignUrlStepTests.java From pipeline-aws-plugin with Apache License 2.0 | 6 votes |
@Test public void presignWithExpiration() throws Exception { WorkflowJob job = this.jenkinsRule.jenkins.createProject(WorkflowJob.class, "s3PresignTest"); job.setDefinition(new CpsFlowDefinition("" + "node {\n" + " def url = s3PresignURL(bucket: 'foo', key: 'bar', durationInSeconds: 10000)\n" + " echo \"url=$url\"\n" + "}\n", true) ); String urlString = "http://localhost:283/sdkd"; URL url = new URL(urlString); Mockito.when(this.s3.generatePresignedUrl(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(url); //minus a buffer for the test Date expectedDate = DateTime.now().plusSeconds(10000).minusSeconds(25).toDate(); WorkflowRun run = this.jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0)); jenkinsRule.assertLogContains("url=" + urlString, run); ArgumentCaptor<Date> expirationCaptor = ArgumentCaptor.forClass(Date.class); Mockito.verify(s3).generatePresignedUrl(Mockito.eq("foo"), Mockito.eq("bar"), expirationCaptor.capture(), Mockito.eq(HttpMethod.GET)); Assertions.assertThat(expirationCaptor.getValue()).isAfterOrEqualsTo(expectedDate); }
Example #5
Source File: S3PreSignedUrlProviderImpl.java From teamcity-s3-artifact-storage-plugin with Apache License 2.0 | 6 votes |
@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 = getUrlResolver(httpMethod, bucketName, objectKey, params); 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 Throwable cause = e.getCause(); final AWSException awsException = cause != null ? new AWSException(cause) : new AWSException(e); final String details = awsException.getDetails(); if (StringUtil.isNotEmpty(details)) { final String message = awsException.getMessage() + details; LOG.warn(message); } throw new IOException(String.format( "Failed to create pre-signed URL to %s artifact '%s' in bucket '%s': %s", httpMethod.name().toLowerCase(), objectKey, bucketName, awsException.getMessage() ), awsException); } }
Example #6
Source File: S3PreSignedUrlProviderImpl.java From teamcity-s3-artifact-storage-plugin with Apache License 2.0 | 6 votes |
@NotNull private Callable<String> getUrlResolver(@NotNull final HttpMethod httpMethod, @NotNull final String bucketName, @NotNull final String objectKey, @NotNull final Map<String, String> params) { return () -> S3Util.withS3Client(ParamUtil.putSslValues(myServerPaths, params), client -> { final GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectKey, httpMethod) .withExpiration(new Date(System.currentTimeMillis() + getUrlLifetimeSec() * 1000)); return IOGuard.allowNetworkCall(() -> { try { return client.generatePresignedUrl(request).toString(); } catch (Throwable t) { if (t instanceof Exception) { throw (Exception)t; } else { throw new Exception(t); } } }); }); }
Example #7
Source File: AwsSdkTest.java From s3proxy with Apache License 2.0 | 6 votes |
@Test public void testAwsV4UrlSigning() throws Exception { String blobName = "foo"; ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(BYTE_SOURCE.size()); client.putObject(containerName, blobName, BYTE_SOURCE.openStream(), metadata); Date expiration = new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(1)); URL url = client.generatePresignedUrl(containerName, blobName, expiration, HttpMethod.GET); try (InputStream actual = url.openStream(); InputStream expected = BYTE_SOURCE.openStream()) { assertThat(actual).hasContentEqualTo(expected); } }
Example #8
Source File: AwsSdkTest.java From s3proxy with Apache License 2.0 | 6 votes |
@Test public void testAwsV2UrlSigning() 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); Date expiration = new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(1)); URL url = client.generatePresignedUrl(containerName, blobName, expiration, HttpMethod.GET); try (InputStream actual = url.openStream(); InputStream expected = BYTE_SOURCE.openStream()) { assertThat(actual).hasContentEqualTo(expected); } }
Example #9
Source File: AmazonS3Manager.java From carina with Apache License 2.0 | 5 votes |
/** * 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; }
Example #10
Source File: ContentHelper.java From aws-photosharing-example with Apache License 2.0 | 5 votes |
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); }
Example #11
Source File: Acl.java From cloudExplorer with GNU General Public License v3.0 | 5 votes |
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; }
Example #12
Source File: S3FileStoreTest.java From Cheddar with Apache License 2.0 | 5 votes |
@Test public void shouldGetURL() throws IOException { // Given final S3FileStore s3FileStore = new S3FileStore(bucketSchema); s3FileStore.initialize(mockAmazonS3Client); final int randomMillis = Randoms.randomInt(1000); DateTimeUtils.setCurrentMillisFixed(randomMillis); // When s3FileStore.publicUrlForFilePath(filePath); // Then verify(mockAmazonS3Client).generatePresignedUrl(bucketSchema + "-" + filePath.directory(), filePath.filename(), new Date(3600000 + randomMillis), HttpMethod.GET); }
Example #13
Source File: CrossOriginResourceSharingResponseTest.java From s3proxy with Apache License 2.0 | 5 votes |
@Before public void setUp() throws Exception { TestUtils.S3ProxyLaunchInfo info = TestUtils.startS3Proxy( "s3proxy-cors.conf"); awsCreds = new BasicAWSCredentials(info.getS3Identity(), info.getS3Credential()); context = info.getBlobStore().getContext(); s3Proxy = info.getS3Proxy(); s3Endpoint = info.getSecureEndpoint(); servicePath = info.getServicePath(); s3EndpointConfig = new EndpointConfiguration( s3Endpoint.toString() + servicePath, "us-east-1"); s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withEndpointConfiguration(s3EndpointConfig) .build(); httpClient = getHttpClient(); containerName = createRandomContainerName(); info.getBlobStore().createContainerInLocation(null, containerName); s3Client.setBucketAcl(containerName, CannedAccessControlList.PublicRead); String blobName = "test"; ByteSource payload = ByteSource.wrap("blob-content".getBytes( StandardCharsets.UTF_8)); Blob blob = info.getBlobStore().blobBuilder(blobName) .payload(payload).contentLength(payload.size()).build(); info.getBlobStore().putBlob(containerName, blob); Date expiration = new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(1)); presignedGET = s3Client.generatePresignedUrl(containerName, blobName, expiration, HttpMethod.GET).toURI(); publicGET = s3Client.getUrl(containerName, blobName).toURI(); }
Example #14
Source File: S3Storage.java From digdag with Apache License 2.0 | 5 votes |
@Override public Optional<DirectUploadHandle> getDirectUploadHandle(String key) { final long secondsToExpire = config.get("direct_upload_expiration", Long.class, 10L*60); GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucket, key); req.setMethod(HttpMethod.PUT); req.setExpiration(Date.from(Instant.now().plusSeconds(secondsToExpire))); String url = client.generatePresignedUrl(req).toString(); return Optional.of(DirectUploadHandle.of(url)); }
Example #15
Source File: S3DaoImpl.java From herd with Apache License 2.0 | 5 votes |
@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(); } }
Example #16
Source File: S3Service.java From modeldb with Apache License 2.0 | 5 votes |
@Override public String generatePresignedUrl(String s3Key, String method, long partNumber, String uploadId) throws ModelDBException { // Validate bucket Boolean exist = doesBucketExist(bucketName); if (!exist) { throw new ModelDBException("Bucket does not exists", io.grpc.Status.Code.INTERNAL); } HttpMethod reqMethod; if (method.equalsIgnoreCase(ModelDBConstants.PUT)) { reqMethod = HttpMethod.PUT; } else if (method.equalsIgnoreCase(ModelDBConstants.GET)) { reqMethod = HttpMethod.GET; } else { String errorMessage = "Unsupported HTTP Method for S3 Presigned URL"; Status status = Status.newBuilder().setCode(Code.NOT_FOUND_VALUE).setMessage(errorMessage).build(); LOGGER.info(errorMessage); throw StatusProto.toStatusRuntimeException(status); } // Set Expiration java.util.Date expiration = new java.util.Date(); long milliSeconds = expiration.getTime(); milliSeconds += 1000 * 60 * 5; // Add 5 mins expiration.setTime(milliSeconds); GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, s3Key) .withMethod(reqMethod) .withExpiration(expiration); if (partNumber != 0) { request.addRequestParameter("partNumber", String.valueOf(partNumber)); request.addRequestParameter("uploadId", uploadId); } return s3Client.generatePresignedUrl(request).toString(); }
Example #17
Source File: GeneratePresignedPutUrl.java From aws-doc-sdk-examples with Apache License 2.0 | 5 votes |
public static void main(String[] args) { final String USAGE = "\n" + "To run this example, supply the name of an S3 bucket and a file to\n" + "upload to it.\n" + "\n" + "Ex: GeneratePresignedPutUrl <bucketname> <filename>\n"; if (args.length < 2) { System.out.println(USAGE); System.exit(1); } String bucket_name = args[0]; String key_name = args[1]; System.out.format("Creating a pre-signed URL for uploading %s to S3 bucket %s...\n", key_name, bucket_name); final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build(); // Set the pre-signed URL to expire after 12 hours. java.util.Date expiration = new java.util.Date(); long expirationInMs = expiration.getTime(); expirationInMs += 1000 * 60 * 60 * 12; expiration.setTime(expirationInMs); try { GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket_name, key_name) .withMethod(HttpMethod.PUT) .withExpiration(expiration); URL url = s3.generatePresignedUrl(generatePresignedUrlRequest); //print URL System.out.println("\n\rGenerated URL: " + url.toString()); //Print curl command to consume URL System.out.println("\n\rExample command to use URL for file upload: \n\r"); System.out.println("curl --request PUT --upload-file /path/to/" + key_name + " '" + url.toString() + "' -# > /dev/null"); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } }
Example #18
Source File: S3UrlGenerator.java From hmftools with GNU General Public License v3.0 | 5 votes |
@NotNull URL generateUrl(@NotNull String bucketName, @NotNull String objectKey, int expirationHours) { try { LOGGER.info("Generating pre-signed URL for bucket: {}\tobject: {}\texpirationTime: {} hours", bucketName, objectKey, expirationHours); long millisNow = System.currentTimeMillis(); long expirationMillis = millisNow + 1000 * 60 * 60 * expirationHours; Date expiration = new java.util.Date(expirationMillis); 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 IllegalStateException("Failed to create URL based on the S3 input configurations."); }
Example #19
Source File: DummyS3Client.java From ignite with Apache License 2.0 | 4 votes |
/** Unsupported Operation. */ @Override public URL generatePresignedUrl(String bucketName, String key, Date expiration, HttpMethod mtd) throws SdkClientException { throw new UnsupportedOperationException("Operation not supported"); }
Example #20
Source File: MockAmazonS3.java From tajo with Apache License 2.0 | 4 votes |
@Override public URL generatePresignedUrl(String bucketName, String key, Date expiration, HttpMethod method) throws AmazonClientException { throw new TajoInternalError(new UnsupportedException()); }
Example #21
Source File: AwsSdkTest.java From s3proxy with Apache License 2.0 | 4 votes |
@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); } }
Example #22
Source File: S3FileStore.java From Cheddar with Apache License 2.0 | 4 votes |
@Override public URL publicUrlForFilePath(final FilePath filePath) throws NonExistentItemException { return amazonS3Client.generatePresignedUrl(bucketNameForFilePath(filePath), filePath.filename(), DateTime.now().plusHours(1).toDate(), HttpMethod.GET); }
Example #23
Source File: S3Utils.java From cloudstack with Apache License 2.0 | 4 votes |
public static URL generatePresignedUrl(final ClientOptions clientOptions, final String bucketName, final String key, final Date expiration) { LOGGER.debug(format("Generating presigned url for key %1s in bucket %2s with expiration date %3s", key, bucketName, expiration.toString())); return getTransferManager(clientOptions).getAmazonS3Client().generatePresignedUrl(bucketName, key, expiration, HttpMethod.GET); }
Example #24
Source File: S3PreSignedUrlProvider.java From teamcity-s3-artifact-storage-plugin with Apache License 2.0 | 4 votes |
@NotNull String getPreSignedUrl(@NotNull HttpMethod httpMethod, @NotNull String bucketName, @NotNull String objectKey, @NotNull Map<String, String> params) throws IOException;
Example #25
Source File: AmazonS3Mock.java From Scribengin with GNU Affero General Public License v3.0 | 4 votes |
@Override public URL generatePresignedUrl(String bucketName, String key, Date expiration, HttpMethod method) throws AmazonClientException { // TODO Auto-generated method stub return null; }
Example #26
Source File: S3PresignUrlStep.java From pipeline-aws-plugin with Apache License 2.0 | 4 votes |
public HttpMethod getHttpMethod() { return httpMethod; }