亚马逊S3 OSS 上传文件并设置跨域

本文介绍了如何通过Java配置AWS SDK,实现文件上传到S3存储服务。首先,展示了Maven项目的依赖配置,包括必要的Amazon S3和S3 Transfer Manager库。接着,详细说明了Java代码中设置AWS凭据、指定桶名称和区域,以及执行文件上传的过程,同时涉及CORS规则的设置。最后,提供了参考资料链接。
摘要由CSDN通过智能技术生成

不多说,全是干货,上菜。

1. Maven引入

          我这里因为有其他的模块,所以依赖都引入了,你们可以根据自身需要,酌情删减依赖。

    <dependencies>       
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>s3</artifactId>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>s3-transfer-manager</artifactId>
            <version>2.17.103-PREVIEW</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>s3control</artifactId>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-sts</artifactId>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>software.amazon.awssdk</groupId>
                <artifactId>bom</artifactId>
                <version>2.16.60</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-java-sdk-bom</artifactId>
                <version>1.11.837</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>

    </dependencyManagement>

2. java 代码配置

       需要注意的是,我是使用的配置的方式输入秘钥和key ,别忘了填写桶名称和配置桶的区域,且一定要对应。

 public static void uploadDirectory(MultipartFile[] files) {
        List<CORSRule.AllowedMethods> rule1AM = new ArrayList<CORSRule.AllowedMethods>();
        rule1AM.add(CORSRule.AllowedMethods.PUT);
        rule1AM.add(CORSRule.AllowedMethods.POST);
        rule1AM.add(CORSRule.AllowedMethods.DELETE);

        List<CORSRule.AllowedMethods> rule2AM = new ArrayList<CORSRule.AllowedMethods>();
        rule2AM.add(CORSRule.AllowedMethods.GET);
        rule2AM.add(CORSRule.AllowedMethods.POST);
        rule2AM.add(CORSRule.AllowedMethods.PUT);
        CORSRule rule2 = new CORSRule().withId("CORSRule2")
                .withMaxAgeSeconds(3000)
                .withAllowedMethods(rule2AM)
                .withAllowedOrigins(Arrays.asList("*"))
                .withAllowedHeaders(Arrays.asList("*"));

        List<CORSRule> rules = new ArrayList<CORSRule>();
        //rules.add(new CORSRule().withId("CORSRule1").withAllowedMethods(rule1AM).withAllowedOrigins(Arrays.asList("https://*.bragaming.com")));
        rules.add(rule2);

        // Add the rules to a new CORS configuration.
        BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration();
        configuration.setRules(rules);


        AWSCredentials awsCredentials = new BasicAWSCredentials("accessKey", "secretKet");
        AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials));
        String bucketName = "桶名称";
        builder.setRegion("区域");
        AmazonS3 s3Client = builder.build();
        //设置跨域
        s3Client.setBucketCrossOriginConfiguration(bucketName, configuration);
        for (MultipartFile file : files) {
            try {
                ObjectMetadata objectMetadata = new ObjectMetadata();
                objectMetadata.setContentType(file.getContentType());
                objectMetadata.setContentLength(file.getSize());
                String key = file.getName();
                InputStream inputStream = file.getInputStream();
                PutObjectRequest request = new PutObjectRequest(bucketName, key, inputStream, objectMetadata);
                s3Client.putObject(request.withCannedAcl(CannedAccessControlList.PublicRead));
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
        }
    }

以上就是完整配置了,Over。

资料参考 MB博客 - 每日分享精彩资源

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值