AWS S3 Java SDK-下载文件帮助

以下代码仅适用于从S3中的存储桶下载文本文件。这不适用于图像。有使用AWS开发工具包管理下载/类型的简便方法吗?文档中包含的示例并不明显。谢谢!

AWSCredentials myCredentials = new BasicAWSCredentials(

String.valueOf(Constants.act), String.valueOf(Constants.sk));

AmazonS3Client s3Client = new AmazonS3Client(myCredentials);

S3Object object = s3Client.getObject(new GetObjectRequest("bucket", "file"));

BufferedReader reader = new BufferedReader(new InputStreamReader(

object.getObjectContent()));

File file = new File("localFilename");

Writer writer = new OutputStreamWriter(new FileOutputStream(file));

while (true) {

String line = reader.readLine();

if (line == null)

break;

writer.write(line + "\n");

}

writer.close();

回答:

而不是ReaderWriter类,你应该使用InputStreamOutputStream类:

InputStream reader = new BufferedInputStream(

object.getObjectContent());

File file = new File("localFilename");

OutputStream writer = new BufferedOutputStream(new FileOutputStream(file));

int read = -1;

while ( ( read = reader.read() ) != -1 ) {

writer.write(read);

}

writer.flush();

writer.close();

reader.close();

以上是 AWS S3 Java SDK-下载文件帮助 的全部内容, 来源链接: utcz.com/qa/420485.html

回到顶部