java – 为什么上传到S3的文件具有内容类型application / octet-stream,除非我命名文件.html

前端之家收集整理的这篇文章主要介绍了java – 为什么上传到S3的文件具有内容类型application / octet-stream,除非我命名文件.html前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
即使我将内容类型设置为text / html,它在S3上最终作为应用程序/八位字节流.
  1. ByteArrayInputStream contentsAsStream = new ByteArrayInputStream(contentAsBytes);
  2. ObjectMetadata md = new ObjectMetadata();
  3. md.setContentLength(contentAsBytes.length);
  4. md.setContentType("text/html");
  5. s3.putObject(new PutObjectRequest(ARTIST_BUCKET_NAME,artistId,contentsAsStream,md));

然而,如果我命名该文件,以最终为.html

  1. s3.putObject(new PutObjectRequest(ARTIST_BUCKET_NAME,artistId + ".html",md));

那么它工作.

我的md对象是否被忽略?如何随着时间的推移,我需要上传数千个文件,所以不能进入S3 UI并手动修复contentType.

解决方法

您必须在代码中执行其他操作.我刚刚尝试使用1.9.6 S3 SDK的代码示例,该文件获取“text / html”内容类型.

以下是确切的(Groovy)代码

  1. class S3Test {
  2. static void main(String[] args) {
  3.  
  4. def s3 = new AmazonS3Client()
  5.  
  6. def random = new Random()
  7. def bucketName = "raniz-playground"
  8. def keyName = "content-type-test"
  9.  
  10. byte[] contentAsBytes = new byte[1024]
  11. random.nextBytes(contentAsBytes)
  12.  
  13. ByteArrayInputStream contentsAsStream = new ByteArrayInputStream(contentAsBytes);
  14. ObjectMetadata md = new ObjectMetadata();
  15. md.setContentLength(contentAsBytes.length);
  16. md.setContentType("text/html");
  17. s3.putObject(new PutObjectRequest(bucketName,keyName,md))
  18.  
  19. def object = s3.getObject(bucketName,keyName)
  20. println(object.objectMetadata.contentType)
  21. object.close()
  22. }
  23. }

程序打印

text/html

而S3元数据也是一样的:

以下是通过网络发送的通信(由Apache HTTP Commons调试日志提供):

  1. >> PUT /content-type-test HTTP/1.1
  2. >> Host: raniz-playground.s3.amazonaws.com
  3. >> Authorization: AWS <nope>
  4. >> User-Agent: aws-sdk-java/1.9.6 Linux/3.2.0-84-generic Java_HotSpot(TM)_64-Bit_Server_VM/25.45-b02/1.8.0_45
  5. >> Date: Fri,12 Jun 2015 02:11:16 GMT
  6. >> Content-Type: text/html
  7. >> Content-Length: 1024
  8. >> Connection: Keep-Alive
  9. >> Expect: 100-continue
  10. << HTTP/1.1 200 OK
  11. << x-amz-id-2: mOsmhYGkW+SxipF6S2+CnmiqOhwJ62WfWUkmZk4zU3rzkWCEH9P/bT1hUz27apmO
  12. << x-amz-request-id: 8706AE3BE8597644
  13. << Date: Fri,12 Jun 2015 02:11:23 GMT
  14. << ETag: "6c53debeb28f1d12f7ad388b27c9036d"
  15. << Content-Length: 0
  16. << Server: AmazonS3
  17.  
  18. >> GET /content-type-test HTTP/1.1
  19. >> Host: raniz-playground.s3.amazonaws.com
  20. >> Authorization: AWS <nope>
  21. >> User-Agent: aws-sdk-java/1.9.6 Linux/3.2.0-84-generic Java_HotSpot(TM)_64-Bit_Server_VM/25.45-b02/1.8.0_45
  22. >> Date: Fri,12 Jun 2015 02:11:23 GMT
  23. >> Content-Type: application/x-www-form-urlencoded; charset=utf-8
  24. >> Connection: Keep-Alive
  25. << HTTP/1.1 200 OK
  26. << x-amz-id-2: 9U1CQ8yIYBKYyadKi4syaAsr+7BV76Q+5UAGj2w1zDiPC2qZN0NzUCQNv6pWGu7n
  27. << x-amz-request-id: 6777433366DB6436
  28. << Date: Fri,12 Jun 2015 02:11:24 GMT
  29. << Last-Modified: Fri,12 Jun 2015 02:11:23 GMT
  30. << ETag: "6c53debeb28f1d12f7ad388b27c9036d"
  31. << Accept-Ranges: bytes
  32. << Content-Type: text/html
  33. << Content-Length: 1024
  34. << Server: AmazonS3

这也是source code显示我们的行为 – 如果您设置内容类型,SDK将不会覆盖它.

猜你在找的Java相关文章