简体   繁体   中英

how to download a perticular file using it's name from amazon s3 bucket using java

Actually i have been trying to download the specific file using it's name from amazon s3 bucket folder using java code but i didn't find the worth answer from anyone.

package com.simon.amazonbucket;
import java.io.FileOutputStream;
import org.apache.tomcat.util.http.fileupload.IOUtils;
    import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
    public class DownloadingImage {
    public static void main(String[] args) {
        AmazonS3 s3 = new AmazonS3Client(
                new ClasspathPropertiesFileCredentialsProvider());
        try {
/*          GetObjectRequest obj = new GetObjectRequest("simon-aluvala/simonFolder",
    "/" + "");
            S3Object gobj = s3.getObject(obj);
            String name = gobj.getKey();
            S3ObjectInputStream in = gobj.getObjectContent();
            IOUtils.copy(in, new FileOutputStream(
                    "C:/Users/ycs/Desktop/test.doc"));
            System.out.println("downloaded successfully...............");*/

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

How to Get an Object Using the AWS SDK for Java :

  1. Create an instance of the AmazonS3Client class by providing your AWS credentials.
  2. Execute one of the AmazonS3Client.getObject() method. You need to provide the request information, such as bucket name, and key name. You provide this information by creating an instance of the GetObjectRequest class.
  3. Execute one of the getObjectContent() methods on the object returned to get a stream on the object data and process the response.

You can find example here : http://docs.aws.amazon.com/AmazonS3/latest/dev/RetrievingObjectUsingJava.html

[Disclousre : Bucket Explorer]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM