java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java 亚马逊云储存

在java上使用亚马逊云储存方法

作者:js1029

这篇文章主要介绍了在java上使用亚马逊云储存方法,首先写一个配置类,写一个controller接口调用方法存储文件,本文结合示例代码给大家介绍的非常详细,需要的朋友参考下吧

在java上使用亚马逊云储存方法

1.写一个配置类

package com.app.framework.config;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AmazonS3Config {
    @Bean
    public AmazonS3 amazonS3Client() {
        //用户名
        String accessKey = "";
        //密码
        String secretKey = "";
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(credentials);
        ClientConfiguration config = new ClientConfiguration();
        String region = "wx-pbc";
        AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration("cos.wx-pbc.cos.tg.unicom.local", region);
        return AmazonS3ClientBuilder.standard()
        .withCredentials(awsStaticCredentialsProvider)
        .withClientConfiguration(config.withProtocol(Protocol.HTTP).withSignerOverride("S3SignerType"))
        .withEndpointConfiguration(endpointConfiguration).build();
    }
}

2.写一个controller接口调用方法存储文件

package com.app.project.welfare.controller;
import cn.hutool.core.lang.UUID;
import com.aliyun.oss.*;
import com.aliyun.oss.model.PutObjectResult;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
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;
import com.app.framework.web.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@RequestMapping("/oss")
@RestController
@Slf4j
public class OssController {
    /**
     * 把文件上传到自己的服务器,然后在上传到 Amazon s3存储器
     */
    @PostMapping("/upload-img-s3")
    public AjaxResult uploadImgAmazonS3(@RequestParam("file") MultipartFile file) throws IOException {
        log.info("文件上传:{}", file);
        //生成一个随机的UUID(通用唯一标识符)
        UUID uuid = UUID.fastUUID();
        //获取上传文件的原始文件名,并将其存储在变量fileName中
        String fileName = file.getOriginalFilename();
        //使用断言确保fileName不为空,如果为空则抛出异常
        assert fileName != null;
        //将文件名按照点号(".")分割成一个字符串数组
        String[] fileNameSplit = fileName.split("\\.");
        //将UUID和原始文件名中的扩展名重新拼接起来,形成新的文件名
        fileName = uuid + "." + fileNameSplit[1];
        //创建一个新的ObjectMetadata对象
        ObjectMetadata objectMetadata = new ObjectMetadata();
        //获取上传文件的输入流,并使用available()方法来获取该流中可用的字节数
        objectMetadata.setContentLength(file.getInputStream().available());
        //获取上传文件的内容类型(MIME类型)
        String contentType = file.getContentType();
        //将获取到的文件内容类型设置为新文件的元数据内容类型
        objectMetadata.setContentType(contentType);
        // 设置公共读
        objectMetadata.setHeader("x-amz-acl", "public-read");
        //拼接
        String finalFileName = "name/" + fileName;
        //将文件上传到名为"name"的S3存储桶中
        amazonS3.putObject("name", finalFileName, file.getInputStream(), objectMetadata);
        String imgUrl = "https://xxx" + finalFileName;
        return AjaxResult.success(imgUrl);
    }
}

这样,当前端调用后端的controller接口,就可以上传文件到亚马逊oss储存地址了

到此这篇关于在java上使用亚马逊云储存方法的文章就介绍到这了,更多相关java 亚马逊云储存内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

阅读全文