How to write an S3 object to a file?

前端 未结 5 1835
无人及你
无人及你 2020-12-17 10:14

What\'s the fastest way to write an S3 object (of which I have the key) to a file? I\'m using Java.

5条回答
  •  执念已碎
    2020-12-17 11:12

    From AWS SDK for Java v2 released in 2017, you can just specify a Path for writing to the file.

    s3.getObject(GetObjectRequest.builder().bucket(bucket).key(key).build(),
            ResponseTransformer.toFile(Paths.get("multiPartKey")));
    

    https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/examples-s3-objects.html#download-object

    If you need a File, you can use toFile method.

    Path path = Paths.get("file.txt");
    s3.getObject(GetObjectRequest.builder().bucket(bucket).key(key).build(),
            path);
    File file = path.toFile();
    

提交回复
热议问题