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

SpringBoot整合aws的示例代碼

 更新時間:2021年12月01日 16:43:01   作者:一個不會代碼的搬磚人  
本文通過實例代碼給大家介紹SpringBoot整合aws的全過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧

業(yè)務(wù)需求

  • 將本地的一些文件保存到aws上
  • 引入依賴
  • 創(chuàng)建client
  • 工具類

引入依賴

      <dependency>
           <groupId>software.amazon.awssdk</groupId>
           <artifactId>s3</artifactId>
       </dependency>
       
         <dependency>
           <groupId>com.amazonaws</groupId>
           <artifactId>aws-java-sdk-s3</artifactId>
       </dependency>
       
       <dependency>
           <groupId>com.amazonaws</groupId>
           <artifactId>aws-java-sdk-sqs</artifactId>
       </dependency>
       
       <dependency>
           <groupId>software.amazon.awssdk</groupId>
           <artifactId>sns</artifactId>
       </dependency>
       
       <dependency>
           <groupId>com.amazonaws</groupId>
           <artifactId>aws-java-sdk-cloudfront</artifactId>
       </dependency>

創(chuàng)建client

 private S3Client createClient() {
       AwsCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create(minIoAccessKey, minIoSecretKey));

       S3Client s3 = S3Client.builder()
               .region(Region.CN_NORTHWEST_1)
               .credentialsProvider(credentialsProvider)
               .endpointOverride(URI.create(minIoUrl))
               .build();

       return s3;
   }

aws工具類

public class MinioOperate  {


   private String minIoAccessKey;
   private String minIoSecretKey;
   private String minIoUrl;

   public MinioOperate() {
   }

   public MinioOperate(String minIoAccessKey, String minIoSecretKey, String minIoUrl) {
       this.minIoAccessKey = minIoAccessKey;
       this.minIoSecretKey = minIoSecretKey;
       this.minIoUrl = minIoUrl;
   }

#創(chuàng)建aws的客戶端
   private S3Client createClient() {
       AwsCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create(minIoAccessKey, minIoSecretKey));

       S3Client s3 = S3Client.builder()
               .region(Region.CN_NORTHWEST_1)
               .credentialsProvider(credentialsProvider)
               .endpointOverride(URI.create(minIoUrl))
               .build();

       return s3;
   }
//    public String generatePresignedUrl(String bucketName, String objectKey, String acl) {
//        URL url = null;
//        try {
//            AWSStaticCredentialsProvider credentialsProvider =
//                    new AWSStaticCredentialsProvider(
//                            new BasicAWSCredentials(minIoAccessKey, minIoSecretKey));
//
//            AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();
//            builder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(minIoUrl, Regions.CN_NORTHWEST_1.getName()));
//
//            AmazonS3 s3Client = builder
//                    .withPathStyleAccessEnabled(true)
//                    .withCredentials(credentialsProvider)
//                    .build();
//
//            // Set the presigned URL to expire after one hour.
//            Date expiration = new Date();
//            long expTimeMillis = expiration.getTime();
//            expTimeMillis += 1000 * 60 * 60 * 4;
//            expiration.setTime(expTimeMillis);
//
//            // Generate the presigned URL.
//            GeneratePresignedUrlRequest generatePresignedUrlRequest =
//                    new GeneratePresignedUrlRequest(bucketName, objectKey)
//                            .withMethod(HttpMethod.GET)
//                            .withExpiration(expiration);
//
//            // set acl
//            if (!StringUtils.isEmpty(acl)) {
//                generatePresignedUrlRequest.withMethod(HttpMethod.PUT);
//                generatePresignedUrlRequest.addRequestParameter(Headers.S3_CANNED_ACL, acl);
//            }
//
//            url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
//
//        } catch (AmazonServiceException e) {
//            // The call was transmitted successfully, but Amazon S3 couldn't process
//            // it, so it returned an error response.
//            e.printStackTrace();
//        } catch (SdkClientException e) {
//            // Amazon S3 couldn't be contacted for a response, or the client
//            // couldn't parse the response from Amazon S3.
//            e.printStackTrace();
//        }
//
//        if (StringUtils.isEmpty(url)) {
//            return null;
//        }
//        return url.toString();
//    }
  

