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>

get Bucket ACL

String access = "access"; //ex: AKIA3PGEPUAAAAAABBBB
String secret = "secret"; //ex: W3pIE45y64DAjeRXyBUQAAAAAAAAAABBBBBBBBBB
Regions regions = Regions.AP_EAST_1;
String bucket = "bucket";

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

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

AccessControlList aclRes = s3Client.getBucketAcl(bucket);

for (Grant grant : aclRes.getGrantsAsList()) {
	System.out.println(grant.getGrantee().getIdentifier()); //182168387c385e506634d27dd5284f
	System.out.println(grant.getPermission());              //FULL_CONTROL
}

get Bucket Version On or Off

String access = "access"; //ex: AKIA3PGEPUAAAAAABBBB
String secret = "secret"; //ex: W3pIE45y64DAjeRXyBUQAAAAAAAAAABBBBBBBBBB
Regions regions = Regions.AP_EAST_1;
String bucket = "bucket";

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

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

String bucketVersionStatus = s3Client.getBucketVersioningConfiguration(bucket).getStatus();
System.out.println(bucketVersionStatus);  //Off, Suspended or Enabled

list file Versions

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();

ListVersionsRequest request = new ListVersionsRequest()
	.withBucketName(bucket)
	.withPrefix(fileName);
	//.withMaxResults(1);

VersionListing versionListing = s3Client.listVersions(request);
for (S3VersionSummary objectSummary : versionListing.getVersionSummaries()) {
  System.out.println(objectSummary.getKey());
  System.out.println(objectSummary.getVersionId());
}

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