com.amazonaws.services.s3.model.ResponseHeaderOverrides#setContentType ( )源码实例Demo

下面列出了com.amazonaws.services.s3.model.ResponseHeaderOverrides#setContentType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: s3proxy   文件: AwsSdkTest.java
@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);
    }
}
 
源代码2 项目: s3proxy   文件: AwsSdkTest.java
@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);
    }
}