Search in sources :

Example 11 with HttpRequest

use of software.amazon.awssdk.crt.http.HttpRequest in project aws-crt-java by awslabs.

the class S3ClientTest method testS3OverrideRequestCredentials.

@Test
public void testS3OverrideRequestCredentials() {
    skipIfNetworkUnavailable();
    Log.initLoggingToFile(Log.LogLevel.Error, "log.txt");
    Assume.assumeTrue(hasAwsCredentials());
    S3ClientOptions clientOptions = new S3ClientOptions().withEndpoint(ENDPOINT).withRegion(REGION);
    boolean expectedException = false;
    byte[] madeUpCredentials = "I am a madeup credentials".getBytes();
    StaticCredentialsProvider.StaticCredentialsProviderBuilder builder = new StaticCredentialsProvider.StaticCredentialsProviderBuilder().withAccessKeyId(madeUpCredentials).withSecretAccessKey(madeUpCredentials);
    try (S3Client client = createS3Client(clientOptions);
        CredentialsProvider emptyCredentialsProvider = builder.build()) {
        CompletableFuture<Integer> onFinishedFuture = new CompletableFuture<>();
        S3MetaRequestResponseHandler responseHandler = new S3MetaRequestResponseHandler() {

            @Override
            public void onFinished(int errorCode, int responseStatus, byte[] errorPayload) {
                Log.log(Log.LogLevel.Info, Log.LogSubject.JavaCrtS3, "Meta request finished with error code " + errorCode);
                if (errorCode != 0) {
                    onFinishedFuture.completeExceptionally(new CrtS3RuntimeException(errorCode, responseStatus, errorPayload));
                    return;
                }
                onFinishedFuture.complete(Integer.valueOf(errorCode));
            }
        };
        HttpHeader[] headers = { new HttpHeader("Host", ENDPOINT) };
        HttpRequest httpRequest = new HttpRequest("GET", "/get_object_test_1MB.txt", headers, null);
        S3MetaRequestOptions metaRequestOptions = new S3MetaRequestOptions().withMetaRequestType(MetaRequestType.GET_OBJECT).withHttpRequest(httpRequest).withResponseHandler(responseHandler).withCredentialsProvider(emptyCredentialsProvider);
        try (S3MetaRequest metaRequest = client.makeMetaRequest(metaRequestOptions)) {
            Assert.assertEquals(Integer.valueOf(0), onFinishedFuture.get());
        }
    } catch (InterruptedException | ExecutionException ex) {
        expectedException = true;
        /*
             * Maybe better to have a cause of the max retries exceed to be more informative
             */
        if (!(ex.getCause() instanceof CrtS3RuntimeException)) {
            Assert.fail(ex.getMessage());
        }
    }
    Assert.assertTrue(expectedException);
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) StaticCredentialsProvider(software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider) CredentialsProvider(software.amazon.awssdk.crt.auth.credentials.CredentialsProvider) StaticCredentialsProvider(software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider) DefaultChainCredentialsProvider(software.amazon.awssdk.crt.auth.credentials.DefaultChainCredentialsProvider) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) Test(org.junit.Test)

Example 12 with HttpRequest

use of software.amazon.awssdk.crt.http.HttpRequest in project aws-crt-java by awslabs.

the class S3ClientTest method testS3Get.

