我成功将对象存储到S3。由于PutObjectRequest
,它会返回PutObjectResult
,其中包含getMetadata()
方法。但是,它返回的元数据保留了一些值,并且错过了我希望存在的其他值。特别是它会错过内容长度值,但会明确指定给PutObjectRequest
。
例如,此示例代码:
val client: AmazonS3 = ???
val contentBytes = "Test content".getBytes
val inputStream: InputStream = new ByteArrayInputStream(contentBytes)
val metadata = new ObjectMetadata
metadata.setContentLength(contentBytes.length)
val putRequest = new PutObjectRequest("bucketName", "key", inputStream, metadata)
.withStorageClass(StorageClass.valueOf("ReducedRedundancy"))
val putObjectResult = client.putObject(putRequest)
println(s"Storage class: ${putObjectResult.getMetadata.getStorageClass}; Length: ${putObjectResult.getMetadata.getContentLength}")
将返回以下内容:存储类:REDUCED_REDUNDANCY;长度:0
这是一个错误,我做错了什么,还是预期的行为?
答案 0 :(得分:2)
我认为请求和响应的内容长度是不同的。
在DEBUG模式下运行应用程序,您将看到以下结果,
在build.sbt
中添加以下sbt log4j依赖项并在src/main/resources/log4j.properties
中记录配置,
libraryDependencies += "log4j" % "log4j" % "1.2.17"
log config
# Root logger option
log4j.rootLogger=DEBUG, file, stdout
## Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
然后根据您的要求,您可以清楚地看到内容长度为12,
2017-05-31 15:16:40 DEBUG request:1137 - Sending Request: PUT https://samsa-repo.s3.amazonaws.com /test-bytes Headers: (User-Agent: aws-sdk-java/1.11.109 Mac_OS_X/10.11.6 Java_HotSpot(TM)_64-Bit_Server_VM/25.111-b14/1.8.0_111 scala/2.11.8, amz-sdk-invocation-id: ad0dec57-4d52-9855-4d4b-b802306cd610, Content-Length: 12, x-amz-storage-class: REDUCED_REDUNDANCY, Content-Type: application/octet-stream, )
2017-05-31 15:16:40 DEBUG AWS4Signer:33 - AWS4 Canonical Request: '"PUT
/test-bytes
amz-sdk-invocation-id:ad0dec57-4d52-9855-4d4b-b802306cd610
amz-sdk-retry:0/0/500
content-length:12
content-type:application/octet-stream
host:samsa-repo.s3.amazonaws.com
user-agent:aws-sdk-java/1.11.109 Mac_OS_X/10.11.6 Java_HotSpot(TM)_64-Bit_Server_VM/25.111-b14/1.8.0_111 scala/2.11.8
x-amz-content-sha256:UNSIGNED-PAYLOAD
x-amz-date:20170531T221640Z
x-amz-security-token:"i am hiding it"
x-amz-storage-class:REDUCED_REDUNDANCY
在回复时,您会看到以下带有http响应代码和etag等的结果。由于除了标题信息之外没有其他内容,因此内容长度必须为0
。
2017-05-31 15:16:40 DEBUG wire:72 - http-outgoing-0 << "HTTP/1.1 200 OK[\r][\n]"
2017-05-31 15:16:40 DEBUG wire:72 - http-outgoing-0 << "x-amz-id-2: 3DpsdWzL97tHmVBq4xfuqQtHBzaeGjhcsTBmqc8cZWjcZYGWkjnGOgrYaqfTqZo5UygLd3tLEPE=[\r][\n]"
2017-05-31 15:16:40 DEBUG wire:72 - http-outgoing-0 << "x-amz-request-id: 47FCD67C6CBAC015[\r][\n]"
2017-05-31 15:16:40 DEBUG wire:72 - http-outgoing-0 << "Date: Wed, 31 May 2017 22:16:41 GMT[\r][\n]"
2017-05-31 15:16:40 DEBUG wire:72 - http-outgoing-0 << "ETag: "8bfa8e0684108f419933a5995264d150"[\r][\n]"
2017-05-31 15:16:40 DEBUG wire:72 - http-outgoing-0 << "x-amz-storage-class: REDUCED_REDUNDANCY[\r][\n]"
2017-05-31 15:16:40 DEBUG wire:72 - http-outgoing-0 << "Content-Length: 0[\r][\n]"
2017-05-31 15:16:40 DEBUG wire:72 - http-outgoing-0 << "Server: AmazonS3[\r][\n]"
2017-05-31 15:16:40 DEBUG wire:72 - http-outgoing-0 << "[\r][\n]"
2017-05-31 15:16:40 DEBUG headers:124 - http-outgoing-0 << HTTP/1.1 200 OK
2017-05-31 15:16:40 DEBUG headers:127 - http-outgoing-0 << x-amz-id-2: 3DpsdWzL97tHmVBq4xfuqQtHBzaeGjhcsTBmqc8cZWjcZYGWkjnGOgrYaqfTqZo5UygLd3tLEPE=
2017-05-31 15:16:40 DEBUG headers:127 - http-outgoing-0 << x-amz-request-id: 47FCD67C6CBAC015
2017-05-31 15:16:40 DEBUG headers:127 - http-outgoing-0 << Date: Wed, 31 May 2017 22:16:41 GMT
2017-05-31 15:16:40 DEBUG headers:127 - http-outgoing-0 << ETag: "8bfa8e0684108f419933a5995264d150"
2017-05-31 15:16:40 DEBUG headers:127 - http-outgoing-0 << x-amz-storage-class: REDUCED_REDUNDANCY
2017-05-31 15:16:40 DEBUG headers:127 - http-outgoing-0 << Content-Length: 0
2017-05-31 15:16:40 DEBUG headers:127 - http-outgoing-0 << Server: AmazonS3
参见示例1:上传对象http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html