在java上使用亞馬遜云儲存方法
更新時間:2024年01月30日 10:39:14 作者:js1029
這篇文章主要介紹了在java上使用亞馬遜云儲存方法,首先寫一個配置類,寫一個controller接口調(diào)用方法存儲文件,本文結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友參考下吧
在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接口調(diào)用方法存儲文件
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存儲器
*/
@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;
//將文件名按照點號(".")分割成一個字符串?dāng)?shù)組
String[] fileNameSplit = fileName.split("\\.");
//將UUID和原始文件名中的擴展名重新拼接起來,形成新的文件名
fileName = uuid + "." + fileNameSplit[1];
//創(chuàng)建一個新的ObjectMetadata對象
ObjectMetadata objectMetadata = new ObjectMetadata();
//獲取上傳文件的輸入流,并使用available()方法來獲取該流中可用的字節(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存儲桶中
amazonS3.putObject("name", finalFileName, file.getInputStream(), objectMetadata);
String imgUrl = "https://xxx" + finalFileName;
return AjaxResult.success(imgUrl);
}
}這樣,當(dāng)前端調(diào)用后端的controller接口,就可以上傳文件到亞馬遜oss儲存地址了
到此這篇關(guān)于在java上使用亞馬遜云儲存方法的文章就介紹到這了,更多相關(guān)java 亞馬遜云儲存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis自動生成時如何設(shè)置不生成Example類詳解
這篇文章主要給大家介紹了關(guān)于mybatis自動生成時如何設(shè)置不生成Example類的相關(guān)資料,文中介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-05-05
springboot結(jié)合redis實現(xiàn)搜索欄熱搜功能及文字過濾
本文主要介紹了springboot結(jié)合redis實現(xiàn)搜索欄熱搜功能及文字過濾,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
通過spring boot 設(shè)置tomcat解決 post參數(shù)限制問題
這篇文章主要介紹了通過spring boot 設(shè)置tomcat解決 post參數(shù)限制問題,需要的朋友可以參考下2019-05-05
詳解在spring boot中配置多個DispatcherServlet
本篇文章主要介紹了詳解在spring boot中配置多個DispatcherServlet,具有一定的參考價值,有興趣的可以了解一下。2017-03-03

