aws s3响应头

时间:2017-09-18 18:52:28

标签: java amazon-web-services

我正在尝试在ObjectMetadata类中创建自定义标头,并为其提供以下代码

private String cust_header;
public String getcust_header() {
    return cust_header;
}

public void setcust_header(String cust_header) {
    cust_header = cust_header;
}

这是构造函数的外观:

private ObjectMetadata(ObjectMetadata from) {
    this.userMetadata = from.userMetadata == null
        ? null
        : new TreeMap<String,String>(from.userMetadata);
    // shallow clone the meata data
    this.metadata = from.metadata == null
        ? null
        : new TreeMap<String, Object>(from.metadata);
    this.expirationTime = cloneDate(from.expirationTime);
    this.expirationTimeRuleId = from.expirationTimeRuleId;
    this.httpExpiresDate = cloneDate(from.httpExpiresDate);
    this.ongoingRestore = from.ongoingRestore;
    this.restoreExpirationTime = cloneDate(from.restoreExpirationTime);
    this.cust_header = "custom header";
}

我正在尝试将文件上传到s3实例并获取响应标头。以下是我的代码:

AmazonS3 conn = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials))
            .withRegion(Regions.US_WEST_2).build();
    Bucket bucket;
    if (conn.doesBucketExist("xxx")) {
        System.out.println("Bucket already exists");

    }
    bucket = conn.createBucket("xxx");

    ByteArrayInputStream input = new ByteArrayInputStream("Hello World!".getBytes());
    ObjectMetadata objectMetadata = new ObjectMetadata();
    PutObjectRequest request = new PutObjectRequest(bucket.getName(), "hello.txt", input, objectMetadata);

    PutObjectResult response = conn.putObject(request);

    S3Object object = conn.getObject(new GetObjectRequest(bucket.getName(), "hello.txt"));
    InputStream objectData = object.getObjectContent();
    System.out.println("Content-Type: " + object.getObjectMetadata().getContentType() + " custom Header : "
            + object.getObjectMetadata().getcust_header());

我正在尝试打印所有标题。 但是,我在尝试打印所有标题时获取空值。有人可以解释我哪里出错吗?

来源AWS Documentation

1 个答案:

答案 0 :(得分:0)

我已经找到了一个工作正常的解决方案。与HTTP标头不同,必须设置响应将读取的自定义标头。然后我们可以打印如下:

Date expirationTime = new Date();
response.setETag("123");
response.setExpirationTime(expirationTime);
System.out.println("ET Flag : " + response.getETag() + " " + "Expiration Time" + response.getExpirationTime() + " ");

首先,我们必须在ObjectMetadata中创建自定义标头 我们必须使用PutObjectRequest中提供的方法,或者为自定义对象创建自定义getter和setter。