AMAZON S3

使用官方SDK進行資料交換,

先安裝必要資料

POM.XML

<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 -->
<dependency>
	<groupId>com.amazonaws</groupId>
	<artifactId>aws-java-sdk-s3</artifactId>
	<version>1.11.767</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-kms -->
<dependency>
	<groupId>com.amazonaws</groupId>
	<artifactId>aws-java-sdk-kms</artifactId>
	<version>1.11.767</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-core -->
<dependency>
	<groupId>com.amazonaws</groupId>
	<artifactId>aws-java-sdk-core</artifactId>
	<version>1.11.767</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazonaws/jmespath-java -->
<dependency>
	<groupId>com.amazonaws</groupId>
	<artifactId>jmespath-java</artifactId>
	<version>1.11.767</version>
</dependency>

上傳物件 

String access = "access"; //ex: AKIA3PGEPUAAAAAABBBB
String secret = "secret"; //ex: W3pIE45y64DAjeRXyBUQAAAAAAAAAABBBBBBBBBB
Regions regions = Regions.AP_EAST_1;
String bucket = "bucket";
String fileName = "test/data.jpg"; //檔案(包含資料夾)

//憑證
AWSCredentials creds = new BasicAWSCredentials(access, secret); 

//建立連線
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
		.withRegion(regions)
		.withCredentials(new AWSStaticCredentialsProvider(creds))
		.build();

//資料設定
PutObjectRequest request = new PutObjectRequest(bucket, fileName, new File("C:\\Users\\aaa\\Desktop\\logo.jpg"));
//Metadata
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("image/jpg");
request.setMetadata(metadata);
//ACL
//https://docs.aws.amazon.com/zh_tw/AmazonS3/latest/dev/acl-overview.html 
request.setCannedAcl(CannedAccessControlList.PublicRead);

//開始傳送
s3Client.putObject(request);

取得檔案路徑

String access = "access"; //ex: AKIA3PGEPUAAAAAABBBB
String secret = "secret"; //ex: W3pIE45y64DAjeRXyBUQAAAAAAAAAABBBBBBBBBB
Regions regions = Regions.AP_EAST_1;
String bucket = "bucket";
String fileName = "test/data.jpg"; //檔案(包含資料夾)

//憑證
AWSCredentials creds = new BasicAWSCredentials(access, secret); 

//建立連線
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
		.withRegion(regions)
		.withCredentials(new AWSStaticCredentialsProvider(creds))
		.build();

//s3路徑
String fileUrl = s3Client.getUrl(bucket, fileName).toString();

取得物件 

String access = "access"; //ex: AKIA3PGEPUAAAAAABBBB
String secret = "secret"; //ex: W3pIE45y64DAjeRXyBUQAAAAAAAAAABBBBBBBBBB
Regions regions = Regions.AP_EAST_1;
String bucket = "bucket";
String fileName = "test/data.jpg"; //檔案(包含資料夾)

//憑證
AWSCredentials creds = new BasicAWSCredentials(access, secret); 

//建立連線
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
		.withRegion(regions)
		.withCredentials(new AWSStaticCredentialsProvider(creds))
		.build();

S3Object s3Object = s3Client.getObject(bucket , fileName);

取得物件metadata

String access = "access"; //ex: AKIA3PGEPUAAAAAABBBB
String secret = "secret"; //ex: W3pIE45y64DAjeRXyBUQAAAAAAAAAABBBBBBBBBB
Regions regions = Regions.AP_EAST_1;
String bucket = "bucket";
String fileName = "test/data.jpg"; //檔案(包含資料夾)

//憑證
AWSCredentials creds = new BasicAWSCredentials(access, secret); 

//建立連線
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
		.withRegion(regions)
		.withCredentials(new AWSStaticCredentialsProvider(creds))
		.build();

//TYPE 1
ObjectMetadata objectMetadata = s3Client.getObjectMetadata(bucket, fileName);
//TYPE 2
objectMetadata = s3Client.getObjectMetadata(new GetObjectMetadataRequest(bucket, fileName));

 

刪除物件

需考慮到版本控制問題:
如果bucket啟用S3 版本控制,當刪除未指定版本 ID 的物件,S3 僅在物件上新增刪除標記。
否則必須透過指定版本 ID 來刪除物件的特定版本 (可透過ListVersions 抓取此物件所有版本再刪除)


ref:
https://docs.aws.amazon.com/zh_tw/AmazonS3/latest/dev/DeletingOneObjectUsingJava.html
https://docs.aws.amazon.com/zh_tw/AmazonS3/latest/dev/DeleteMarker.html

刪除單檔案

String access = "access"; //ex: AKIA3PGEPUAAAAAABBBB
String secret = "secret"; //ex: W3pIE45y64DAjeRXyBUQAAAAAAAAAABBBBBBBBBB
Regions regions = Regions.AP_EAST_1;
String bucket = "bucket";
String fileName = "test/data.jpg"; //檔案(包含資料夾)

//憑證
AWSCredentials creds = new BasicAWSCredentials(access, secret); 

//建立連線
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
		.withRegion(regions)
		.withCredentials(new AWSStaticCredentialsProvider(creds))
		.build();

//無版本控管
//TYPE 1
s3Client.deleteObject(bucket, fileName);
//TYPE 2
s3Client.deleteObject(new DeleteObjectRequest(bucket, fileName));
//有版本控管
//TYPE 1
s3Client.deleteVersion(bucket, fileName, "bbbCs_D6Ay5VxoI2soku79QSSSS_aaaa");
//TYPE 2
s3Client.deleteVersion(new DeleteVersionRequest(bucket, fileName, "bbbCs_D6Ay5VxoI2soku79QSSSS_aaaa"));

一次刪除有版本控制檔案

public static ArrayList listVersions(AmazonS3 s3Client, String _bucketName, String _file){

	ArrayList keys = new ArrayList();

	try {

		ListVersionsRequest request = new ListVersionsRequest()
			.withBucketName(_bucketName)
			.withPrefix(_file);
		
		VersionListing versionListing = s3Client.listVersions(request);
		for (S3VersionSummary objectSummary : versionListing.getVersionSummaries()) {
		  keys.add(new KeyVersion(objectSummary.getKey(), objectSummary.getVersionId()));
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return keys;
}

public static void deleteObjWithVersions(){
	String access = "access"; //ex: AKIA3PGEPUAAAAAABBBB
	String secret = "secret"; //ex: W3pIE45y64DAjeRXyBUQAAAAAAAAAABBBBBBBBBB
	Regions regions = Regions.AP_EAST_1;
	String bucket = "bucket";
	String fileName = "test/data.jpg"; //檔案(包含資料夾)

	//憑證
	AWSCredentials creds = new BasicAWSCredentials(access, secret); 

	try {
		AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
				.withCredentials(new AWSStaticCredentialsProvider(creds))
				.withRegion(region)
				.build();

		ArrayList keys = listVersions(s3Client, bucket , fileName);
		
		DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucket)
			.withKeys(keys)
			.withQuiet(false);

	 // Verify that the object versions were successfully deleted.
	 DeleteObjectsResult DeleteObjectsResult = s3Client.deleteObjects(multiObjectDeleteRequest);
	 System.out.println(DeleteObjectsResult.getDeletedObjects().size());
		
	} catch (Exception e) {
		e.printStackTrace();
	}
}

    咪卡恰比 發表在 痞客邦 留言(0) 人氣()