Java Code Examples for com.amazonaws.services.s3.model.ResponseHeaderOverrides#setContentType()

The following examples show how to use com.amazonaws.services.s3.model.ResponseHeaderOverrides#setContentType() . 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);
    }
}