/*
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
//或者下載官網源碼
}
}