我们正在尝试使用 TransferManager 从 Amazon S3 下载文件。但是 TransferManager 在 doDownload 函数中多次使用 getObjectMetadata 服务。并通过从我提供的初始请求中复制普通成员来调用这些服务来创建请求。
问题是我添加了一些自定义标头,包括授权标头。getObjectMetadata 需要该标头。所以我必须修改一些 sdk 类。
AmazonS3Client.java
protected <X extends AmazonWebServiceRequest> Request<X> createRequest(String bucketName, String key, X originalRequest, HttpMethodName httpMethod, URI endpoint) {
// If the underlying AmazonS3Client has enabled accelerate mode and the original
// request operation is accelerate mode supported, then the request will use the
// s3-accelerate endpoint to performe the operations.
if (clientOptions.isAccelerateModeEnabled() && !(originalRequest instanceof S3AccelerateUnsupported)) {
if (clientOptions.isDualstackEnabled()) {
endpoint = RuntimeHttpUtils.toUri(Constants.S3_ACCELERATE_DUALSTACK_HOSTNAME, clientConfiguration);
} else {
endpoint = RuntimeHttpUtils.toUri(Constants.S3_ACCELERATE_HOSTNAME, clientConfiguration);
}
}
Request<X> request = new DefaultRequest<X>(originalRequest, Constants.S3_SERVICE_DISPLAY_NAME);
request.setHttpMethod(httpMethod);
request.addHandlerContext(S3HandlerContextKeys.IS_CHUNKED_ENCODING_DISABLED,
Boolean.valueOf(clientOptions.isChunkedEncodingDisabled()));
request.addHandlerContext(S3HandlerContextKeys.IS_PAYLOAD_SIGNING_ENABLED,
Boolean.valueOf(clientOptions.isPayloadSigningEnabled()));
//my code starts here
if ( originalRequest.getCustomRequestHeaders() != null ) {
Set<String> keys = originalRequest.getCustomRequestHeaders().keySet();
for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();) {
String headerKey = iterator.next();
request.addHeader(headerKey, originalRequest.getCustomRequestHeaders().get(headerKey));
}
}
//my code ends here
resolveRequestEndpoint(request, bucketName, key, endpoint);
request.addHandlerContext(HandlerContextKey.SIGNING_REGION, getSigningRegion());
return request;
}
RequestCopyUtils.java
public static GetObjectMetadataRequest createGetObjectMetadataRequestFrom(GetObjectRequest getObjectRequest) {
GetObjectMetadataRequest getObjectMetadataRequest = new GetObjectMetadataRequest(getObjectRequest.getBucketName(), getObjectRequest.getKey())
.withVersionId(getObjectRequest.getVersionId())
.withRequesterPays(getObjectRequest.isRequesterPays())
.withSSECustomerKey(getObjectRequest.getSSECustomerKey())
.withPartNumber(getObjectRequest.getPartNumber());
Set<String> keys = getObjectRequest.getCustomRequestHeaders().keySet();
for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();) {
String key = iterator.next();
getObjectMetadataRequest.putCustomRequestHeader(key, getObjectRequest.getCustomRequestHeaders().get(key));
}
return getObjectMetadataRequest;
}
对我来说,这似乎是一个错误。但是有什么合适的解决办法吗?
问候