最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在java上使用亞馬遜云儲(chǔ)存方法

 更新時(shí)間:2024年01月30日 10:39:14   作者:js1029  
這篇文章主要介紹了在java上使用亞馬遜云儲(chǔ)存方法,首先寫(xiě)一個(gè)配置類,寫(xiě)一個(gè)controller接口調(diào)用方法存儲(chǔ)文件,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧

在java上使用亞馬遜云儲(chǔ)存方法

1.寫(xiě)一個(gè)配置類

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.寫(xiě)一個(gè)controller接口調(diào)用方法存儲(chǔ)文件

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 {
    /**
     * 把文件上傳到自己的服務(wù)器,然后在上傳到 Amazon s3存儲(chǔ)器
     */
    @PostMapping("/upload-img-s3")
    public AjaxResult uploadImgAmazonS3(@RequestParam("file") MultipartFile file) throws IOException {
        log.info("文件上傳:{}", file);
        //生成一個(gè)隨機(jī)的UUID(通用唯一標(biāo)識(shí)符)
        UUID uuid = UUID.fastUUID();
        //獲取上傳文件的原始文件名,并將其存儲(chǔ)在變量fileName中
        String fileName = file.getOriginalFilename();
        //使用斷言確保fileName不為空,如果為空則拋出異常
        assert fileName != null;
        //將文件名按照點(diǎn)號(hào)(".")分割成一個(gè)字符串?dāng)?shù)組
        String[] fileNameSplit = fileName.split("\\.");
        //將UUID和原始文件名中的擴(kuò)展名重新拼接起來(lái),形成新的文件名
        fileName = uuid + "." + fileNameSplit[1];
        //創(chuàng)建一個(gè)新的ObjectMetadata對(duì)象
        ObjectMetadata objectMetadata = new ObjectMetadata();
        //獲取上傳文件的輸入流,并使用available()方法來(lái)獲取該流中可用的字節(jié)數(shù)
        objectMetadata.setContentLength(file.getInputStream().available());
        //獲取上傳文件的內(nèi)容類型(MIME類型)
        String contentType = file.getContentType();
        //將獲取到的文件內(nèi)容類型設(shè)置為新文件的元數(shù)據(jù)內(nèi)容類型
        objectMetadata.setContentType(contentType);
        // 設(shè)置公共讀
        objectMetadata.setHeader("x-amz-acl", "public-read");
        //拼接
        String finalFileName = "name/" + fileName;
        //將文件上傳到名為"name"的S3存儲(chǔ)桶中
        amazonS3.putObject("name", finalFileName, file.getInputStream(), objectMetadata);
        String imgUrl = "https://xxx" + finalFileName;
        return AjaxResult.success(imgUrl);
    }
}

這樣,當(dāng)前端調(diào)用后端的controller接口,就可以上傳文件到亞馬遜oss儲(chǔ)存地址了

到此這篇關(guān)于在java上使用亞馬遜云儲(chǔ)存方法的文章就介紹到這了,更多相關(guān)java 亞馬遜云儲(chǔ)存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringTask-Timer實(shí)現(xiàn)定時(shí)任務(wù)的詳細(xì)代碼

    SpringTask-Timer實(shí)現(xiàn)定時(shí)任務(wù)的詳細(xì)代碼

    在項(xiàng)目中開(kāi)發(fā)定時(shí)任務(wù)應(yīng)該一種比較常見(jiàn)的需求,今天通過(guò)示例代碼給大家講解SpringTask-Timer實(shí)現(xiàn)定時(shí)任務(wù)的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2024-06-06
  • Mybatis pagehelper分頁(yè)插件使用過(guò)程解析

    Mybatis pagehelper分頁(yè)插件使用過(guò)程解析

    這篇文章主要介紹了mybatis pagehelper分頁(yè)插件使用過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • SpringBoot接收J(rèn)SON類型的參數(shù)方式

    SpringBoot接收J(rèn)SON類型的參數(shù)方式

    這篇文章主要介紹了SpringBoot接收J(rèn)SON類型的參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • 用JAVA實(shí)現(xiàn)單鏈表,檢測(cè)字符串是否是回文串

    用JAVA實(shí)現(xiàn)單鏈表,檢測(cè)字符串是否是回文串

    這篇文章主要介紹了使用JAVA實(shí)現(xiàn)單鏈表,檢測(cè)字符串是否是回文串,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-11-11
  • SpringBoot2.1 RESTful API項(xiàng)目腳手架(種子)項(xiàng)目

    SpringBoot2.1 RESTful API項(xiàng)目腳手架(種子)項(xiàng)目

    這篇文章主要介紹了SpringBoot2.1 RESTful API項(xiàng)目腳手架(種子)項(xiàng)目,用于搭建RESTful API工程的腳手架,只需三分鐘你就可以開(kāi)始編寫(xiě)業(yè)務(wù)代碼,不再煩惱于構(gòu)建項(xiàng)目與風(fēng)格統(tǒng)一,感興趣的小伙伴們可以參考一下
    2018-12-12
  • mybatis如何設(shè)置useGeneratedKeys=true

    mybatis如何設(shè)置useGeneratedKeys=true

    這篇文章主要介紹了mybatis如何設(shè)置useGeneratedKeys=true,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2022-01-01
  • springboot如何配置多kafka

    springboot如何配置多kafka

    這篇文章主要介紹了springboot如何配置多kafka問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java定義泛型方法實(shí)例分析

    Java定義泛型方法實(shí)例分析

    這篇文章主要介紹了Java定義泛型方法,結(jié)合實(shí)例形式分析了java定義泛型的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-07-07
  • Java設(shè)計(jì)模式之備忘錄模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院

    Java設(shè)計(jì)模式之備忘錄模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院

    我們?cè)诰幊痰臅r(shí)候,經(jīng)常需要保存對(duì)象的中間狀態(tài),當(dāng)需要的時(shí)候,可以恢復(fù)到這個(gè)狀態(tài)。接下來(lái)通過(guò)本文給大家分享java設(shè)計(jì)模式之備忘錄模式,感興趣的的朋友一起看看吧
    2017-08-08
  • Spring Boot核心注解@ResponseBody深度解析與實(shí)戰(zhàn)指南

    Spring Boot核心注解@ResponseBody深度解析與實(shí)戰(zhàn)指南

    SpringBoot中@ResponseBody注解詳解,全面拆解其核心作用、工作原理、實(shí)戰(zhàn)示例、簡(jiǎn)化用法及注意事項(xiàng),本文介紹Spring Boot核心注解@ResponseBody深度解析與實(shí)戰(zhàn)指南,感興趣的朋友跟隨小編一起看看吧
    2026-01-01

最新評(píng)論

大城县| 兴山县| 钟祥市| 大厂| 增城市| 铜山县| 阿鲁科尔沁旗| 石棉县| 怀安县| 吉林省| 香港 | 大洼县| 亳州市| 休宁县| 栾川县| 永和县| 泽库县| 曲麻莱县| 吉林省| 珲春市| 连州市| 上杭县| 府谷县| 石林| 呼和浩特市| 桐乡市| 平泉县| 上蔡县| 西林县| 府谷县| 铜陵市| 固阳县| 祁阳县| 西乌| 信阳市| 大埔县| 桐柏县| 肃宁县| 泾川县| 红原县| 区。|