当前位置 : 首页 » 文章分类 :  开发  »  AWS-S3使用笔记

AWS-S3使用笔记

aws s3 使用笔记


下载

/**
 * 从指定bucket下载名为key的文件并保存到本地localFileDir目录
 * @param bucket bucket名,包含子目录
 * @param key 文件名
 * @param localDir 本地目录名
 * @return
 */
public static File downloadFileToLocalDir(String bucket, String key, String localDir) {
    logger.info("Download object from s3, bucket: {}, key: {}, to local dir: {}", bucket, key, localDir);
    try {
        S3Object s3Object = s3Client.getObject(bucket, key);
        File fileDir = new File(localDir);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        File file = new File(localDir + key);
        OutputStream outputStream = new FileOutputStream(file);
        IOUtils.copy(s3Object.getObjectContent(), outputStream);
        s3Object.getObjectContent().close();
        outputStream.close();
        return file;
    } catch (Exception e) {
        logger.error("Fail to download file from amazon s3, bucket: {}, key: {}, to local dir: {}", bucket, key, localDir, e);
        throw new UdsRuntimeException(UdsErrorCode.INTERNAL_SERVER_ERROR,
                "Fail to download file from amazon s3, bucket: {}, key: {}, to local dir: {}", bucket, key, localDir);
    }
}

对 Amazon S3 对象执行操作
https://docs.aws.amazon.com/zh_cn/sdk-for-java/v1/developer-guide/examples-s3-objects.html

使用 AWS SDK for Java 获取对象
https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/dev/RetrievingObjectUsingJava.html

Interface AmazonS3
https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3.html


使用 AWS SDK for Java 上传对象

以下示例将创建两个对象。第一个对象将一个文本字符串作为数据,第二对象是一个文件。该示例通过在对 AmazonS3Client.putObject() 的调用中直接指定存储桶名称、对象键和文本数据来创建第一个对象。该示例通过使用指定存储桶、对象键和文件路径的 PutObjectRequest 来创建第二个对象。PutObjectRequest 还指定 ContentType 标头和标题元数据。

// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)

import java.io.File;
import java.io.IOException;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;

public class UploadObject {

    public static void main(String[] args) throws IOException {
        String clientRegion = "*** Client region ***";
        String bucketName = "*** Bucket name ***";
        String stringObjKeyName = "*** String object key name ***";
        String fileObjKeyName = "*** File object key name ***";
        String fileName = "*** Path to file to upload ***";

        try {
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(clientRegion)
                    .withCredentials(new ProfileCredentialsProvider())
                    .build();

            // Upload a text string as a new object.
            s3Client.putObject(bucketName, stringObjKeyName, "Uploaded String Object");

            // Upload a file as a new object with ContentType and title specified.
            PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType("plain/text");
            metadata.addUserMetadata("x-amz-meta-title", "someTitle");
            request.setMetadata(metadata);
            s3Client.putObject(request);
        }
        catch(AmazonServiceException e) {
            // The call was transmitted successfully, but Amazon S3 couldn't process
            // it, so it returned an error response.
            e.printStackTrace();
        }
        catch(SdkClientException e) {
            // Amazon S3 couldn't be contacted for a response, or the client
            // couldn't parse the response from Amazon S3.
            e.printStackTrace();
        }
    }
}

Interface AmazonS3
https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3.html

使用 AWS SDK for Java 上传对象
https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/dev/UploadObjSingleOpJava.html


上一篇 Spring-Web

下一篇 关于

阅读
评论
639
阅读预计3分钟
创建日期 2019-01-07
修改日期 2019-01-07
类别

页面信息

location:
protocol:
host:
hostname:
origin:
pathname:
href:
document:
referrer:
navigator:
platform:
userAgent:

评论