AWS S3请求者支付不适用于TransferManager

时间:2017-05-29 19:08:35

标签: java amazon-s3 awss3transfermanager s3transfermanager

我正在尝试在我的java程序中使用TransferManager类从Requester Pays Buckets下载文件。 我得到"状态代码:403"来自amazon AWS的例外情况。 我写了一个简单的程序来测试这个功能,并比较它下载文件的单一连接方式。这是我的代码:

import java.io.*;
import com.amazonaws.auth.*;
import com.amazonaws.services.s3.*;
import com.amazonaws.services.s3.model.*;
import com.amazonaws.services.s3.transfer.*;
import com.amazonaws.util.IOUtils;
public class RequesterPaysTest {
    final static AWSCredentials awsCredentials = new BasicAWSCredentials(MY IAMAccessKey,MY IAMSecretKey);
    final static String bucketName = "7268982505fe.mixnode.com";
    final static String fileName = "5379-7268982505fe-0-1496081968663.warc.gz";
    final static AWSCredentialsProvider awsCredentialsProvider =  new AWSStaticCredentialsProvider(awsCredentials);
    final static AmazonS3 s3client = AmazonS3ClientBuilder.standard().withCredentials(awsCredentialsProvider).withRegion(MY bucketRegion).build();
    final static GetObjectRequest getRequest = new GetObjectRequest(bucketName, fileName, true);

    static void testSimpleRequesterPays() {
        try {
        S3Object object = s3client.getObject(getRequest);
        InputStream objectData = object.getObjectContent();
        FileOutputStream out = new FileOutputStream (new File(fileName));
        IOUtils.copy(objectData, out);
        out.close();
        System.out.println(" Simple RequesterPays successful");
        } catch (Exception e) {
        System.out.println(" Simple RequesterPays unsuccessful: " + e.getMessage());
        }
    }
    static void testTransferManagerRequesterPays() {
        try {
            TransferManager tx = TransferManagerBuilder.standard().withS3Client(s3client).build();
            Download download = tx.download(getRequest, new File(fileName));
            while (download.isDone() == false)
                Thread.sleep(10);
            System.out.println(" TransferManager RequesterPays successful");
        } catch (Exception e) {
        System.out.println(" TransferManager RequesterPays unsuccessful: " + e.getMessage());
        }
    }
public static void main(String[] args) throws IOException {
    testSimpleRequesterPays();
    testTransferManagerRequesterPays();
}
}

这是输出:

Simple RequesterPays successful
 TransferManager RequesterPays unsuccessful: Forbidden (Service: Amazon S3; Status Code: 403; Error Code: 403 Forbidden; Request ID: 77D5EBF5EE195A7A)

如您所见,可以使用简单方法下载相同的文件,但不能使用TransferManager下载。我尝试创建自己的存储桶并使用权限进行游戏,但它无法正常工作。 我想知道我的代码中是否有任何遗漏?或者AWS s3是否不支持在请求者支付存储桶上使用TransferManager?

1 个答案:

答案 0 :(得分:0)

TransferManager在内部使用http HEAD 方法而不是 GET 来下载对象(文件)。您可以通过调试TransferManager SDK代码找到它。如果您的AWS api网关中没有 HEAD 方法配置,则必须创建 HEAD 方法配置以与TransferManager SDK代码匹配。顺便说一句,如果找不到资源,它应该给出404,但是您得到403。原因在这里https://forums.aws.amazon.com/thread.jspa?threadID=216684