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