gpt4 book ai didi

java - AWS SDK `PutObjectResult` 元数据是否没有保存内容长度值?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:11:28 25 4
gpt4 key购买 nike

我成功地将对象存储到 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

这是一个错误,我做错了什么,还是预期的行为?

最佳答案

我认为请求和响应的内容长度是不同的。

在 DEBUG 模式下运行应用程序,您将看到以下结果,

将以下 sbt log4j 依赖项放入 build.sbt 并在 src/main/resources/log4j.properties 中记录配置,

libraryDependencies += "log4j" % "log4j" % "1.2.17"

日志配置

# 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

然后根据你的要求你可以清楚地看到content-length是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 等的以下结果。由于它除了 header 信息之外没有其他内容,因此内容长度必须为 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

关于java - AWS SDK `PutObjectResult` 元数据是否没有保存内容长度值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44291787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com