【Java-S3】使用Java操作S3-SDK,实现获取S3文件大小

  • Maven依赖
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>software.amazon.awssdkgroupId>
            <artifactId>bomartifactId>
            <version>2.20.45version>
            <type>pomtype>
            <scope>importscope>
        dependency>
    dependencies>
dependencyManagement>

<dependencies>
		<-- 核心依赖 -->
        <dependency>
            <groupId>com.amazonawsgroupId>
            <artifactId>aws-java-sdk-s3artifactId>
            <version>1.11.490version>
        dependency>
        
        <dependency>
            <groupId>org.junit.jupitergroupId>
            <artifactId>junit-jupiter-apiartifactId>
            <version>5.9.2version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>software.amazon.awssdkgroupId>
            <artifactId>secretsmanagerartifactId>
        dependency>
        <dependency>
            <groupId>com.google.code.gsongroupId>
            <artifactId>gsonartifactId>
            <version>2.10.1version>
        dependency>
        <dependency>
            <groupId>org.junit.jupitergroupId>
            <artifactId>junit-jupiter-engineartifactId>
            <version>5.9.2version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.14.2version>
        dependency>
        <dependency>
            <groupId>org.junit.platformgroupId>
            <artifactId>junit-platform-commonsartifactId>
            <version>1.9.2version>
        dependency>
        <dependency>
            <groupId>org.junit.platformgroupId>
            <artifactId>junit-platform-launcherartifactId>
            <version>1.9.2version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>software.amazon.awssdkgroupId>
            <artifactId>s3artifactId>
        dependency>
        <dependency>
            <groupId>software.amazon.awssdkgroupId>
            <artifactId>s3-transfer-managerartifactId>
            <version>2.20.26version>
        dependency>
        <dependency>
            <groupId>software.amazon.awssdkgroupId>
            <artifactId>kmsartifactId>
        dependency>
        <dependency>
            <groupId>software.amazon.awssdkgroupId>
            <artifactId>s3controlartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.logging.log4jgroupId>
            <artifactId>log4j-apiartifactId>
            <version>2.20.0version>
        dependency>
    dependencies>

<build>
    <plugins>
        
        <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-compiler-pluginartifactId>
            <version>3.1version>
            <configuration>
                <source>${java.version}source>
                <target>${java.version}target>
            configuration>
        plugin>

        
        
        <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-shade-pluginartifactId>
            <version>3.0.0version>
            <executions>
                
                <execution>
                    <phase>packagephase>
                    <goals>
                        <goal>shadegoal>
                    goals>
                    <configuration>
                        <artifactSet>
                            <excludes>
                                <exclude>org.apache.flink:force-shadingexclude>
                                <exclude>com.google.code.findbugs:jsr305exclude>
                                <exclude>org.slf4j:*exclude>
                                <exclude>org.apache.logging.log4j:*exclude>
                            excludes>
                        artifactSet>
                        <filters>
                            <filter>
                                
                                <artifact>*:*artifact>
                                <excludes>
                                    <exclude>META-INF/*.SFexclude>
                                    <exclude>META-INF/*.DSAexclude>
                                    <exclude>META-INF/*.RSAexclude>
                                excludes>
                            filter>
                        filters>
                    configuration>
                execution>
            executions>
        plugin>

        <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-surefire-pluginartifactId>
            <version>2.22.1version>
            <configuration>
                <groups>IntegrationTestgroups>
            configuration>
        plugin>
        <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-compiler-pluginartifactId>
            <version>3.1version>
            <configuration>
                <source>${java.version}source>
                <target>${java.version}target>
            configuration>
        plugin>
        <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-surefire-pluginartifactId>
            <version>2.22.1version>
        plugin>
    plugins>
build>
  • S3Utils工具类
package gaei.cn.x5l.x5lhive2cos.utils;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ListObjectsRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsResponse;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.model.S3Object;

import java.util.ArrayList;
import java.util.List;

public class S3Utils {
    /**
     * 我是通过构造器实现s3的初始化,
     * 可以根据实际的场景和需求修改初始化的方式
     */
    private static String accessKey = "*******************";
    private static String secretKey = "**************************************";
    private static String serviceEndpoint = "192.168.1.1";
    private static String bucketName = "bucket-name";
    private static AmazonS3 s3 = AmazonS3Util();

    public static AmazonS3 AmazonS3Util() {
        ClientConfiguration config = new ClientConfiguration();
        config.setProtocol(Protocol.HTTP);
        AwsClientBuilder.EndpointConfiguration endpointConfig =
                new AwsClientBuilder.EndpointConfiguration(serviceEndpoint, Regions.CN_NORTH_1.getName());
        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
        AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
        s3 = AmazonS3Client.builder()
                .withEndpointConfiguration(endpointConfig)
                .withClientConfiguration(config)
                .withCredentials(awsCredentialsProvider)
                .disableChunkedEncoding()
                .withPathStyleAccessEnabled(true)
                .build();

        return s3;
    }

    /**
     * 获取bucket中所有文件
     * 此处我是写了一个统一的bucket,可以根据实际需要,修改为传参等方式
     */
    public static long getS3SampleDateSize(String s3PartPath) {

        long dataSize = 0L;
        List<S3ObjectSummary> objects = new ArrayList<S3ObjectSummary>();
        ObjectListing result = s3.listObjects(bucketName, s3PartPath);
        objects.addAll(result.getObjectSummaries());

        while (result.isTruncated()) {
            result = s3.listNextBatchOfObjects(result);
            objects.addAll(result.getObjectSummaries());
        }
        for (S3ObjectSummary os : objects) {
            dataSize += os.getSize();
        }

        return dataSize;
    }
}

其他参考:

中文官网:

https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/userguide/example_s3_ListObjects_section.html

github代码示例:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/

你可能感兴趣的:(Java,java,开发语言)