使用java SDK在S3 AWS上上载文件时无法设置元数据

使用java SDK在S3 AWS上上载文件时无法设置元数据,java,amazon-web-services,netbeans,Java,Amazon Web Services,Netbeans,我试图在S3 aws上上载文件时设置用户定义的元数据。 但它不起作用。。 以下是我代码的一部分: AmazonS3 s3 = new AmazonS3Client(credentials); Map<String, String> metaList = new HashMap<>(); metaList.put("x-amz-meta-example", "true"); ObjectMetadata medata =

我试图在S3 aws上上载文件时设置用户定义的元数据。 但它不起作用。。 以下是我代码的一部分:

      AmazonS3 s3 = new AmazonS3Client(credentials); 
      Map<String, String> metaList = new HashMap<>();
      metaList.put("x-amz-meta-example", "true"); 

      ObjectMetadata medata = new ObjectMetadata(); 
      medata.setUserMetadata(metaList); 

      String bucketName = "bucketName"; 
      String key = "objKey"; 
      File file = new File("example.txt"); 


      try{

             s3.putObject(bucketName, key, file)
             .setMetadata(medata);
             System.out.println("Successfully uplooaded...");


         }catch(Exception e){


           System.out.println(e.getMessage());
      }
AmazonS3 s3=新的AmazonS3客户端(凭证);
Map metaList=newhashmap();
metaList.put(“x-amz-meta-example”、“true”);
ObjectMetadata medata=新的ObjectMetadata();
setUserMetadata(metaList);
字符串bucketName=“bucketName”;
String key=“objKey”;
File File=新文件(“example.txt”);
试一试{
s3.putObject(bucketName、键、文件)
.setMetadata(medata);
System.out.println(“成功上传…”);
}捕获(例外e){
System.out.println(e.getMessage());
}

在AWS控制台中,文件上载成功。但是当我检查元数据时,我并没有使用我设置的元数据

在SDK中,它说不要设置“x-amz-meta-BLAH”

AmazonS3可以通过在内部将其表示为前缀为“x-amz-meta-”的HTTP头来在对象上存储额外的元数据。在AmazonS3中,使用用户元数据将任意元数据与其数据一起存储。设置用户元数据时,调用方不应包含内部“x-amz-meta-”前缀此库将为他们处理该问题。同样,调用方检索自定义用户元数据时,将不会看到“x-amz-meta-”头前缀

在上面的方法调用中,您将元数据设置为结果,而不是请求的元数据

这将有助于:

s3.putObject(bucketName, key, new FileInputStream(file),medata)  

这适用于aws sdk 1.11.x

ObjectMetadata metadata = new ObjectMetadata();
metadata.setCacheControl("max-age=2592000, must-revalidate");

PutObjectResult s3putObj = s3client.putObject(new PutObjectRequest(s3bucket,
                    filePath,
                    filetoupload)
                    .withMetadata(metadata)//set metadata
                    .withCannedAcl(CannedAccessControlList.PublicRead));

即使去掉前缀“x-amz-meta”,它也不起作用。。。有什么我做错了吗?
ObjectMetadata metadata = new ObjectMetadata();
metadata.setCacheControl("max-age=2592000, must-revalidate");

PutObjectResult s3putObj = s3client.putObject(new PutObjectRequest(s3bucket,
                    filePath,
                    filetoupload)
                    .withMetadata(metadata)//set metadata
                    .withCannedAcl(CannedAccessControlList.PublicRead));