当前位置:   article > 正文

Java使用S3的一些操作_一、使用开发工具包通过s3进行开发

一、使用开发工具包通过s3进行开发

Java使用S3的一些操作

首先下载所需要的包

		<dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
            <version>1.11.868</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

然后就是自己封装的一些S3方法了 上完整代码

package com.idp.cmarket.aws;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
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.*;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.model.ListObjectsV2Request;
import com.amazonaws.services.s3.model.ListObjectsV2Result;

import java.io.IOException;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

/**
 * @author jiawei.zhao
 * Date: 2021/05/24 15:31
 */
@Component
public class S3Util {

	/**
	 * 禁用调用无参构造函数
	 */
	private S3Util() {

	}

	private static String accessKeyId = "";

	private static String accessKeySecret = "";

	private static String bucketName = "";

	private static String regionName = "cn-north-1";

	private static String S3Url ="https://test.s3.cn-north-1.amazonaws.com.cn/";

	/**
	 * S3初始化
	 * @return
	 */
	public static AmazonS3 amazonS3() {
		AWSCredentials awsCredentials = new BasicAWSCredentials(accessKeyId, accessKeySecret);
		AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials));
		//设置S3的地区
		builder.setRegion(regionName);
		AmazonS3 s3Client = builder.build();
		return s3Client;
	}

	/**
	 * 上传图片到S3
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static String upload(String url,String directory) throws Exception {
		AmazonS3 amazonS3Client = amazonS3();
		//将url转为MultipartFile对象
		MultipartFile file = urlToMultipartFile(url);
		//通过url获取图片名称
		String fileName = getFileName(url);
		if(StringUtils.isNotEmpty(directory)){
			fileName=directory+"/"+fileName;
		}
		ObjectMetadata objectMetadata = new ObjectMetadata();
		objectMetadata.setContentType(file.getContentType());
		objectMetadata.setContentLength(file.getSize());
		//调用S3上传文件
		PutObjectResult putObjectRequest = amazonS3Client.
				putObject(new PutObjectRequest(bucketName, fileName, file.getInputStream(), objectMetadata)
						.withCannedAcl(CannedAccessControlList.PublicRead));
		return S3Url+fileName;
	}
	/**
	 * 下载文件到本地
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static boolean downLoad(String fileName, String filePath) throws Exception{
		AmazonS3 amazonS3Client = amazonS3();
		S3Object object = amazonS3Client.getObject(new GetObjectRequest(bucketName,fileName));
		if(object!=null){
			InputStream input = null;
			FileOutputStream fileOutputStream = null;
			byte[] data = null;
			try {
				//获取文件流
				input=object.getObjectContent();
				data = new byte[input.available()];
				int len = 0;
				fileOutputStream = new FileOutputStream(filePath);
				while ((len = input.read(data)) != -1) {
					fileOutputStream.write(data, 0, len);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				if(fileOutputStream!=null){
					try {
						fileOutputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(input!=null){
					try {
						input.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return true;
	}

	/**
	 * 读取S3的Excel数据
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static S3Object readExcelDataForS3(String fileName,String directory) throws Exception{
		AmazonS3 amazonS3Client = amazonS3();
		if(StringUtils.isNotEmpty(directory)){
			fileName=directory+"/"+fileName;
		}
		return amazonS3Client.getObject(new GetObjectRequest(bucketName,fileName));
	}

	/**
	 * 移动文件
	 * @param fileName
	 * @param moverFileName
	 */
	public static void moveFile(String fileName,String moverFileName){
		try{
			AmazonS3 amazonS3Client = amazonS3();
			//目前哪个桶 目前的文件名 移动到的桶 移动过去的文件名
			CopyObjectResult copyFile = amazonS3Client.copyObject(bucketName, fileName, bucketName, moverFileName);
			//判断是否上传成功
			if(StringUtils.isNotEmpty(copyFile.getETag())){
				//将当前桶中的文件删除
				amazonS3Client.deleteObject(bucketName, fileName);
			}
		}catch (AmazonServiceException e) {
			System.err.println(e.getErrorMessage());
			System.exit(1);
		}
	}


	/**
	 * 获取文件列表
	 * @param limit
	 * @param prefix
	 * @return
	 * @throws IOException
	 */
	public static List<S3ObjectSummary> getFileListPaginator(Integer limit,String prefix) throws IOException{
		try {
			AmazonS3 amazonS3Client = amazonS3();
			ListObjectsV2Request req = new ListObjectsV2Request()
					.withBucketName(bucketName).withMaxKeys(limit).withPrefix(prefix);
			ListObjectsV2Result result;
			result = amazonS3Client.listObjectsV2(req);
//			for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
//				System.out.printf(" - %s (size: %d)\n", objectSummary.getKey(), objectSummary.getSize());
//			}
			result.setTruncated(true);
			return result.getObjectSummaries();
		} catch (AmazonServiceException e) {
			// 已经打通S3但处理失败
			e.printStackTrace();
		} catch (SdkClientException e) {
			// 链接S3失败
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * url转MultipartFile
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static MultipartFile urlToMultipartFile(String url) throws Exception {
		File file = null;
		MultipartFile multipartFile = null;
		try {
			HttpURLConnection httpUrl = (HttpURLConnection) new URL(url).openConnection();
			httpUrl.connect();
			file = inputStreamToFile(httpUrl.getInputStream(),"template.png");

			multipartFile = fileToMultipartFile(file);
			httpUrl.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return multipartFile;
	}

	/**
	 * inputStream 转 File
	 * @param ins
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public static File inputStreamToFile(InputStream ins, String name) throws Exception{
		File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
		OutputStream os = new FileOutputStream(file);
		int bytesRead;
		int len = 8192;
		byte[] buffer = new byte[len];
		while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
			os.write(buffer, 0, bytesRead);
		}
		os.close();
		ins.close();
		return file;
	}

	/**
	 * file转multipartFile
	 * @param file
	 * @return
	 */
	public static MultipartFile fileToMultipartFile(File file) {
		FileItemFactory factory = new DiskFileItemFactory(16, null);
		FileItem item=factory.createItem(file.getName(),"text/plain",true,file.getName());
		int bytesRead = 0;
		byte[] buffer = new byte[8192];
		try {
			FileInputStream fis = new FileInputStream(file);
			OutputStream os = item.getOutputStream();
			while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
			os.close();
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new CommonsMultipartFile(item);
	}

	/**
	 * 根据正则获取HC图片名称
	 * @param url
	 * @return
	 */
	public static String getFileName(String url){
		//指定图片后缀名称
		String suffixes="bmp|jpg|jpeg|png|gif|BMP|JPG|JPEG|PNG|GIF";
		Pattern pat=Pattern.compile("[\\w]+[\\.]("+suffixes+")");//正则判断
		Matcher mc=pat.matcher(url);//条件匹配
		while(mc.find()) {
			String fileName= mc.group();
			return fileName;
		}
		return url;
	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287

以上就是Java操作S3的一些方法
转载请注明地址~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/706920
推荐阅读
相关标签
  

闽ICP备14008679号