#獲取所有的對象(根據(jù)桶和前綴)
   public List<S3Object> ListObjects(String bucket, String prefix) {
   S3Client s3Client = this.createClient();
    List<S3Object> contents = null;
       try {
           ListObjectsV2Request request = ListObjectsV2Request.builder().bucket(bucket).prefix(prefix).build();
           ListObjectsV2Response listObjectsV2Response = s3Client.listObjectsV2(request);
           contents = listObjectsV2Response.contents();
       } finally {
           this.closeClient(s3Client);
       }
       return contents;
   }
  
#上傳對象
   public void putObject(String bucket, String key, String content) {
       S3Client s3Client = this.createClient();
        try {
           ByteBuffer byteBuffer = ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8));
           PutObjectResponse putObjectResponse =
                   s3.putObject(PutObjectRequest.builder().bucket(bucket).key(key)
                                   .build(),
                           RequestBody.fromByteBuffer(byteBuffer));
       } finally {
           this.closeClient(s3);
       }
   }
#上傳文件
   public void putFile(String filePath, String key, String bucket) {
       S3Client s3Client = this.createClient();
       File tempFile = new File(filePath);
       try {
           PutObjectResponse putObjectResponse =
                   s3Client.putObject(PutObjectRequest.builder().bucket(bucket).key(key)
                                   .build(),
                           RequestBody.fromFile(tempFile));
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           this.closeClient(s3Client);
       }
   }

#獲取對象大小
   @Override
   public long getS3ObjectLength(String bucket, String key) {
    S3Client s3Client = this.createClient();
       long size = 0;
       try {
           List<S3Object> s3Objects = this.ListObjects(s3, bucket, key);
           size = s3Objects.get(0).size();
       }catch (Exception e){
           e.printStackTrace();
       }finally {
           this.closeClient(s3);
       }
       return size;
   }

   # 獲取對象
   public String getObject(String bucket, String key) {
        S3Client s3Client = this.createClient();
        try {
           ResponseBytes<GetObjectResponse> responseResponseBytes = s3.getObject(GetObjectRequest.builder().bucket(bucket).key(key).build(),
                   ResponseTransformer.toBytes());         # ResponseTransformer.toBytes()	將響應(yīng)轉(zhuǎn)換為二進制流
           return this.decode(responseResponseBytes.asByteBuffer());
       } finally {
           this.closeClient(s3);
       }
   }

# 獲取對象的流
   @Override
   public InputStream getInputStream(String bucket, String key) {
       S3Client s3Client = this.createClient();
       try {
           ResponseBytes<GetObjectResponse> responseResponseBytes = s3.getObject(GetObjectRequest.builder().bucket(bucket).key(key).build(),
                   ResponseTransformer.toBytes());
           return responseResponseBytes.asInputStream();
       } finally {
           this.closeClient(s3);
       }
   }

#刪除對象
   public void deleteObject(String bucket, String key) {
    S3Client s3Client = this.createClient();
     try {
           DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucket).key(key).build();
           DeleteObjectResponse deleteObjectResponse = s3.deleteObject(deleteObjectRequest);
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           this.closeClient(s3);
       }
   }
#批量刪除對象
   public void deleteObjects(List<String> buckets, String key) {
        S3Client s3Client = this.createClient();
        try {
           String prefix = key.substring(0, key.lastIndexOf(File.separator));
           for (String bucket : buckets) {
               ListObjectsRequest listObjectsRequest = ListObjectsRequest.builder().bucket(bucket).prefix(prefix).build();
               ListObjectsResponse listObjectsResponse = s3.listObjects(listObjectsRequest);
               List<S3Object> contents = listObjectsResponse.contents();
               for (S3Object content : contents) {
                   String objectKey = content.key();
                   this.deleteObject(s3, bucket, objectKey);
               }
               this.deleteObject(s3, bucket, prefix);
           }
       } finally {
           this.closeClient(s3);
       }
   }

}

 public String decode(ByteBuffer byteBuffer) {
       Charset charset = StandardCharsets.UTF_8;
       return charset.decode(byteBuffer).toString();
   }