@Test
public void testS3Get() {
    skipIfNetworkUnavailable();
    Assume.assumeTrue(hasAwsCredentials());
    S3ClientOptions clientOptions = new S3ClientOptions().withEndpoint(ENDPOINT).withRegion(REGION);
    try (S3Client client = createS3Client(clientOptions)) {
        CompletableFuture<Integer> onFinishedFuture = new CompletableFuture<>();
        S3MetaRequestResponseHandler responseHandler = new S3MetaRequestResponseHandler() {

            @Override
            public int onResponseBody(ByteBuffer bodyBytesIn, long objectRangeStart, long objectRangeEnd) {
                byte[] bytes = new byte[bodyBytesIn.remaining()];
                bodyBytesIn.get(bytes);
                Log.log(Log.LogLevel.Info, Log.LogSubject.JavaCrtS3, "Body Response: " + Arrays.toString(bytes));
                return 0;
            }

            @Override
            public void onFinished(int errorCode, int responseStatus, byte[] errorPayload) {
                Log.log(Log.LogLevel.Info, Log.LogSubject.JavaCrtS3, "Meta request finished with error code " + errorCode);
                if (errorCode != 0) {
                    onFinishedFuture.completeExceptionally(new CrtS3RuntimeException(errorCode, responseStatus, errorPayload));
                    return;
                }
                onFinishedFuture.complete(Integer.valueOf(errorCode));
            }
        };
        HttpHeader[] headers = { new HttpHeader("Host", ENDPOINT) };
        HttpRequest httpRequest = new HttpRequest("GET", "/get_object_test_1MB.txt", headers, null);
        S3MetaRequestOptions metaRequestOptions = new S3MetaRequestOptions().withMetaRequestType(MetaRequestType.GET_OBJECT).withHttpRequest(httpRequest).withResponseHandler(responseHandler);
        try (S3MetaRequest metaRequest = client.makeMetaRequest(metaRequestOptions)) {
            Assert.assertEquals(Integer.valueOf(0), onFinishedFuture.get());
        }
    } catch (InterruptedException | ExecutionException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) ByteBuffer(java.nio.ByteBuffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) Test(org.junit.Test)

Example 13 with HttpRequest

use of software.amazon.awssdk.crt.http.HttpRequest in project aws-crt-java by awslabs.

the class S3ClientTest method benchmarkS3Put.

@Test
public void benchmarkS3Put() {
    skipIfNetworkUnavailable();
    Assume.assumeTrue(hasAwsCredentials());
    Assume.assumeNotNull(System.getProperty("aws.crt.s3.benchmark"));
    // Override defaults with values from system properties, via -D on mvn
    // commandline
    final int threadCount = Integer.parseInt(System.getProperty("aws.crt.s3.benchmark.threads", "0"));
    final String region = System.getProperty("aws.crt.s3.benchmark.region", "us-west-2");
    final String bucket = System.getProperty("aws.crt.s3.benchmark.bucket", (region == "us-west-2") ? "aws-crt-canary-bucket" : String.format("aws-crt-canary-bucket-%s", region));
    final String endpoint = System.getProperty("aws.crt.s3.benchmark.endpoint", String.format("%s.s3.%s.amazonaws.com", bucket, region));
    final boolean useTls = Boolean.parseBoolean(System.getProperty("aws.crt.s3.benchmark.tls", "false"));
    final double expectedGbps = Double.parseDouble(System.getProperty("aws.crt.s3.benchmark.gbps", "10"));
    final int numTransfers = Integer.parseInt(System.getProperty("aws.crt.s3.benchmark.transfers", "16"));
    final int concurrentTransfers = Integer.parseInt(System.getProperty("aws.crt.s3.benchmark.concurrent", "16"));
    /* should be 1.6 * expectedGbps */
    // avg of .3Gbps per connection, 32 connections per vip, 5 seconds per vip
    // resolution
    final int vipsNeeded = (int) Math.ceil(expectedGbps / 0.5 / 10);
    final int sampleDelay = Integer.parseInt(System.getProperty("aws.crt.s3.benchmark.warmup", new Integer((int) Math.ceil(vipsNeeded / 5)).toString()));
    System.out.println(String.format("REGION=%s, WARMUP=%s", region, sampleDelay));
    // Ignore stats during warm up time, they skew results
    TransferStats.global.withSampleDelay(Duration.ofSeconds(sampleDelay));
    try (TlsContext tlsCtx = createTlsContextOptions(getContext().trustStore)) {
        S3ClientOptions clientOptions = new S3ClientOptions().withRegion(region).withEndpoint(endpoint).withThroughputTargetGbps(expectedGbps).withTlsContext(useTls ? tlsCtx : null);
        try (S3Client client = createS3Client(clientOptions, threadCount)) {
            List<CompletableFuture<TransferStats>> requestFutures = new LinkedList<>();
            // Each meta request will acquire a slot, and release it when it's done
            Semaphore concurrentSlots = new Semaphore(concurrentTransfers);
            for (int transferIdx = 0; transferIdx < numTransfers; ++transferIdx) {
                try {
                    concurrentSlots.acquire();
                } catch (InterruptedException ex) {
                    Assert.fail(ex.toString());
                }
                final int myIdx = transferIdx;
                CompletableFuture<TransferStats> onFinishedFuture = new CompletableFuture<>();
                requestFutures.add(onFinishedFuture);
                S3MetaRequestResponseHandler responseHandler = new S3MetaRequestResponseHandler() {

                    TransferStats stats = new TransferStats();

                    @Override
                    public void onFinished(int errorCode, int responseStatus, byte[] errorPayload) {
                        // release the slot first
                        concurrentSlots.release();
                        if (errorCode != 0) {
                            onFinishedFuture.completeExceptionally(new CrtS3RuntimeException(errorCode, responseStatus, errorPayload));
                            return;
                        }
                        synchronized (System.out) {
                            System.out.println(String.format("Transfer %d finished.", myIdx + 1));
                        }
                        onFinishedFuture.complete(stats);
                    }
                };
                final long payloadSize = 5L * 1024L * 1024L * 1024L;
                final String payloadString = "This is an S3 Test.  This is an S3 Test.  This is an S3 Test.  This is an S3 Test.";
                HttpRequestBodyStream payloadStream = new HttpRequestBodyStream() {

                    private long remainingBody = payloadSize;

                    @Override
                    public boolean sendRequestBody(ByteBuffer outBuffer) {
                        byte[] payloadBytes = null;
                        try {
                            payloadBytes = payloadString.getBytes("ASCII");
                        } catch (Exception ex) {
                            System.out.println("Encountered error trying to get payload bytes.");
                            return true;
                        }
                        while (remainingBody > 0 && outBuffer.remaining() > 0) {
                            long amtToTransfer = Math.min(remainingBody, (long) outBuffer.remaining());
                            amtToTransfer = Math.min(amtToTransfer, (long) payloadBytes.length);
                            // Transfer the data
                            outBuffer.put(payloadBytes, 0, (int) amtToTransfer);
                            remainingBody -= amtToTransfer;
                        }
                        return remainingBody == 0;
                    }

                    @Override
                    public boolean resetPosition() {
                        return true;
                    }

                    @Override
                    public long getLength() {
                        return payloadSize;
                    }
                };
                HttpHeader[] headers = { new HttpHeader("Host", endpoint), new HttpHeader("Content-Length", Long.valueOf(payloadSize).toString()) };
                HttpRequest httpRequest = new HttpRequest("PUT", String.format("/put_object_test_5GB_%d.txt", myIdx + 1), headers, payloadStream);
                S3MetaRequestOptions metaRequestOptions = new S3MetaRequestOptions().withMetaRequestType(MetaRequestType.PUT_OBJECT).withHttpRequest(httpRequest).withResponseHandler(responseHandler);
                try (S3MetaRequest metaRequest = client.makeMetaRequest(metaRequestOptions)) {
                }
            }
            // Finish each future, and deduct failures from completedTransfers
            int completedTransfers = numTransfers;
            for (CompletableFuture<TransferStats> request : requestFutures) {
                try {
                    request.join();
                } catch (CompletionException ex) {
                    System.out.println(ex.toString());
                    Throwable cause = ex.getCause();
                    if (cause != ex && cause != null) {
                        System.out.println(cause.toString());
                    }
                    cause = cause.getCause();
                    if (cause != null && cause != ex) {
                        System.out.println(cause.toString());
                    }
                    --completedTransfers;
                }
            }
        }
    }
}
Also used : HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) HttpRequestBodyStream(software.amazon.awssdk.crt.http.HttpRequestBodyStream) ByteBuffer(java.nio.ByteBuffer) LinkedList(java.util.LinkedList) BufferOverflowException(java.nio.BufferOverflowException) FileNotFoundException(java.io.FileNotFoundException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 14 with HttpRequest

use of software.amazon.awssdk.crt.http.HttpRequest in project aws-crt-java by awslabs.

the class HttpClientConnectionManagerTest method createHttpRequest.

private HttpRequest createHttpRequest(String method, String endpoint, String path, String requestBody) throws Exception {
    URI uri = new URI(endpoint);
    HttpHeader[] requestHeaders = new HttpHeader[] { new HttpHeader("Host", uri.getHost()), new HttpHeader("Content-Length", Integer.toString(requestBody.getBytes(UTF8).length)) };
    HttpRequest request = new HttpRequest(method, path, requestHeaders, null);
    return request;
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) URI(java.net.URI)

Example 15 with HttpRequest

use of software.amazon.awssdk.crt.http.HttpRequest in project aws-crt-java by awslabs.

the class HttpRequestResponseTest method testMarshallJniUtf8Path.

@Test
public void testMarshallJniUtf8Path() throws Exception {
    HttpRequest request = new HttpRequest("GET", "/?ሴ=bar");
    request.marshalForJni();
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) Test(org.junit.Test)

Aggregations

HttpRequest (software.amazon.awssdk.crt.http.HttpRequest)47 HttpHeader (software.amazon.awssdk.crt.http.HttpHeader)26 Test (org.junit.Test)22 URI (java.net.URI)15 ByteBuffer (java.nio.ByteBuffer)13 HttpRequestBodyStream (software.amazon.awssdk.crt.http.HttpRequestBodyStream)13 AwsSigningConfig (software.amazon.awssdk.crt.auth.signing.AwsSigningConfig)11 CompletableFuture (java.util.concurrent.CompletableFuture)8 StaticCredentialsProvider (software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider)8 SdkHttpRequest (software.amazon.awssdk.http.SdkHttpRequest)8 List (java.util.List)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 ArrayList (java.util.ArrayList)6 AwsSigningResult (software.amazon.awssdk.crt.auth.signing.AwsSigningResult)6 AsyncExecuteRequest (software.amazon.awssdk.http.async.AsyncExecuteRequest)6 S3MetaRequestOptions (software.amazon.awssdk.crt.s3.S3MetaRequestOptions)5 Optional (java.util.Optional)4 S3MetaRequest (software.amazon.awssdk.crt.s3.S3MetaRequest)4 StandardCharsets (java.nio.charset.StandardCharsets)3 Arrays (java.util.Arrays)3