Java 接口

Maven依赖

在 pom.xml 中加入

<dependency>
    <groupId>com.amazonaws</groupId>
	<artifactId>aws-java-sdk-s3</artifactId>
	<version>1.12.292</version>
</dependency>

Java 示例

package com.s3;
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.SdkClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
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 s3 {

	//两个KEY从安装的服务器 /root/.s3cfg 获取
	private static String access_key_id = "5WsAtWJEkopkO9Oo";
	private static String secret_key_id = "kchfTxgS30LfMUbLiZwKzAOLP2Xv2jov";
	private static String serviceEndpoint = "http://192.168.2.124:80";

	private static AwsClientBuilder.EndpointConfiguration endpointConfig =
			new AwsClientBuilder.EndpointConfiguration(serviceEndpoint, Regions.DEFAULT_REGION.getName());
	private static BasicAWSCredentials awsCredentials = 
			new BasicAWSCredentials(access_key_id, secret_key_id);

	private static AmazonS3 s3Client = AmazonS3Client.builder()
				.withEndpointConfiguration(endpointConfig)
				.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
				.build();

	// 列出桶
	public static void list_buckets() {
		List<Bucket> buckets = s3Client.listBuckets();
		for (Bucket b : buckets) {
			System.out.println(b.getName());
		}
	}

	// 创建桶
	public static void create_buckets() {
		//Bucket name should not contain uppercase characters
		String bucket_name = "udbtovol";
		s3Client.createBucket(bucket_name);
	}

	// 删除桶
	public static void delete_buckets() {
		String bucket_name = "udbtovol";
		s3Client.deleteBucket(bucket_name);
	}

	// 列出对象
	public static void list_objects() {
		String bucket_name = "udbtovol";
		ObjectListing result = s3Client.listObjects(bucket_name);
		List<S3ObjectSummary> objects = result.getObjectSummaries();
		for (S3ObjectSummary os : objects) {
			System.out.println("* " + os.getKey() + " : " + os.toString());
		}

		// 第二种方式
		ListObjectsV2Result resultV2 = s3Client.listObjectsV2(bucket_name);
		List<S3ObjectSummary> objectsV2 = resultV2.getObjectSummaries();
		for (S3ObjectSummary os : objectsV2) {
			System.out.println("* " + os.getKey() + " : " + os.toString());
		}
	}

	// 上传对象
	public static void put_object() {
		String bucket_name = "udbtovol";
		s3Client.putObject(bucket_name, "UDBTO.png", new File("UDBTO.png"));
	}

	// 下载对象
	public static void get_object() {
		try {
			String bucket_name = "udbtovol";
			S3Object so = s3Client.getObject(bucket_name, "UDBTO.png");
			S3ObjectInputStream s3is = so.getObjectContent();
			FileOutputStream fos = new FileOutputStream(new File("UDBTO.png"));
			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 (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	// 删除对象
	public static void delete_object() {
		String bucket_name = "udbtovol";
		s3Client.deleteObject(bucket_name, "UDBTO.png");
	}

	public static void main(String[] args) {
		System.out.println("udbto test start");
		try {
			list_buckets();
			//create_buckets();
			//delete_buckets();
			//list_objects();
			//put_object();
			//get_object();
			//delete_object();

		} catch (AmazonServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SdkClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 关闭客户端以避免线程警告
			if (s3Client instanceof AmazonS3Client) {
				((AmazonS3Client) s3Client).shutdown();
			}
		}
		System.out.println("udbto test end");
	}
}