一 .Amazon S3介绍
Amazon Simple Storage Service (Amazon S3) 是一种对象存储,它具有简单的 Web 服务接口,可用于在 Web 上的任何位置存储和检索任意数量的数据。它能够提供 99.999999999% 的持久性,并且可以在全球大规模传递数万亿对象。
客户使用 S3 作为云原生应用程序的主要存储;作为分析的批量存储库或“数据湖”;作为备份和恢复以及灾难恢复的目标;并将其与无服务器计算配合使用。
使用 Amazon 的云数据迁移选项,客户可以轻松地将大量数据移入或移出 Amazon S3。数据在存储到 S3 中之后,会自动采用成本更低、存储期限更长的云存储类 (如 S3 Standard – Infrequent Access 和 Amazon Glacier) 进行存档。
二.Java S3 Example
准备工作:
1.导入依赖包
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.9.2</version>
</dependency>
2.在s3服务中创建用户,获取用户的Access key和Secret Access Key,使用这个作为凭证连接s3
3.在s3服务中配置AWSConnector和AmazonS3FullAccess的连接权限。可以通过这个配置,在访问的时候进行authenticate验证。
s3 api简单操作:
1.创建凭证
AWSCredentials credentials = new BasicAWSCredentials(“YourAccessKeyID”, “YourSecretAccessKey”);
2.创建S3 Client
AmazonS3 s3client = new AmazonS3Client(credentials);
3.创建Bucket
String bucketName = “javatutorial-net-example-bucket”;
s3client.createBucket(bucketName);
4.获取s3 Bucket的list
for (Bucket bucket : s3client.listBuckets()) {
System.out.println(” – ” + bucket.getName());
}
5.在s3 Bucket中创建文件
public static void createFolder(String bucketName, String folderName, AmazonS3 client) {
// create meta-data for your folder and set content-length to 0
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(0);
// create empty content
InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
// create a PutObjectRequest passing the folder name suffixed by /
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,
folderName + SUFFIX, emptyContent, metadata);
// send request to S3 to create folder
client.putObject(putObjectRequest);
}
6.上传文件
String fileName = folderName + SUFFIX + “testvideo.mp4”;
s3client.putObject(new PutObjectRequest(bucketName, fileName,
new File(“C:\Users\user\Desktop\testvideo.mp4”)));
7.删除Bucket
s3client.deleteBucket(bucketName);
8.删除文件
s3client.deleteObject(bucketName, fileName);
完整实例
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.List;
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.Bucket;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3ObjectSummary;
public class AmazonS3Example {
private static final String SUFFIX = “/”;
public static void main(String[] args) {
// credentials object identifying user for authentication
// user must have AWSConnector and AmazonS3FullAccess for
// this example to work
AWSCredentials credentials = new BasicAWSCredentials(
“YourAccessKeyID”,
“YourSecretAccessKey”);
// create a client connection based on credentials
AmazonS3 s3client = new AmazonS3Client(credentials);
// create bucket – name must be unique for all S3 users
String bucketName = “javatutorial-net-example-bucket”;
s3client.createBucket(bucketName);
// list buckets
for (Bucket bucket : s3client.listBuckets()) {
System.out.println(” – ” + bucket.getName());
}
// create folder into bucket
String folderName = “testfolder”;
createFolder(bucketName, folderName, s3client);
// upload file to folder and set it to public
String fileName = folderName + SUFFIX + “testvideo.mp4”;
s3client.putObject(new PutObjectRequest(bucketName, fileName,
new File(“C:\Users\user\Desktop\testvideo.mp4”))
.withCannedAcl(CannedAccessControlList.PublicRead));
deleteFolder(bucketName, folderName, s3client);
// deletes bucket
s3client.deleteBucket(bucketName);
}
public static void createFolder(String bucketName, String folderName, AmazonS3 client) {
// create meta-data for your folder and set content-length to 0
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(0);
// create empty content
InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
// create a PutObjectRequest passing the folder name suffixed by /
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,
folderName + SUFFIX, emptyContent, metadata);
// send request to S3 to create folder
client.putObject(putObjectRequest);
}
/**
* This method first deletes all the files in given folder and than the
* folder itself
*/
public static void deleteFolder(String bucketName, String folderName, AmazonS3 client) {
List fileList =
client.listObjects(bucketName, folderName).getObjectSummaries();
for (S3ObjectSummary file : fileList) {
client.deleteObject(bucketName, file.getKey());
}
client.deleteObject(bucketName, folderName);
}
}
参考:
https://javatutorial.net/java-s3-example
https://aws.amazon.com/cn/s3/getting-started/
python版的s3 api:
http://boto3.readthedocs.io/en/latest/guide/quickstart.html