其中 minIoAccessKey,minIoSecretKey, minIoUrl;分別對應(yīng)賬號、密碼、請求地址。需要對應(yīng)自己的相關(guān)信息。

到此這篇關(guān)于SpringBoot整合aws的文章就介紹到這了,更多相關(guān)SpringBoot整合aws內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot整合EasyExcel實現(xiàn)文件導(dǎo)入導(dǎo)出

    SpringBoot整合EasyExcel實現(xiàn)文件導(dǎo)入導(dǎo)出

    這篇文章主要介紹了SpringBoot整合EasyExcel實現(xiàn)文件導(dǎo)入導(dǎo)出的方法,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot,感興趣的朋友可以了解下
    2021-05-05
  • Java實現(xiàn)定時任務(wù)的方法詳解

    Java實現(xiàn)定時任務(wù)的方法詳解

    大家都用過鬧鐘,鬧鐘可以說是一種定時任務(wù)。那么,在?Java?中,如何實現(xiàn)這樣的功能呢?即如何實現(xiàn)定時任務(wù)呢?本文就來詳細(xì)和大家聊聊
    2022-10-10
  • java 中平方根(sqrt)算法 的實例詳解

    java 中平方根(sqrt)算法 的實例詳解

    這篇文章主要介紹了java 中平方根(sqrt)算法 的實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • java顯示當(dāng)前運行時的參數(shù)(java運行參數(shù))

    java顯示當(dāng)前運行時的參數(shù)(java運行參數(shù))

    這篇文章主要介紹了java顯示當(dāng)前運行時參數(shù)的示例(java運行參數(shù)),需要的朋友可以參考下
    2014-04-04
  • java中重寫equals和重寫hashCode()

    java中重寫equals和重寫hashCode()

    這篇文章主要介紹了java中重寫equals和重寫hashCode()的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Java并行處理的實現(xiàn)

    Java并行處理的實現(xiàn)

    并行計算一般是指許多指令得以同時進行的計算模式。本文主要介紹了Java并行處理的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • java多線程之Phaser的使用詳解

    java多線程之Phaser的使用詳解

    這篇文章主要介紹了java多線程之Phaser的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Java 中實現(xiàn)隨機無重復(fù)數(shù)字的方法

    Java 中實現(xiàn)隨機無重復(fù)數(shù)字的方法

    為了更好地理解這個題意,我們先來看下具體內(nèi)容:生成一個1-100 的隨機數(shù)組,但數(shù)組中的數(shù)字不能重復(fù),即位置是隨機的,但數(shù)組元素不能重復(fù)
    2013-03-03
  • java實現(xiàn)本地日期時間處理

    java實現(xiàn)本地日期時間處理

    這篇文章主要介紹了本地日期時間處理的程序,實現(xiàn)了下面的功能,大家參考使用吧
    2014-01-01
  • Java加解密工具類源碼示例

    Java加解密工具類源碼示例

    最近在項目中接觸到了數(shù)據(jù)加解密的業(yè)務(wù),數(shù)據(jù)加密技術(shù)是網(wǎng)絡(luò)中最基本的安全技術(shù),這篇文章主要給大家介紹了關(guān)于Java加解密工具類源碼的相關(guān)資料,需要的朋友可以參考下
    2023-11-11

最新評論

阳新县| 灌南县| 和顺县| 满洲里市| 常山县| 金山区| 沧州市| 仲巴县| 遵义县| 罗定市| 邢台市| 竹山县| 康定县| 额尔古纳市| 蛟河市| 北宁市| 龙山县| 宁陕县| 龙江县| 治县。| 兴安县| 呼图壁县| 方正县| 独山县| 闻喜县| 涿鹿县| 奉化市| 临沂市| 靖西县| 西宁市| 比如县| 紫金县| 登封市| 新巴尔虎左旗| 阿克陶县| 五河县| 湖州市| 如皋市| 乌恰县| 淮滨县| 太保市|