java s3示例
在本教程中,我将解释如何使用Amazon的S3存储和Amazon提供的javaapi。这个例子展示了如何创建一个bucket,列出它的内容,在bucket中创建一个文件夹,上传一个文件,给这个文件一个公共访问权限,最后如何删除所有这些项目。
建立你的项目
我们需要awssdkforjava才能使这个示例正常工作。如果我们现在还没有下载SDK,请在此处下载。我们还需要将归档文件中的.JAR文件集成到项目中。或者,我们可以使用具有以下依赖项的Maven
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk</artifactId> <version>1.9.2</version> </dependency>
在Amazon IAM中创建用户(https://console.aws.amazon.com/iam)如果你没有的话。在这里我们将得到一个“访问密钥”和“秘密访问密钥”。我们需要此凭据才能连接到S3.
向新用户添加IAM中的“AWSConnector”和“AmazonS3FullAccess”权限。如果没有这个,我们希望能够通过服务器进行身份验证。
通过Amazon S3认证
有4种不同的方法可以根据amazons3验证请求
1. 使用默认的凭证配置文件——这是Amazon推荐的方法。创建具有以下结构的文件并填写访问密钥:
# Move this credentials file to (~/.aws/credentials) # after you fill in your access and secret keys in the default profile # WARNING: To avoid accidental leakage of your credentials, # DO NOT keep this file in your source directory. [default] aws_access_key_id= aws_secret_access_key=
默认情况下,将此文件保存在.aws文件夹中的文件名“credentials”下,对于Windows用户,保存在C:\Users\user.aws\credentials中,或者在Linux中保存在主目录下
如果使用此方法,则可以在代码中创建凭据对象,如下所示:
AWSCredentials credentials = new ProfileCredentialsProvider().getCredentials();
2. 使用环境变量–设置系统中下列环境变量的值
AWS_ACCESS_KEY_ID
和
AWS_SECRET_ACCESS_KEY
3. Java系统属性
aws.accessKeyId
和
aws.secretKey
. 使用
SystemPropertiesCredentialsProvider
加载程序中的变量
4. 以编程方式设置凭证——在这个例子中,我将使用这个方法,因为它更容易理解
在代码中使用以下内容:
AWSCredentials credentials = new BasicAWSCredentials("YourAccessKeyID", "YourSecretAccessKey");
创建S3客户端
为了能够与S3通信,我们必须使用
AmazonS3
. 我们将使用实例向服务器发送请求
AmazonS3 s3client = new AmazonS3Client(credentials);
创建Bucket
bucket在整个S3领域中必须具有唯一的名称
String bucketName = "theitroad-net-example-bucket"; s3client.createBucket(bucketName);
列出bucket
你可以像这样把单子从所有的桶里拿出来
for (Bucket bucket : s3client.listBuckets()) { System.out.println(" - " + bucket.getName()); }
在S3 Bucket中创建文件夹
使用此代码在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); }
S3上传文件
如果你想上传一个文件到一个文件夹使用这个
String fileName = folderName + SUFFIX + "testvideo.mp4"; s3client.putObject(new PutObjectRequest(bucketName, fileName, new File("C:\Users\user\Desktop\testvideo.mp4")));
只需删除文件夹和文件名中的后缀即可直接上传到bucket中。如果你想让你的文件公开(文件在amazons3中默认是私有的),将其设置为PutObjectRequest(更多细节请参见下面的完整示例)
.withCannedAcl(CannedAccessControlList.PublicRead)
删除文件、文件夹、bucket
要删除存储桶,请使用此选项。存储桶必须为空,否则无法删除它们
s3client.deleteBucket(bucketName);
要删除文件,请使用:
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 = "theitroad-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); } }