跳到主要内容

Java-SDK

S3-Java-SDK

推荐使用AWS S3官方的Java SDK

前置条件

  • JDK 7或更高版本,Maven 3或更高版本;
  • 如果您还不了解COS,请参考对象存储服务
  • 使用SDK需要拥有有效的密钥对(包括AccessKeyId和AccessKeySecret)进行签名认证,请在控制台界面获取该密钥对;

pom.xml

通过Maven导入所有需要依赖的jar包,pom.xml配置如下

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

建议使用示例版本,最新版本可参考链接,调用相关API,即可访问Amazon S3或其他兼容S3的存储服务

适用系统

Mac OS X,Windows,Linux等

使用S3-Java-SDK

以下代码使用SDK实现了基本的桶和对象操作:

  • 创建一个桶;
  • 获取用户桶列表;
  • 上传对象到桶中;
  • 下载桶中的对象;
  • 获取桶中的对象列表;
  • 从桶中删除对象;
  • 删除一个空桶;
package com.chinac.stor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.UUID;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;

public class S3SDKSample {

public static void main(String[] args) throws IOException {

ClientConfiguration config = new ClientConfiguration();

// SDK默认走HTTPS协议,这里设置为走HTTP
config.withProtocol(Protocol.HTTP);

// SDK默认v4签名,打开此项使用v2签名
config.setSignerOverride("S3SignerType");

// 设置密钥对,创建Credential对象
final String accessKeyId = "YOUR_ACCESS_KEY_ID";
final String secretAccessKey = "YOU_SECRET_ACCESS_KEY";
AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);

// 创建S3访问客户端
AmazonS3 s3 = new AmazonS3Client(credentials, config);
s3.setEndpoint("s3-cn-suzhou.chinac.com"); // 从用户信息页面描述中获取endpoint

final String bucketName = "test-bucket-" + UUID.randomUUID();
final String key = "test-key";

try {
// 创建一个桶
s3.createBucket(bucketName);

// 获取桶列表
for (Bucket bucket : s3.listBuckets()) {
System.out.println("Bucket:" + bucket.getName());
}

// 上传对象到桶中
s3.putObject(new PutObjectRequest(bucketName, key, createFile()));

// 下载刚刚上传的对象
S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
dumpInputStream(object.getObjectContent());

// 获取桶中的对象列表
ObjectListing objectListing = s3.listObjects(new ListObjectsRequest().withBucketName(bucketName));

for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.println(objectSummary.getKey() +"Size:" + objectSummary.getSize());
}

// 从桶中删除对象
s3.deleteObject(bucketName, key);

// 删除一个空桶
s3.deleteBucket(bucketName);
} catch (AmazonServiceException ase) {
System.out.println(ase.getMessage());
System.out.println(ase.getStatusCode());
} catch (AmazonClientException ace) {
System.out.println(ace.getMessage());
}
}

// 创建文件并返回该对象
private static File createFile() throws IOException {
File file = File.createTempFile("storultra_test", ".txt");
file.deleteOnExit();
Writer writer = new OutputStreamWriter(new FileOutputStream(file));
writer.write("Hello, world!\n");
writer.close();
return file;
}

// 输出InputStream流
private static void dumpInputStream(InputStream input) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
while (true) {
String line = reader.readLine();
if (line == null) break;
System.out.println(line);
}
}
}

Eclipse下执行代码

  • 在Eclipse下New->Maven Project;
  • New一个名为S3SDKSample的类;
  • 拷贝上述代码并保存到S3SDKSample.java文件中;
  • 右键Run As->Java Application,执行结果输出到控制台。