我正在尝试阅读Amazon S3上可用的文件,因为问题解释了问题。我找不到已弃用的构造函数的替代调用。
这是代码:
private String AccessKeyID = "xxxxxxxxxxxxxxxxxxxx";
private String SecretAccessKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private static String bucketName = "documentcontainer";
private static String keyName = "test";
//private static String uploadFileName = "/PATH TO FILE WHICH WANT TO UPLOAD/abc.txt";
AWSCredentials credentials = new BasicAWSCredentials(AccessKeyID, SecretAccessKey);
void downloadfile() throws IOException
{
// Problem lies here - AmazonS3Client is deprecated
AmazonS3 s3client = new AmazonS3Client(credentials);
try {
System.out.println("Downloading an object...");
S3Object s3object = s3client.getObject(new GetObjectRequest(
bucketName, keyName));
System.out.println("Content-Type: " +
s3object.getObjectMetadata().getContentType());
InputStream input = s3object.getObjectContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
while (true) {
String line = reader.readLine();
if (line == null) break;
System.out.println(" " + line);
}
System.out.println();
} catch (AmazonServiceException ase) {
//do something
} catch (AmazonClientException ace) {
// do something
}
}
有帮助吗?如果需要更多解释,请提及。我查看了SDK的.zip文件中提供的示例代码,它是一样的。
您可以使用AmazonS3ClientBuilder或AwsClientBuilder作为替代品。
对于S3,最简单的是AmazonS3ClientBuilder,
BasicAWSCredentials creds = new BasicAWSCredentials("access_key", "secret_key");
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)).build();
使用下面列出的代码创建没有凭据的S3客户端:
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();
用法示例是调用S3的lambda函数。
您可以按如下方式创建S3默认客户端(使用aws-java-sdk-s3-1.11.232):
AmazonS3ClientBuilder.defaultClient();