我正在尝试在我的Java程序中使用TransferManager类来从请求者支付存储桶中下载文件。我从Amazon AWS获得了"状态代码:403"例外。我编写了一个简单的程序来测试此功能,并比较它下载文件的单连接方式。这是我的代码:
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?
使用http head 方法而不是 get 用于下载对象(file(s((。您可以通过调试TransferManager SDK代码来找到它。如果您没有AWS API网关中的 HEAD 方法配置,则必须创建 head 方法配置才能与TransferManager SDK代码匹配。顺便说一句,如果找不到资源应该给出404,但您将获得403。原因是在这里https://forums.aws.amazon.com/thread.jspa?threadid=216684