如何设置通过SDK一个S3对象的内容类型? [英] How to set the content type of an S3 object via the SDK?
问题描述
我试图使用AWS API来设置多个对象的内容类型,并添加一个内容编码:gzip头给他们。这是我的code做的:
I'm trying to use AWS Api to set the content type of multiple objects and to add a 'content-encoding: gzip' header to them. Here's my code for doing that:
for (S3ObjectSummary summary : objs.getObjectSummaries() )
{
String key = summary.getKey();
if (! key.endsWith(".gz"))
continue;
ObjectMetadata metadata = new ObjectMetadata();
metadata.addUserMetadata("Content-Encoding", "gzip");
metadata.addUserMetadata("Content-Type", "application/x-gzip");
final CopyObjectRequest request = new CopyObjectRequest(bucket, key, bucket, key)
.withSourceBucketName( bucket )
.withSourceKey(key)
.withNewObjectMetadata(metadata);
s3.copyObject(request);
}
当我运行这个然而,以下是结果:
When I run this however, the following is the result:
正如你所看到的,preFIX X-AMZ-元
添加到我的自定义页眉和他们是低套管。而内容类型
头被忽略,而是把 WWW /形式恩codeD
作为标题
As you can see, the prefix x-amz-meta
was added to my custom headers, and they were lower cased. And the content-type
header was ignored, instead it put www/form-encoded
as the header.
我能做到这一点,以使其接受我的头值?
What can I do it to cause it to accept my header values?
推荐答案
发现问题。 ObjectMetadata code>要求要设置的内容类型/编码明确,而不是通过
addUserMetadata()
。更改如下:
Found the problem. ObjectMetadata
requires the content-type / encoding to be set explicitly rather than via addUserMetadata()
. Changing the following:
metadata.addUserMetadata("Content-Encoding", "gzip");
metadata.addUserMetadata("Content-Type", "application/x-gzip");
到
metadata.setContentEncoding("gzip");
metadata.setContentType("application/x-gzip");
修正了这个。
fixed this.
这篇关于如何设置通过SDK一个S3对象的内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!