欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合aws的示例代碼

 更新時間:2021年12月01日 16:43:01   作者:一個不會代碼的搬磚人  
本文通過實(shí)例代碼給大家介紹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)換為二進(jì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 Admin健康檢查功能的實(shí)現(xiàn)

    SpringBoot Admin健康檢查功能的實(shí)現(xiàn)

    admin主要就是告訴運(yùn)維人員,服務(wù)出現(xiàn)異常,然后進(jìn)行通知(微信、郵件、短信、釘釘?shù)龋┛梢苑浅?焖偻ㄖ竭\(yùn)維人員,相當(dāng)報警功能,接下來通過本文給大家介紹SpringBoot Admin健康檢查的相關(guān)知識,一起看看吧
    2021-06-06
  • java GUI實(shí)現(xiàn)五子棋游戲

    java GUI實(shí)現(xiàn)五子棋游戲

    這篇文章主要為大家詳細(xì)介紹了java GUI實(shí)現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • Java接口返回省市區(qū)樹形結(jié)構(gòu)的實(shí)現(xiàn)

    Java接口返回省市區(qū)樹形結(jié)構(gòu)的實(shí)現(xiàn)

    本文主要介紹了Java接口返回省市區(qū)樹形結(jié)構(gòu)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • java后端調(diào)用第三方接口返回圖片流給前端的具體代碼實(shí)現(xiàn)

    java后端調(diào)用第三方接口返回圖片流給前端的具體代碼實(shí)現(xiàn)

    在前后端分離的開發(fā)中,經(jīng)常會遇到需要從后端返回圖片流給前端的情況,下面這篇文章主要給大家介紹了關(guān)于java后端調(diào)用第三方接口返回圖片流給前端的具體代碼實(shí)現(xiàn),需要的朋友可以參考下
    2024-02-02
  • java中獲取當(dāng)前服務(wù)器的Ip地址的方法

    java中獲取當(dāng)前服務(wù)器的Ip地址的方法

    本篇文章主要介紹了java中獲取當(dāng)前服務(wù)器的Ip地址的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • Spring中IOC和AOP的深入講解

    Spring中IOC和AOP的深入講解

    這篇文章主要給大家介紹了關(guān)于Spring中IOC和AOP的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • Java泛型最全知識總結(jié)

    Java泛型最全知識總結(jié)

    面試被問到Java泛型怎么辦,有了這篇文章,讓你直接保送,文中有非常詳細(xì)的知識總結(jié)及相關(guān)代碼示例,需要的朋友可以參考下
    2021-06-06
  • Springboot FeignClient調(diào)用Method has too many Body parameters解決

    Springboot FeignClient調(diào)用Method has too m

    本文主要介紹了Springboot FeignClient微服務(wù)間調(diào)用Method has too many Body parameters 解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • spring AOP定義AfterThrowing增加處理實(shí)例分析

    spring AOP定義AfterThrowing增加處理實(shí)例分析

    這篇文章主要介紹了spring AOP定義AfterThrowing增加處理,結(jié)合實(shí)例形式分析了spring面向切面AOP定義AfterThrowing相關(guān)實(shí)現(xiàn)步驟與操作技巧,需要的朋友可以參考下
    2020-01-01
  • springboot讀取配置文件中的參數(shù)具體步驟

    springboot讀取配置文件中的參數(shù)具體步驟

    在本篇文章里小編給大家分享了關(guān)于springboot讀取配置文件中的參數(shù)的相關(guān)知識點(diǎn)內(nèi)容,有需要的朋友們跟著學(xué)習(xí)下。
    2019-06-06

最新評論