当前位置:   article > 正文

Java实现AmazonS3工具类_s3 getobjectasstring

s3 getobjectasstring

文章目录


1. 依赖

<dependency>
	<groupId>com.amazonaws</groupId>
	<artifactId>aws-java-sdk-s3</artifactId>
	<version>1.12.632</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

2. 代码

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
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.PutObjectResult;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * bucketName 存储桶(开头结尾都不带 '/', eg: ddd)
 * key        路径(开头不带 '/', eg: product/test/xxx)
 */
public class AmazonS3Util {
    private static final Logger log = LoggerFactory.getLogger(AmazonS3Util.class);

    private static final AWSCredentials credentials = new BasicAWSCredentials("s3_access_key", "s3_secret_key");
    private static final AmazonS3 s3Client = AmazonS3ClientBuilder
            .standard()
            .withCredentials(new AWSStaticCredentialsProvider(credentials))
            .withRegion(Regions.US_EAST_1)
            .build();

    /**
     * 上传
     */
    public static boolean putObject(String bucketName, String key, String content) {
        byte[] contentBytes = content.getBytes(StringUtils.UTF8);
        InputStream input = new ByteArrayInputStream(contentBytes);
        return putObject(bucketName, key, input);
    }

    public static boolean putObject(String bucketName, String key, InputStream inputStream) {
        ObjectMetadata metadata = new ObjectMetadata();
        try {
            metadata.setContentLength(inputStream.available());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        PutObjectResult result = s3Client.putObject(bucketName, key, inputStream, metadata);
        return result != null && !result.getVersionId().isEmpty();
    }

	/**
     * 查询
     */
    public static S3Object getObject(String bucketName, String key) {
        if (s3Client.doesObjectExist(bucketName, key)) {
            return s3Client.getObject(bucketName, key);
        }
        return null;
    }

    public static String getObjectAsString(String bucketName, String key) {
        if (s3Client.doesObjectExist(bucketName, key)) {
            return s3Client.getObjectAsString(bucketName, key);
        }
        return null;
    }

	/**
     * 删除
     */
    public static void deleteObject(String bucketName, String key) {
        s3Client.deleteObject(bucketName, key);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/706904
推荐阅读
相关标签
  

闽ICP备14008679号