亚马逊 AmazonS3文件上传

首先导入依赖
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-s3</artifactId>
    <version>1.11.837</version>
</dependency>
主要配置声明
    //密匙
    private static final String ACCESS_KEY = "*";
    //私钥
    private static final String SECRET_KEY = "*";
    //储存桶的名称
    private static final String BUCKET_NAME = "*";
    //所属地区
    private static final String REGION = "*";
    //域名地址
    private static final String HUR = "*";
    private static final BasicAWSCredentials AWS_CREDS = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);

    private static final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
            .withCredentials(new AWSStaticCredentialsProvider(AWS_CREDS))
            //设置服务器所属地区
            .withRegion(REGION)
            .build();
主要代码
    /**
     * 
     * @param file  文件
     * @return
     */
   public JsonObject adds3ShrinkImg(MultipartFile file) {
        // 文件后缀
        String fileNameSuffix = getSuffix(Objects.requireNonNull(file.getOriginalFilename()));
        String url = JsonUtil.toJson(fileSuffix);
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType(file.getContentType());
        objectMetadata.setContentLength(file.getSize());
		//文件夹
        String folder = "test/";
		//生成新的文件名
        String img = folder + randomName() + "." + fileNameSuffix;
        try {
            //开始上传文件
            s3.putObject(new PutObjectRequest(BUCKET_NAME, img, file.getInputStream(), objectMetadata));

				//压缩操作
                //下载原图
                S3Object s3object = s3.getObject(BUCKET_NAME, img);
                S3ObjectInputStream inputStream = s3object.getObjectContent();
                BufferedImage originalImage = ImageIO.read(inputStream.getDelegateStream());
                int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
                int width = originalImage.getWidth();
                int height = originalImage.getHeight();
                // 缩略图宽度
                int newWidth = 215;
                // 缩略图高度
                int newHeight = (int) Math.round(height * newWidth / (double) width);
                BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, type);
                Graphics2D g = resizedImage.createGraphics();
                g.drawImage(originalImage, 0, 0, newWidth, newHeight, null);
                g.dispose();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ImageIO.write(resizedImage, fileNameSuffix, baos);
                byte[] bytes = baos.toByteArray();
                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
                // 上传缩略图
                ObjectMetadata metadata = new ObjectMetadata();
                metadata.setContentLength(bytes.length);
                metadata.setContentType(file.getContentType());
				//生成压缩后文件名
                String originalIma = folder + randomName()+newWidth+"."+fileNameSuffix;
				//开始上传文件
                s3.putObject(new PutObjectRequest(BUCKET_NAME, originalIma, bais, metadata));
           
        } catch (Exception e) {
            e.printStackTrace();
        }
        JsonObject objoect = new JsonObject();
        objoect.addProperty("url",HUR + img);
        return objoect;
    }
	
	/**
     * 生成随机数
     * @return
     */
	    private static String randomName() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = sdf.format(new Date());
        int i = (int) ((Math.random() * 9 + 1) * 10000);
        return date + i;
    }
	
	/**
     * 获取文件后缀
     *
     * @param fileName
     * @return
     */
    public static String getSuffix(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值