AWS 亞馬遜S3檔案上傳
LZ看了半天文件全是英文翻譯還有語法錯誤,這對英文水平低下的我來說不能容忍,在網上也找了很久都沒有實用的資料
所以自己琢磨了一下就弄個演示看看吧..
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
* Created by Administrator on 2018/7/5
*/
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.*;
import com.amazonaws.util.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class S3Util {
public static AWSCredentials awsCredentials;
public static String access_key = "你的key";
private static String secret_key = "你的金鑰";
private static AmazonS3 conn;
private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");//設定日期格式
/**
* 建立連線,連線S3伺服器
*/
public void creatConnect() {
if (awsCredentials == null) {
awsCredentials = new BasicAWSCredentials(access_key, secret_key);
ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.setProtocol(Protocol.HTTP);
conn = new AmazonS3Client(awsCredentials, clientConfig);
}
}
public AmazonS3 getConnect() {
return conn;
}
/**
* 獲取該連線下所有的容器資訊
* @return
*/
public List<Bucket> getBuckets() {
List<Bucket> buckets = conn.listBuckets();
return buckets;
}
public Bucket getBuketsByname(String bucketName) {
Bucket resultBucket = null;
if (bucketName.isEmpty()) {
return null;
}
List<Bucket> buckets = conn.listBuckets();
if(buckets == null){
return resultBucket;
}
for (Bucket bucket : buckets) {
if (bucketName.equals(bucket.getName())) {
resultBucket = bucket;
break;
}
}
return resultBucket;
}
/**
* 新建容器名稱
* @param bucketName
* @return
*/
public Bucket creatBucket(String bucketName) {
if (bucketName.isEmpty()) {
return null;
}
Bucket bucket = conn.createBucket(bucketName);
return bucket;
}
/**
* 獲取該容器下面的所有資訊(檔案目錄集合和檔案資訊集合)
* @param bucketName
* @return
*/
public ObjectListing getBacketObjects(String bucketName) {
if (bucketName.isEmpty()) {
return null;
}
ObjectListing objects = conn.listObjects(bucketName);
return objects;
}
/**
* 獲取某個檔案(字首路徑)下的所有資訊
* @param bucketName
* @param prefix
* @param isDelimiter
* @return
*/
public ObjectListing getBacketObjects(String bucketName, String prefix,Boolean isDelimiter ) {
if ( bucketName == null || bucketName.isEmpty()) {
return null;
}
ListObjectsRequest objectsRequest = new ListObjectsRequest().withBucketName(bucketName);
if (prefix != null && !prefix.isEmpty()) {
objectsRequest = objectsRequest.withPrefix(prefix);
}
if(isDelimiter){
objectsRequest = objectsRequest.withDelimiter("/");
}
ObjectListing objects = conn.listObjects(objectsRequest);
return objects;
}
/**
* 獲取當前容器下面的目錄集合
* @param objects
* @return
*/
/* public List<StorageObjectVo> getDirectList(ObjectListing objects) {
List<StorageObjectVo> diectList = new ArrayList<StorageObjectVo>();
String prefix = objects.getPrefix();
do {
List<String> commomprefix = objects.getCommonPrefixes();
for (String comp : commomprefix) {
StorageObjectVo dirStorageObjectVo = new StorageObjectVo();
String dirName = comp.substring(prefix == null?0:prefix.length(), comp.length()-1);
dirStorageObjectVo.setName(dirName);
dirStorageObjectVo.setType("資料夾");
diectList.add(dirStorageObjectVo);
}
objects = conn.listNextBatchOfObjects(objects);
} while (objects.isTruncated());
return diectList;
} */
/**
* 獲取當前容器下面的檔案集合
* @param objects
* @return
*/
/* public List<StorageObjectVo> getFileList(ObjectListing objects) {
List<StorageObjectVo> fileList = new ArrayList<StorageObjectVo>();
String prefix = objects.getPrefix();
do {
for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
System.out.println(objectSummary.getKey() + "\t" + objectSummary.getSize() + "\t" + StringUtils.fromDate(objectSummary.getLastModified()));
if(prefix!= null && objectSummary.getKey().equals(prefix.trim())){
continue;
}
StorageObjectVo fileStorageObjectVo = new StorageObjectVo();
String fileName = objectSummary.getKey().substring(prefix == null?0:prefix.length(), objectSummary.getKey().length());
// fileStorageObjectVo.setName(objectSummary.getKey());
fileStorageObjectVo.setName(fileName);
fileStorageObjectVo.setType("檔案");
fileStorageObjectVo.setSize(bytes2kb(objectSummary.getSize()));
fileStorageObjectVo.setDate(df.format(objectSummary.getLastModified()));
fileList.add(fileStorageObjectVo);
}
objects = conn.listNextBatchOfObjects(objects);
} while (objects.isTruncated());
return fileList;
}
public List<StorageObjectVo> getObjectList(String bucketName, String prefix) {
if ( bucketName == null && bucketName.isEmpty()) {
return null;
}
ListObjectsRequest objectsRequest = new ListObjectsRequest().withBucketName(bucketName);
if (prefix!=null && !prefix.isEmpty()) {
objectsRequest = objectsRequest.withPrefix(prefix);
}
objectsRequest = objectsRequest.withDelimiter("/");
ObjectListing objects = conn.listObjects(objectsRequest);
List<StorageObjectVo> resultList = new ArrayList<StorageObjectVo>();
List<StorageObjectVo> dirList = getDirectList(objects);
if (dirList != null && dirList.size() > 0) {
resultList.addAll(dirList);
}
List<StorageObjectVo> fileList = getFileList(objects);
if (fileList != null && fileList.size() > 0) {
resultList.addAll(fileList);
}
return resultList;
} */
//建立檔案目錄
public Boolean creatpath(String bucketName,String StorageObjectVoPath, String folderName){
if(bucketName == null || folderName == null){
return false;
}
if(StorageObjectVoPath == null || StorageObjectVoPath.isEmpty()|| "null".equals(StorageObjectVoPath)){
StorageObjectVoPath ="";
}
String key = StorageObjectVoPath + folderName+"/";
ByteArrayInputStream local = new ByteArrayInputStream("".getBytes());
PutObjectResult result = conn.putObject(bucketName, key, local, new ObjectMetadata());
return true;
}
public Boolean deleteBucket(String bucketName) {
if (bucketName.isEmpty()) {
return false;
}
Bucket bucket = conn.createBucket(bucketName);
conn.deleteBucket(bucket.getName());
return true;
}
/**
*
* 上傳 檔案物件到容器
* @param bucketName
* @param StorageObjectVoPath
* @param fileName
* @param uploadFile
* @return
*/
public PutObjectResult creatObject(String bucketName,String StorageObjectVoPath, String fileName, File uploadFile) {
if(StorageObjectVoPath == null || StorageObjectVoPath.isEmpty()|| "null".equals(StorageObjectVoPath)){
StorageObjectVoPath ="";
}
if(uploadFile == null){
return null;
}
String fileAllPath = StorageObjectVoPath + fileName;
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(uploadFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
PutObjectResult result = conn.putObject(bucketName, fileAllPath, inputStream, new ObjectMetadata());
return result;
}
public void changeOBjectACL(String bucketName, String ObjectName, CannedAccessControlList aceess) {
conn.setObjectAcl(bucketName, ObjectName, aceess);
}
public ObjectMetadata download(String bucketName, String objectName, File destinationFile) {
if (bucketName.isEmpty() || objectName.isEmpty()) {
return null;
}
ObjectMetadata result = conn.getObject(new GetObjectRequest(bucketName, objectName), destinationFile);
return result;
}
public S3Object download(String bucketName, String objectName) {
if (bucketName.isEmpty() || objectName.isEmpty()) {
return null;
}
S3Object object = conn.getObject(bucketName, objectName);
return object;
}
public Boolean deleteObject(String bucketName, String objectName) {
if (bucketName.isEmpty() || objectName.isEmpty()) {
return false;
}
conn.deleteObject(bucketName, objectName);
return true;
}
/**生成檔案url
*
* @param bucketName
* @param objectName
* @return
*/
public String getDownloadUrl(String bucketName, String objectName) {
if (bucketName.isEmpty() || objectName.isEmpty()) {
return null;
}
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName);
System.out.println(conn.generatePresignedUrl(request));
return conn.generatePresignedUrl(request).toString();
}
/**
* 移動物件資訊到目標容器
* @param OrgbucketName
* @param orgKey
* @param destinationName
* @param destinationKey
* @return
*/
public Boolean moveObject(String OrgbucketName,String orgKey,String destinationName,String destinationKey){
CopyObjectResult result=conn.copyObject(OrgbucketName, orgKey, destinationName, destinationKey);
Boolean isDelete=deleteObject(OrgbucketName,orgKey);
if(result!=null){
return isDelete;
}
return false;
}
/**
* 移動目標資料夾資訊到目標容器
* @param objects
* @param destinationBucket
* @return
*/
public Boolean moveForder(ObjectListing objects,String destinationBucket){
String bucketName = objects.getBucketName();
do {
for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
System.out.println(objectSummary.getKey() + "\t" + objectSummary.getSize() + "\t" + StringUtils.fromDate(objectSummary.getLastModified()));
CopyObjectResult result=conn.copyObject(bucketName, objectSummary.getKey(), destinationBucket, objectSummary.getKey());
Boolean isDelete=deleteObject(bucketName, objectSummary.getKey());
}
objects = conn.listNextBatchOfObjects(objects);
} while (objects.isTruncated());
return true;
}
/**
* 刪除資料夾內容(必須先遍歷刪除資料夾內的內容)
* @param objects
* @return
*/
public Boolean deleteForder(ObjectListing objects){
String bucketName = objects.getBucketName();
do {
for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
System.out.println(objectSummary.getKey() + "\t" + objectSummary.getSize() + "\t" + StringUtils.fromDate(objectSummary.getLastModified()));
Boolean isDelete=deleteObject(bucketName, objectSummary.getKey());
}
objects = conn.listNextBatchOfObjects(objects);
} while (objects.isTruncated());
return true;
}
/**
* 將檔案大小格式轉為MB格式
* @param bytes
* @return
*/
public static String bytes2kb(long bytes) {
BigDecimal filesize = new BigDecimal(bytes);
BigDecimal megabyte = new BigDecimal(1024 * 1024);
float returnValue = filesize.divide(megabyte, 2, BigDecimal.ROUND_UP)
.floatValue();
if (returnValue > 1)
return (returnValue + "MB");
BigDecimal kilobyte = new BigDecimal(1024);
returnValue = filesize.divide(kilobyte, 2, BigDecimal.ROUND_UP)
.floatValue();
return (returnValue + "KB");
}
}
裡面這些邏輯我就不詳解了應該都能看的懂具體的演示我會傳到檔案裡有興趣的自行下載測試..
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
/**
* 圖片上傳
*/
@Api(description = "北美S3圖片上傳")
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
public class PictureUploadController {
//伺服器盤,如需新建桶,需要先將下面建立桶開啟
private static final String BucektName = "rentao";
//路徑字首
//private final static String url="https://s3.amazonaws.com/";//此地址伺服器在美國
// private final static String url="https://s3-ap-southeast-1.amazonaws.com/";//此地址伺服器在新加坡
private final static String url = "https://s3-us-west-1.amazonaws.com/";//此地址伺服器在加利福利亞北部
@ApiOperation(value = "圖片上傳", notes = "圖片上傳")
@RequestMapping(value = "/img/upload.do", method = RequestMethod.POST)
public ResponseEntity<JsonResultEntity> insert(@RequestParam(value = "picture", required = true) MultipartFile uploadFile,
@RequestParam(value = "width", defaultValue = "0", required = false) int width,
@RequestParam(value = "height", defaultValue = "0", required = false) int height) {
// 圖片名稱和字尾判斷
String fileName = uploadFile.getOriginalFilename();
//判斷是否為圖片格式 採用正規表示式
String fileType = fileName.substring(fileName.lastIndexOf("."));
if (!fileType.matches("^.*(jpg|png|gif|jpeg)$")) {
//表示不是圖片型別
return ResponseEntity.ok(JsonResultUtils.error(MessageEnum.PIC_NOT_NORM.getCode(),
MessageEnum.PIC_NOT_NORM.getMessage()));
// Result.errorReponse("上傳檔案不是正確的圖片格式,上傳出錯!");
}
//判斷是否為惡意程式
try {
BufferedImage bufferedImage = ImageIO.read(uploadFile.getInputStream());
//獲取圖片的高度和寬度
int imageWidth = bufferedImage.getWidth();
int imageHeight = bufferedImage.getHeight();
if (imageWidth == 0 || imageHeight == 0) {
return ResponseEntity.ok(JsonResultUtils.error(MessageEnum.PIC_NOT_NORM.getCode(),
MessageEnum.PIC_NOT_NORM.getMessage()));
// Result.errorReponse("上傳檔案非圖片型別,上傳出錯!");
}
} catch (IOException e1) {
e1.printStackTrace();
}
//新建兩個臨時檔案,將記憶體中的圖片放入臨時檔案中,方便圖片轉換
File f = null;
File toPic = null;
try {
//將圖片轉為File型別
f = File.createTempFile("tmp", fileType);
uploadFile.transferTo(f);
//設定圖片壓縮比例。
toPic = File.createTempFile("topic", fileType);
Thumbnails.of(f).scale(1f).outputQuality(0.25f).toFile(toPic);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.ok(JsonResultUtils.error(MessageEnum.PIC_UPLOAD_ERROR.getCode(),
MessageEnum.PIC_UPLOAD_ERROR.getMessage()));
// Result.errorReponse("上傳圖片失敗,請重新提交!");
}
String url = uploadS3File(toPic, width, height);
//刪除兩個臨時檔案
f.deleteOnExit();
toPic.deleteOnExit();
if (!url.startsWith("https")) {
return ResponseEntity.ok(JsonResultUtils.error(MessageEnum.PIC_UPLOAD_ERROR.getCode(),
MessageEnum.PIC_UPLOAD_ERROR.getMessage()));
}
return ResponseEntity.ok(
JsonResultUtils.success(url, MessageEnum.COMMON_STATUS_OK.getCode(), MessageEnum.COMMON_STATUS_OK.getMessage()));
// Result.normalResponse(url);
}
public String uploadS3File(File file, int width, int height) {
//連線遠端圖片伺服器
S3Util s3 = new S3Util();
s3.creatConnect();
String fileType = file.getName().substring(file.getName().lastIndexOf("."));
//準備路徑
Random random = new Random();
//0-999的隨機數,用於區分上傳檔案的資料夾,避免圖片上傳衝突
int randomNum = random.nextInt(999);
String StorageObjectVoPath = new SimpleDateFormat("yyyy/MM/dd/HH").format(new Date()) + "/" + randomNum + "/";
//建立網上bucket的名字,如需新建桶,需要先將此處開啟
/*if(s3.getBuketsByname(BucektName)==null) {
s3.creatBucket(BucektName);
}*/
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e1) {
e1.printStackTrace();
}
int imageWidth = image.getWidth();
int imageHeitht = image.getHeight();
//如果上傳圖片沒有長寬的值,則預設按照原圖尺寸上傳,執行下面if程式碼結束
if (width == 0 && height == 0) {
String fileName = System.currentTimeMillis() + "_" + imageWidth + "X" + imageHeitht + fileType;
s3.creatObject(BucektName, StorageObjectVoPath, fileName, file);
//代表虛擬的全路徑
String url = PictureUploadController.url + BucektName + "/" + StorageObjectVoPath + fileName;
return url;
}
//等比例縮放,等比例只需要傳一個值即可
if (width != height) {
File toPic = null;
try {
toPic = File.createTempFile("topic", fileType);
//按比例縮放
if (width == 0) {
Thumbnails.of(file).scale((double) height / imageHeitht).toFile(toPic);
} else {
Thumbnails.of(file).scale((double) width / imageWidth).toFile(toPic);
}
toPic.deleteOnExit();
image = ImageIO.read(toPic);
} catch (IOException e) {
e.printStackTrace();
}
//設定檔名
width = image.getWidth();
height = image.getHeight();
String fileName = System.currentTimeMillis() + "_" + width + "X" + height + fileType;
s3.creatObject(BucektName, StorageObjectVoPath, fileName, toPic);
//代表虛擬的全路徑
String url = PictureUploadController.url + BucektName + "/" + StorageObjectVoPath + fileName;
return url;
}
//如果長寬均有值且相等,則執行下面的程式碼上傳圖片壓縮的值
if (imageWidth < imageHeitht) {
imageHeitht = imageWidth;
} else {
imageWidth = imageHeitht;
}
File toPic = null;
try {
toPic = File.createTempFile("topic", fileType);
//擷取圖片成正方形
Thumbnails.of(file).sourceRegion(Positions.CENTER, imageWidth, imageHeitht).size(imageWidth, imageHeitht).toFile(toPic);
Thumbnails.of(toPic).scale((double) width / imageWidth).toFile(toPic);
} catch (IOException e) {
e.printStackTrace();
}
//設定檔名
String fileName = System.currentTimeMillis() + "_" + width + "X" + height + fileType;
s3.creatObject(BucektName, StorageObjectVoPath, fileName, toPic);
toPic.deleteOnExit();
//代表虛擬的全路徑
String url = PictureUploadController.url + BucektName + "/" + StorageObjectVoPath + fileName;
return url;
}
}
具體的就去演示裡面吧在檔案裡面 也歡迎大家加入交流群125950185
相關文章
- 亞馬遜雲 | AWS S3 | 基本操作亞馬遜S3
- 亞馬遜aws文件語法錯誤亞馬遜
- 連線亞馬遜AWS伺服器教程亞馬遜伺服器
- 亞馬遜雲服務(AWS)中國區域正式上線兩項全新的檔案儲存服務亞馬遜
- 關於亞馬遜AWS 棄用 Oracle的思考亞馬遜Oracle
- 亞馬遜AWS入門(一):相關資源亞馬遜
- Ant Design Upload 通過後端預生成 URL 分片上傳大檔案到 AWS S3後端S3
- 亞馬遜雲服務(AWS)宣佈Amazon Route 53在中國上線亞馬遜
- 也許我們都小看了亞馬遜AWS(中國)!亞馬遜
- 亞馬遜雲伺服器aws配置ssl https證書亞馬遜伺服器HTTP
- 亞馬遜:北美和AWS業務高速增長助亞馬遜第四季營收同比增21%亞馬遜營收
- 亞馬遜2020年Q4財報:亞馬遜雲服務(AWS)全年收入達454億美元亞馬遜
- Simple WPF: S3實現MINIO大檔案上傳並顯示上傳進度S3
- 亞馬遜AWS當機十小時,這次是人為原因亞馬遜
- 亞馬遜雲服務AWS Marketplace “重塑”企業軟體SaaS之旅亞馬遜
- TiDB Cloud 上線亞馬遜雲科技 MarketplaceTiDBCloud亞馬遜
- 亞馬遜erp_賣家如何選擇亞馬遜erp?亞馬遜
- 亞馬遜定價_亞馬遜erp產品定價策略亞馬遜
- 亞馬遜評級亞馬遜
- 亞馬遜AWS 2022年將在全球招聘2.5萬名員工HVQP亞馬遜
- 如何給亞馬遜買家傳送站內信?亞馬遜
- 埃裡森炮轟亞馬遜AWS不安全,釋出Oracle雲2.0亞馬遜Oracle
- 亞馬遜雲服務(AWS)的“伺服器型號“已近400種亞馬遜伺服器
- 亞馬遜雲服務(AWS)在中國推出ISV全新加速贏計劃亞馬遜
- 亞馬遜跨境電商亞馬遜
- 亞馬遜 Redshift 死了嗎?亞馬遜
- 亞馬遜PSE認證亞馬遜
- 單個檔案上傳和批量檔案上傳
- 檔案上傳
- 亞馬遜雲服務(AWS)加快雲產品和服務落地中國的速度亞馬遜
- 亞馬遜雲服務(AWS)全面推動機器學習創新應用亞馬遜機器學習
- 亞馬遜最新財報:營收724億美元,AWS利潤高達22億亞馬遜營收
- Java大檔案上傳、分片上傳、多檔案上傳、斷點續傳、上傳檔案minio、分片上傳minio等解決方案Java斷點
- 亞馬遜 re:Invent 2020觀察一:雲資料庫挑戰傳統IT體系 AWS迎來更大市場亞馬遜資料庫
- 亞馬遜取消《指環王》MMO網遊專案亞馬遜
- AWS CLI 實現 S3與EC2例項間檔案複製S3
- 亞馬遜高管加入Coinbase亞馬遜
- 亞馬遜DRKG使用體驗亞馬遜