Amazon S3 Java API测试用例

一、 Amazon S3基本概念

S3其实是一套接口规范,很多存储都实现了S3这套规范,例如hadoop、ranger、hive、ceph等。

我们只需要关心S3提供的API服务而没必要关心其背后用什么存储,所以称之为云存储。

  • 桶 :是s3存储对象的容器。用bucket表示,简单的理解就是一个玩具桶,可以装玩具。
  • 对象:对象是 Amazon S3 中存储的基本实体。简单理解就是玩具桶中的玩具。
  • 键:键是指存储桶中对象的唯一标识符。简单理解就是玩具的名字。
  • 区域: AWS 区域供 Amazon S3 存储您创建的存储桶。

更多概念:https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/dev/Introduction.html

二、pom依赖


        
            
                com.amazonaws
                aws-java-sdk-bom
                1.11.837
                pom
                import
            
        


        
            com.amazonaws
            aws-java-sdk-s3
        
        
        
            com.amazonaws
            aws-java-sdk-sts
        
        
            com.amazonaws
            aws-java-sdk-iam
        
        
        
            com.amazonaws
            aws-java-sdk-kms
         

三、测试用例

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
import com.amazonaws.regions.Regions;
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.CreateBucketRequest;
import com.amazonaws.services.s3.model.GetBucketLocationRequest;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;

/**
 * junit5
 */
public class S3Test {
	
	private Regions clientRegion = Regions.DEFAULT_REGION;
	
	private String accessKey = "WX1VZVZIH1NVE9BJQ7";
	
	private String secretKey = " jXNVq0my27iniIlWRDVbBLGhASfr4T0WNnKSIA";
	
	private String s3Endpoint = "http://192.16.40.1:7480";
	
	AmazonS3 s3Client = null;
	
	@BeforeEach
	public void init() {
		s3Client = AmazonS3ClientBuilder.standard()
				.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
				.withEndpointConfiguration(
						new EndpointConfiguration(s3Endpoint, clientRegion.getName()))
				.build();

	}
	
	@Test
	public void testBucketExist() {
		String bucketName = "test";
		assertTrue(s3Client.doesBucketExistV2(bucketName),"bucket"+ bucketName + "doesn't exist.");
	}
	
	@Test
	public void testListBucket() {
		List list = s3Client.listBuckets();
		list.stream().forEach(bucket -> {
			System.out.println(bucket.toString());
		});
	}
	
	@Test
	public void testCreateBucket() {
		String bucketName = "test";
		if(!s3Client.doesBucketExistV2(bucketName)) {
			s3Client.createBucket(bucketName);
		}else {
			System.out.println("Bucket " + bucketName + " has already exist.");
		}
	}
	
	@Test
	public void testCreatBucket2() {
		String bucketName = "test2";
		if(!s3Client.doesBucketExistV2(bucketName)) {
			s3Client.createBucket(new CreateBucketRequest(bucketName));
		}else {
			System.out.println("Bucket " + bucketName + " has already exist.");
		}
	}
	
	@Test
	public void testGetBucketLocation() {
		String bucketName = "test";
		String bucketLocation = s3Client.getBucketLocation(new GetBucketLocationRequest(bucketName));
        System.out.println("Bucket location: " + bucketLocation);
	}
	
	@Test
	public void testDeleteBucket() {
		String bucketName = "test";
		if(s3Client.doesBucketExistV2(bucketName)) {
			//确保bucket下面没有object才能删除,否则报错409
			s3Client.deleteBucket(bucketName);
		}
	}
	
	@Test
	public void testListObject() {
		String bucketName = "test";
		ListObjectsV2Result result = s3Client.listObjectsV2(bucketName);
		result.getObjectSummaries().stream().forEach(object -> {
			System.out.println(object.toString());
		});
	}
	
	@Test
	public void testListObject2() {
		String bucketName = "test";
		//prefix用于查询bucket下某个以某些字符串起始的key的object
		//可以用来模拟查询某个目录下所有的object的需求
		String keyProfix = "test";
		ListObjectsV2Result result = s3Client.listObjectsV2(bucketName , keyProfix);
		result.getObjectSummaries().stream().forEach(object -> {
			System.out.println(object.toString());
		});
	}
	
	@Test
	public void testUploadObject() {
		String bucketName = "test";
		String key = "test1";
		String data = "my test 1";
		s3Client.putObject(bucketName, key , data);
	}
	
	@Test
	public void testUploadObject2() {
		String bucketName = "test";
		String key = "爱在西元前";
		File file = new File("D:\\music\\周杰伦 - 范特西\\01. 周杰伦 - 爱在西元前.wav");
		s3Client.putObject(bucketName, key , file);
	}
	
	@Test
	public void testDeleteObject() {
		String bucketName = "test";
		String key = "music1";
		s3Client.deleteObject(bucketName, key);
	}
	
	@Test
	public void testExistObject() {
		String bucketName = "test";
		String key = "test1";
		System.out.println(s3Client.doesObjectExist(bucketName, key));
	}
	
	@Test
	public void testGetObject() {
		String bucketName = "test";
		String key = "爱在西元前";
		//可以下载到本地
		String outputPath = "D:\\tmp\\爱在西元前.wav";
		try  {
			S3Object o = s3Client.getObject(bucketName, key);
            S3ObjectInputStream s3is = o.getObjectContent();
            FileOutputStream fos = new FileOutputStream(new File(outputPath));
            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 (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}	

更多测试用例:https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/java

你可能感兴趣的:(大数据,AmazonS3,java)