com.amazonaws.services.s3.model.ResponseHeaderOverrides Java Examples

The following examples show how to use com.amazonaws.services.s3.model.ResponseHeaderOverrides. 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: AwsSdkTest.java    From s3proxy with Apache License 2.0 5 votes vote down vote up
@Test
public void testAwsV2SignatureWithOverrideParameters() throws Exception {
    client = AmazonS3ClientBuilder.standard()
            .withClientConfiguration(V2_SIGNER_CONFIG)
            .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
            .withEndpointConfiguration(s3EndpointConfig).build();

    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(BYTE_SOURCE.size());
    client.putObject(containerName, "foo", BYTE_SOURCE.openStream(),
            metadata);

    String blobName = "foo";

    ResponseHeaderOverrides headerOverride = new ResponseHeaderOverrides();

    String expectedContentDisposition = "attachment; " + blobName;
    headerOverride.setContentDisposition(expectedContentDisposition);

    String expectedContentType = "text/plain";
    headerOverride.setContentType(expectedContentType);

    GetObjectRequest request = new GetObjectRequest(containerName,
            blobName);
    request.setResponseHeaders(headerOverride);

    S3Object object = client.getObject(request);
    assertThat(object.getObjectMetadata().getContentLength()).isEqualTo(
            BYTE_SOURCE.size());
    assertThat(object.getObjectMetadata().getContentDisposition())
            .isEqualTo(expectedContentDisposition);
    assertThat(object.getObjectMetadata().getContentType()).isEqualTo(
            expectedContentType);
    try (InputStream actual = object.getObjectContent();
         InputStream expected = BYTE_SOURCE.openStream()) {
        assertThat(actual).hasContentEqualTo(expected);
    }
}
 
Example #2
Source File: AwsSdkTest.java    From s3proxy with Apache License 2.0 4 votes vote down vote up
@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 #3
Source File: AwsSdkTest.java    From s3proxy with Apache License 2.0 4 votes vote down vote up
@Test
public void testOverrideResponseHeader() throws Exception {
    String blobName = "foo";
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(BYTE_SOURCE.size());
    client.putObject(containerName, blobName, BYTE_SOURCE.openStream(),
            metadata);

    String cacheControl = "no-cache";
    String contentDisposition = "attachment; filename=foo.html";
    String contentEncoding = "gzip";
    String contentLanguage = "en";
    String contentType = "text/html; charset=UTF-8";
    String expires = "Wed, 13 Jul 2016 21:23:51 GMT";
    long expiresTime = 1468445031000L;

    GetObjectRequest getObjectRequest = new GetObjectRequest(containerName,
            blobName);
    getObjectRequest.setResponseHeaders(
            new ResponseHeaderOverrides()
                .withCacheControl(cacheControl)
                .withContentDisposition(contentDisposition)
                .withContentEncoding(contentEncoding)
                .withContentLanguage(contentLanguage)
                .withContentType(contentType)
                .withExpires(expires));
    S3Object object = client.getObject(getObjectRequest);
    try (InputStream is = object.getObjectContent()) {
        assertThat(is).isNotNull();
        ByteStreams.copy(is, ByteStreams.nullOutputStream());
    }

    ObjectMetadata reponseMetadata = object.getObjectMetadata();
    assertThat(reponseMetadata.getCacheControl()).isEqualTo(
            cacheControl);
    assertThat(reponseMetadata.getContentDisposition()).isEqualTo(
            contentDisposition);
    assertThat(reponseMetadata.getContentEncoding()).isEqualTo(
            contentEncoding);
    assertThat(reponseMetadata.getContentLanguage()).isEqualTo(
            contentLanguage);
    assertThat(reponseMetadata.getContentType()).isEqualTo(
            contentType);
    assertThat(reponseMetadata.getHttpExpiresDate().getTime())
        .isEqualTo(expiresTime);
}