我正在尝试在我的 java 程序中使用TransferManager类从Requester Pays Buckets下载文件。我从亚马逊 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?