/*
Amazon S3 是专为从任意位置存储和检索任意数量的数据而构建的对象存储,这些数据包括来自网站和移动应用程序、公司应用程序的数据以及来自 IoT 传感器或设备的数据
当然要使用s3,首先你得有使用账户,并在账户管理员处获得了使用凭证,即s3服务器,accesskey,secretkey

本文主要记录自己使用amazons3 java sdk操作存储对象的方法,当然首先是使用java jdk连接s3服务器
官方文档:https://docs.aws.amazon.com/zh_cn/sdk-for-java/v1/developer-guide/welcome.html
*/




package
com.inslink.sinosoft.util; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import com.amazonaws.AmazonServiceException; import com.amazonaws.ClientConfiguration; import com.amazonaws.Protocol; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.Bucket; import com.amazonaws.services.s3.model.ObjectListing; import com.amazonaws.services.s3.model.S3Object; import com.amazonaws.services.s3.model.S3ObjectInputStream; import com.amazonaws.services.s3.model.S3ObjectSummary; public class S3Sample { public static void main(String[] args) throws IOException { //创建Amazon S3对象使用明确凭证 BasicAWSCredentials credentials = new BasicAWSCredentials("your accesskey", "your secretkey"); ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setSignerOverride("S3SignerType");//凭证验证方式 clientConfig.setProtocol(Protocol.HTTP);//访问协议 AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(credentials)) .withClientConfiguration(clientConfig) .withEndpointConfiguration( new AwsClientBuilder.EndpointConfiguration(//设置要用于请求的端点配置(服务端点和签名区域) "s3.xxx.sn",//我的s3服务器 "cn-north-1")).withPathStyleAccessEnabled(true)//是否使用路径方式,是的话s3.xxx.sn/bucketname .build(); System.out.println("Uploading a new object to S3 from a file\n"); //枚举bucket List<Bucket> buckets = s3Client.listBuckets(); for (Bucket bucket : buckets) { System.out.println("Bucket: " + bucket.getName()); } //枚举bucket下对象 ObjectListing 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()); //文件上传 try { s3Client.putObject("bucketname", "keyname", new File("your file path")); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } //文件下载 try { S3Object o = s3Client.getObject("bucketname", "your file's keyname"); S3ObjectInputStream s3is = o.getObjectContent(); FileOutputStream fos = new FileOutputStream(new File("your save file path")); byte[] read_buf = new byte[1024]; int read_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); } //其他操作方法参考:https://docs.aws.amazon.com/zh_cn/sdk-for-java/v1/developer-guide/examples-s3-objects.html //或者下载官网源码 } }

 

posted on 2018-07-20 17:05  梓木的稻稻  阅读(12601)  评论(0编辑  收藏  举报