1. 程式人生 > >Amazonaws S3 java SDK連接初探

Amazonaws S3 java SDK連接初探

port ocs The erl \n poi ted .get tsig

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.yiducloud.cn", "cn-north-1")).withPathStyleAccessEnabled(true)//是否使用路徑方式,是的話s3.yiducloud.cn/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 //或者下載官網源碼 } }

Amazonaws S3 java SDK連接初探