S3接口API

Jar包

在web应用开发中,我们使用Maven做项目管理,基于SpringBoot/SpringCloud做开发,需要引入S3的SDK包配置如下

        
            org.springframework.cloud
            spring-cloud-aws-context
            1.0.2.RELEASE
        

接口API

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.*;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.Objects;

/**
 * @param
 * @Author: DaviHe
 * @Description:
 * @Date: Created in 2018/4/16
 */

@Slf4j
public class S3Template {

    private AmazonS3 s3Client;
    private String bucketName;

    public S3Template setBucket(String bucketName) {
        this.bucketName = bucketName;
        return this;
    }

    public S3Template setAmzonS3Client(AmazonS3Client client) {
        this.s3Client = client;
        return this;
    }

    public void createBucketIfNotExists() {
        if (!(s3Client.doesBucketExist(bucketName))) {
            final Bucket bucket = s3Client.createBucket(new CreateBucketRequest(bucketName));
            log.info("bucket {} is not exists but has created , create time  {} ,owner {}", bucketName, bucket.getCreationDate(),
                    bucket.getOwner().getDisplayName());
        }

        String bucketLocation = s3Client.getBucketLocation(new GetBucketLocationRequest(bucketName));
        log.info("createBucketIfNotExists , bucket location {} ", bucketLocation);
    }

    public static boolean checkBucketExists(AmazonS3 s3, String bucketName) {
        List buckets = s3.listBuckets();
        for (Bucket bucket : buckets) {
            if (Objects.equals(bucket.getName(), bucketName)) {
                return true;
            }
        }
        return false;
    }

    public String uploadFile(File tempFile, String s3KeyName) {
        s3Client.putObject(new PutObjectRequest(bucketName, s3KeyName, tempFile).withCannedAcl(CannedAccessControlList.Private));
        GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(bucketName, s3KeyName);
        URL url = s3Client.generatePresignedUrl(urlRequest);

        log.info("uploaded File  [{}] to S3. url = [{}]", tempFile.getAbsolutePath(), url);
        return url.toString();
    }

    public S3ObjectInputStream downFile(String key) {
        GetObjectRequest request = new GetObjectRequest(bucketName, key);
        S3Object object = s3Client.getObject(request);
        S3ObjectInputStream inputStream = object.getObjectContent();
        log.info("downloaded file [{}] from s3 , url {} , ", key, inputStream.getHttpRequest().getURI());
        return inputStream;
    }

    public void downFile(String key, String localPath) {
        GetObjectRequest request = new GetObjectRequest(bucketName, key);
        s3Client.getObject(request, new File(localPath));
    }

    public String getUrlFromS3(String s3KeyName) {
        GeneratePresignedUrlRequest httpRequest = new GeneratePresignedUrlRequest(bucketName, s3KeyName);
        String url = s3Client.generatePresignedUrl(httpRequest).toString();//临时链接
        return url;
    }

    public void deleteKeyFile(String s3KeyName) {
        s3Client.deleteObject(bucketName, s3KeyName);
    }
}

常见异常处理

  1. 访问异常
Exception in thread "main" com.amazonaws.services.s3.model.AmazonS3Exception: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: 0**665AED3B9), S3 Extended Request ID: NXdN4BwKB66GguhS12WoTbUsfmhYBICivzjTPN+SjjfkwBrMhoNv/dSt**0QStFzru59F9Ck=

一个原因是,S3配置的key/secret的访问权限问题,这里设置成S3FullAccess即可。

S3接口API_第1张图片
image.png

另一个原因是某些用户做了bucket桶权限设置。所以即便在设置了访问密钥的 AmazonS3FullAccess,也依然无效。提前创建好 bucket吧。
2、Region设置异常

Exception in thread "main" com.amazonaws.services.s3.model.AmazonS3Exception: The bucket is in this region: null. Please use this region to retry the request (Service: Amazon S3; Status Code: 301; Error Code: PermanentRedirect; Request ID: 5B405B91**DDCE5), S3 Extended Request ID: woOdKiAQ16/NWtKwcbijGXBAVwHUd3ILE0qqemS**CZNJ3GPXOeQQKeVlO7HHMc=

检查region设置

  1. 访问密钥问题
com.amazonaws.services.s3.model.AmazonS3Exception: The AWS Access Key Id you provided does not exist in our records. (Service: Amazon S3; Status Code: 403; Error Code: InvalidAccessKeyId; Request ID: 4AEE**947D2F5ED), S3 Extended Request ID: ShgWcTW8***l4a0ziQs=

选择设置访问密钥

4、超时相关

Caught an AmazonClientException, which means the client encountered an internal error while trying to communicate with S3, such as not being able to access the network.
Error Message: Unable to execute HTTP request: Connect to s3.ap-northeast-2.amazonaws.com:443 [s3.ap-northeast-2.amazonaws.com/52.219.56.20] failed: connect timed out
com.amazonaws.SdkClientException: Unable to execute HTTP request: Connect to s3.ap-northeast-2.amazonaws.com:443 [s3.ap-northeast-2.amazonaws.com/52.219.56.20] failed: connect timed out

检查设置的Region是否可以联通,可以通过ping命令检查。

你可能感兴趣的:(S3接口API)