Amazonaws S3 Java SDK Connection

Source: Internet
Author: User
Tags getmessage save file

 PackageCom.inslink.sinosoft.util;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;Importjava.util.List;Importcom.amazonaws.AmazonServiceException;Importcom.amazonaws.ClientConfiguration;ImportCom.amazonaws.Protocol;ImportCom.amazonaws.auth.AWSStaticCredentialsProvider;Importcom.amazonaws.auth.BasicAWSCredentials;ImportCom.amazonaws.client.builder.AwsClientBuilder;Importcom.amazonaws.services.s3.AmazonS3;ImportCom.amazonaws.services.s3.AmazonS3ClientBuilder;ImportCom.amazonaws.services.s3.model.Bucket;Importcom.amazonaws.services.s3.model.ObjectListing;ImportCom.amazonaws.services.s3.model.S3Object;ImportCom.amazonaws.services.s3.model.S3ObjectInputStream;Importcom.amazonaws.services.s3.model.S3ObjectSummary; Public classS3sample { Public Static voidMain (string[] args)throwsIOException {//Create an Amazon S3 object with explicit credentialsBasicawscredentials credentials =NewBasicawscredentials ("Your accesskey", "Your Secretkey"); Clientconfiguration ClientConfig=Newclientconfiguration (); Clientconfig.setsigneroverride ("S3signertype");//Voucher Verification MethodClientconfig.setprotocol (protocol.http);//Access AgreementAmazonS3 s3client =Amazons3clientbuilder.standard (). Withcredentials (NewAwsstaticcredentialsprovider (Credentials)). Withclientconfiguration (ClientConfig). Withendpointconfiguration (NewAwsclientbuilder.endpointconfiguration (//set the endpoint configuration to use for the request (service endpoint and signature area)"S3.yiducloud.cn",                           "Cn-north-1")). Withpathstyleaccessenabled (true)//whether to use path mode, yes S3.yiducloud.cn/bucketname. Build (); System.out.println ("Uploading a new object to S3 from a file\n"); //Enumerate Bucketslist<bucket> buckets =s3client.listbuckets ();  for(Bucket bucket:buckets) {System.out.println ("Bucket:" +bucket.getname ()); }            //Enumerate buckets under Objectsobjectlisting objects = s3client.listobjects ("Sinosoft-ocr-bucket");  Do {                 for(S3objectsummary objectSummary:objects.getObjectSummaries ()) {System.out.println ("Object:" +Objectsummary.getkey ()); } Objects=s3client.listnextbatchofobjects (objects); }  while(objects.istruncated ()); //File Upload            Try{s3client.putobject ("Bucketname", "KeyName",NewFile ("Your file path")); } Catch(amazonserviceexception e) {System.err.println (E.geterrormessage ()); System.exit (1); }                        //File Download            Try{s3object o= S3client.getobject ("Bucketname", "Your file ' s KeyName"); S3objectinputstream S3is=o.getobjectcontent (); FileOutputStream Fos=NewFileOutputStream (NewFile ("Your Save file path")); byte[] Read_buf =New byte[1024]; intRead_len = 0;  while((Read_len = S3is.read (read_buf)) > 0) {fos.write (Read_buf,0, Read_len);                } s3is.close ();            Fos.close (); } Catch(amazonserviceexception e) {System.err.println (E.geterrormessage ()); System.exit (1); } Catch(FileNotFoundException e) {System.err.println (E.getmessage ()); System.exit (1); } Catch(IOException e) {System.err.println (E.getmessage ()); System.exit (1); }                        //Other methods of operation refer to:https://docs.aws.amazon.com/zh_cn/sdk-for-java/v1/developer-guide/examples-s3-objects.html            //or download the official website source code                }}

Amazonaws S3 Java SDK Connection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.